前面詳細寫過如何鏈接數據庫的具體操做,下面介紹向數據庫中添加數據。java
注意事項:若是參考下面代碼,須要mysql
改包名,數據庫名,數據庫帳號,密碼,和數據表(數據表裏面的信息)web
1 package com.ningmeng; 2 3 import java.sql.*; 4 5 /** 6 * 1:向數據庫中添加數據 7 * @author biexiansheng 8 * 9 */ 10 public class Test01 { 11 12 public static void main(String[] args) { 13 try { 14 Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動 15 System.out.println("加載數據庫驅動成功"); 16 String url="jdbc:mysql://localhost:3306/test";//聲明數據庫test的url 17 String user="root";//數據庫的用戶名 18 String password="123456";//數據庫的密碼 19 //創建數據庫鏈接,得到鏈接對象conn(拋出異常便可) 20 Connection conn=DriverManager.getConnection(url, user, password); 21 System.out.println("鏈接數據庫成功"); 22 //生成一條mysql語句 23 String sql="insert into users(username,password,age,sex) values('小別','123456',22,0)"; 24 Statement stmt=conn.createStatement();//建立一個Statement對象 25 stmt.executeUpdate(sql);//執行sql語句 26 System.out.println("插入到數據庫成功"); 27 conn.close(); 28 System.out.println("關閉數據庫成功"); 29 } catch (ClassNotFoundException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 }// 33 catch (SQLException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 38 } 39 40 }
詳細運行結果sql
這樣就能夠完美插入數據,增刪改查第一步完美解決。數據庫
簡單介紹一下所使用的知識點:eclipse
在java程序中一旦創建了數據庫的鏈接,就可使用Connection接口的createStatement()方法來得到statement對象ide
而後經過excuteUpdate()方法來執行sql語句,就能夠向數據庫中添加數據了。大數據
1:createStatement()方法是Connection接口的方法,用來建立Statement對象this
2:Connection接口表明和特定的數據庫鏈接,要對數據庫中數據表的數據進行操做,首先要獲取數據庫鏈接。url
3:Statement接口用於建立向數據庫中傳遞SQL語句的對象,該接口提供了一些方法能夠實現對數據庫的經常使用操做。
4:Statement接口中的excuteUpdate()方法執行給定的SQL語句,該語句能夠是INSERT,UPDATE,DELETE語句。
第二種方法
使用PreparedStatement接口向mysql數據庫中插入數據
1 package com.ningmeng; 2 3 import java.sql.*; 4 5 /** 6 * 1:使用PreparedStatement接口來執行插入語句 7 * 8 * @author biexiansheng 9 * 10 */ 11 public class Test02 { 12 13 public static void main(String[] args) { 14 // TODO Auto-generated method stub 15 try { 16 Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動 17 System.out.println("加載數據庫驅動成功"); 18 String url="jdbc:mysql://localhost:3306/test";//聲明數據庫test的url 19 String user="root";//數據庫用戶名 20 String password="123456";//數據庫密碼 21 //創建數據庫鏈接,得到鏈接對象conn 22 Connection conn=DriverManager.getConnection(url, user, password); 23 System.out.println("鏈接數據庫驅動成功"); 24 //生成一條SQL語句 25 String sql="insert into users(username,password,age,sex) values(?,?,?,?)"; 26 PreparedStatement ps=conn.prepareStatement(sql);//建立一個Statement對象 27 ps.setNString(1,"lisi");//爲sql語句中第一個問號賦值 28 ps.setString(2,"123456");//爲sql語句中第二個問號賦值 29 ps.setInt(3,24);//爲sql語句第三個問號賦值 30 ps.setInt(4,2);//爲sql語句的第四個問號賦值 31 ps.executeUpdate();//執行sql語句 32 conn.close();//關閉數據庫鏈接對象 33 System.out.println("關閉數據庫鏈接對象"); 34 } catch (ClassNotFoundException e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } catch (SQLException e) { 38 // TODO Auto-generated catch block 39 e.printStackTrace(); 40 } 41 42 43 } 44 45 }
因爲剛纔不當心多執行了一遍第一個程序,因此多了一行id==7的,特此註釋一下
1:PreparedStatement接口繼承Statement,用於執行動態的SQL語句,經過PreparedStatement實例執行SQL語句,將被預編譯而且保存到PreparedStatement實例中,從而能夠反覆的執行該SQL語句。
2:PreparementStatement接口中的方法,如executeUpdate在此PrepareStatement對象中執行sql語句,該sql語句必須是一個INSERT.UPDATE,DELETE語句,或者是沒有返回值的DDL語句。
3:setString(int pIndex,String str)將參數pIndex位置上設置爲給定的String類型的參數,俗語就是在第幾個位置寫上符合的數據類型
setInt(int pIndex,int x)
其餘的都相似,不做多敘述
更深層次的理解JDBC對java程序和數據庫之間的操做
JDBC技術的經常使用類和接口
必須搞明白這些關係,不能只會寫代碼,理解其含義。
(1):必須清楚,JDBC是一種可用於執行SQL語句的JAVA API(Application Programming Interface,應用程序設計接口),是鏈接數據庫和java應用程序的一個紐帶。
(2):DriverManager類用來管理數據庫中的全部驅動程序,是JDBC的管理層,做用與用戶和驅動程序之間,跟蹤可用的驅動程序,並在數據庫的驅動程序之間創建鏈接。
DriverManager類最經常使用的方法是
getConnection(String url,String user,String password);
(3):Connection接口表明與特定的數據庫的鏈接,要對數據表中的數據進行操做,首先要獲取數據庫的鏈接。Connection實例就像在應用程序與數據庫之間開通了一條通道。
可經過DriverManager類的getConnection()方法獲取Connection實例。
好比:
Connection conn=DriverManager.getConnection(url, user, password);
Statement stmt=conn.createStatement();//建立一個Statement對象
Connection接口經常使用的方法是:
createStatement()建立Statement對象
close()當即釋放此Connection對象的數據庫和JDBC資源,而不是等待它們被自動釋放。
(4):Statement接口用於建立向數據庫中傳遞SQL語句的對象,該接口提供了一些方法能夠實現對數據庫的經常使用操做。(Statement接口用於執行靜態SQL語句,並返回它所生成結果的對象)
Statement接口經常使用的方法
execute(String sql);執行靜態的SELECT語句,該語句可能返回多個結果集
executeQuery(String sql);執行給定的SQL語句,該語句返回單個ResultSet對象。
executeUpdate()執行給定的SQL語句,該語句能夠爲INSERT,UPDATE,DELETE語句。
close()釋放Statement實例佔用的數據庫和JDBC資源。
(5):PreparedStatement接口繼承了Statement接口,用於執行動態的SQL語句,經過PreparedStatement實例執行的sql語句,將被預編譯並保存到PreparedStatement實例中,從而能夠反覆的執行該SQL語句。
PreparedStatement接口的經常使用方法。
execute();在此PreparedStatement對象中執行SQL語句,該語句能夠是任何類型的SQL語句。
executeQuery()在此PreparedStatement對象中執行SQL查詢語句,返回結果爲查詢結果集ResultSet對象。
executeUpdate()在此PreparedStatement對象中執行SQL語句,該語句必須是一個INSERT,UPDATE,DELETE語句。或者是沒有返回值的DDL語句。
close()釋放Statement實例佔用的數據庫和JDBC資源。
(6):ResultSet接口相似與一個臨時表,用來暫時存放數據庫查詢操做所得到的結果集,
下面寫幾個程序更深層次的理解一下JDBC做爲鏈接數據庫的JAVA程序紐帶
1:首先封裝了通用的一些東西,而後經過引入調用(須要注意的是包名,類名,mysql數據庫帳號,密碼,數據庫名,數據表名,字段等等。)
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class Dbutil { 8 9 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 10 private static String user="root";//數據庫帳號 11 private static String password="123456";//數據庫密碼 12 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 13 14 /** 15 * 16 * @return 17 * @throws Exception 18 */ 19 public Connection getCon() throws Exception{ 20 Class.forName(driver);//加載數據庫驅動 21 Connection con=DriverManager.getConnection(url, user, password); 22 //創建數據庫的鏈接,得到鏈接對象con 23 return con; 24 } 25 26 /** 27 * 28 * @param con 29 * @throws Exception 30 */ 31 public void close(Connection con) throws Exception{ 32 if(con!=null){ 33 con.close(); 34 } 35 } 36 37 38 }
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 6 import com.util.Dbutil; 7 8 public class Test { 9 10 public static void main(String args[]) throws Exception { 11 Dbutil db=new Dbutil(); 12 String sql="insert into db_book values(null,'javaweb',888,'小別',1)";//生成一條sql語句 13 Connection con=db.getCon();//獲取數據庫的鏈接 14 Statement stmt=con.createStatement();//建立一個Statement鏈接 15 int result=stmt.executeUpdate(sql);//執行sql語句 16 System.out.println("執行了"+result+"數據"); 17 stmt.close();//關閉順序是先關閉小的,後關閉大的 18 con.close(); 19 20 } 21 }
2:數據庫的插入通常都是從前臺獲取的,上面這個例子不是很好,下面舉例另外一種方式。
(插入數據時使用拼接)一樣引用上面封裝的通用的一些東西!!!
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 6 import com.util.Dbutil; 7 8 public class Test2 { 9 10 private static Dbutil db=new Dbutil(); 11 private static int add(String name,float price,String author,int bookTypeId) 12 throws Exception{ 13 Connection con=db.getCon(); 14 String sql="insert into db_book values(null,'"+name+"',"+price+",'"+author+"',"+bookTypeId+")"; 15 Statement stmt=con.createStatement();//建立一個Statement鏈接 16 int result=stmt.executeUpdate(sql);//執行sql語句 17 db.close(stmt,con); 18 return result; 19 } 20 public static void main(String[] args) throws Exception { 21 // TODO Auto-generated method stub 22 int result=add("java",888,"小卡",1); 23 if(result==1){ 24 System.out.println("添加成功"); 25 }else{ 26 System.out.println("添加失敗"); 27 } 28 29 } 30 31 }
(能夠發現已經添加成功了)
3:下面使用面向對象的思想傳入數據(實現的時候和上面的一同完成操做,)
1 package com.ningmeng; 2 3 public class Book { 4 5 private String name; 6 private float price; 7 private String author; 8 private int bookTypeId; 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public float getPrice() { 16 return price; 17 } 18 public void setPrice(float price) { 19 this.price = price; 20 } 21 public String getAuthor() { 22 return author; 23 } 24 public void setAuthor(String author) { 25 this.author = author; 26 } 27 public int getBookTypeId() { 28 return bookTypeId; 29 } 30 public void setBookTypeId(int bookTypeId) { 31 this.bookTypeId = bookTypeId; 32 } 33 public Book(String name, float price, String author, int bookTypeId) { 34 super(); 35 this.name = name; 36 this.price = price; 37 this.author = author; 38 this.bookTypeId = bookTypeId; 39 } 40 41 42 43 44 }
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 6 import com.util.Dbutil; 7 8 public class Test2 { 9 10 private static Dbutil db=new Dbutil(); 11 12 private static int add2(Book book) throws Exception{ 13 Connection con=db.getCon(); 14 String sql="insert into db_book values(null,'"+book.getName()+"',"+book.getPrice()+",'"+book.getAuthor()+"',"+book.getBookTypeId()+")"; 15 Statement stmt=con.createStatement();//建立一個Statement鏈接 16 int result=stmt.executeUpdate(sql);//執行sql語句 17 db.close(stmt,con); 18 return result; 19 } 20 21 private static int add(String name,float price,String author,int bookTypeId) 22 throws Exception{ 23 Connection con=db.getCon(); 24 String sql="insert into db_book values(null,'"+name+"',"+price+",'"+author+"',"+bookTypeId+")"; 25 Statement stmt=con.createStatement();//建立一個Statement鏈接 26 int result=stmt.executeUpdate(sql);//執行sql語句 27 db.close(stmt,con); 28 return result; 29 } 30 public static void main(String[] args) throws Exception { 31 // TODO Auto-generated method stub 32 /*int result=add("java",888,"小卡",1); 33 if(result==1){ 34 System.out.println("添加成功"); 35 }else{ 36 System.out.println("添加失敗"); 37 }*/ 38 39 Book book=new Book("java2",888,"小卡2",1); 40 int result=add2(book); 41 if(result==1){ 42 System.out.println("添加成功"); 43 }else{ 44 System.out.println("添加失敗"); 45 } 46 47 } 48 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 40 } 41 42 43 }
1:使用Statement更新操做
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 6 import com.util.Dbutil; 7 8 /** 9 * 更新操做 10 * @author biexiansheng 11 * 12 */ 13 public class Test3 { 14 15 private static Dbutil db=new Dbutil(); 16 17 private static int update(Book book) throws Exception{ 18 Connection con=db.getCon(); 19 String sql="update db_book set name='"+book.getName()+"',price="+book.getPrice()+",author='"+book.getAuthor()+"',bookTypeId="+book.getBookTypeId()+" where id=13"; 20 Statement stmt=con.createStatement();//建立一個Statement鏈接 21 int result=stmt.executeUpdate(sql);//執行sql語句 22 db.close(stmt,con); 23 return result; 24 } 25 26 public static void main(String[] args) throws Exception { 27 Book book=new Book(13,"java120",666,"小別",1); 28 int result=update(book); 29 if(result==1){ 30 System.out.println("更新成功"); 31 }else{ 32 System.out.println("更新失敗"); 33 } 34 } 35 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 40 } 41 42 43 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 42 public Book(int id, String name, float price, String author, int bookTypeId) { 43 super(); 44 this.id = id; 45 this.name = name; 46 this.price = price; 47 this.author = author; 48 this.bookTypeId = bookTypeId; 49 } 50 51 52 53 54 55 56 }
(已經完成更新操做,須要注意的是在執行sql語句的時候因爲語句過長可使用eclipse自帶的排版功能,完成排版)
1:使用Statement執行刪除操做
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.Statement; 5 6 import com.util.Dbutil; 7 8 public class Test4 { 9 10 private static Dbutil db=new Dbutil(); 11 12 private static int delete(Book book) throws Exception{ 13 Connection con=db.getCon(); 14 String sql="delete from db_book where id="+book.getId(); 15 Statement stmt=con.createStatement();//建立一個Statement鏈接 16 int result=stmt.executeUpdate(sql);//執行sql語句 17 db.close(stmt,con); 18 return result; 19 } 20 public static void main(String[] args) throws Exception{ 21 // TODO Auto-generated method stub 22 Book book=new Book(13); 23 int result=delete(book); 24 if(result==1){ 25 System.out.println("刪除成功"); 26 }else{ 27 System.out.println("刪除失敗"); 28 } 29 } 30 31 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 40 } 41 42 43 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 public Book(int id) { 42 super(); 43 this.id = id; 44 } 45 46 47 48 49 50 }
(能夠看到刪除操做執行完畢。)
1:PreparedStatement是Statement的子接口,屬於預處理操做,與直接使用Statement不一樣的是,PreparedStatement在操做時,是先在數據表中準備好了一條SQL語句,可是此SQL語句的具體內容暫時不設置,而是以後再進行設置。
(之後開發通常用PreparedStatement,通常不用Statement)
2:PreparedStatement插入操做
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 6 import com.util.Dbutil; 7 8 public class Test5 { 9 10 private static Dbutil db=new Dbutil(); 11 12 private static int add(Book book) throws Exception{ 13 Connection con=db.getCon();//創建數據庫的鏈接 14 String sql="insert into db_book values(null,?,?,?,?)";//生成一條SQL語句 15 PreparedStatement pstmt=con.prepareStatement(sql);//建立Statement對象 16 pstmt.setString(1,book.getName()); 17 pstmt.setFloat(2,book.getPrice()); 18 pstmt.setString(3,book.getAuthor()); 19 pstmt.setInt(4,book.getBookTypeId()); 20 int result=pstmt.executeUpdate();//執行SQL語句 21 db.close(pstmt,con); 22 return result; 23 } 24 public static void main(String[] args) throws Exception{ 25 // TODO Auto-generated method stub 26 Book book =new Book("openstack",999,"小別",1); 27 int result=add(book); 28 if(result==1){ 29 System.out.println("插入成功"); 30 }else{ 31 System.out.println("插入失敗"); 32 } 33 } 34 35 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 public Book(String name, float price, String author, int bookTypeId) { 42 super(); 43 this.name = name; 44 this.price = price; 45 this.author = author; 46 this.bookTypeId = bookTypeId; 47 } 48 49 50 51 52 53 54 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }
(如圖已經完成插入操做)
3:PreparedStatement更新操做
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 /*public Book(String name, float price, String author, int bookTypeId) { 42 super(); 43 this.name = name; 44 this.price = price; 45 this.author = author; 46 this.bookTypeId = bookTypeId; 47 }*/ 48 public Book(int id, String name, float price, String author, int bookTypeId) { 49 super(); 50 this.id = id; 51 this.name = name; 52 this.price = price; 53 this.author = author; 54 this.bookTypeId = bookTypeId; 55 } 56 57 58 59 60 61 62 }
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 6 7 import com.util.Dbutil; 8 9 public class Test6 { 10 11 private static Dbutil db=new Dbutil(); 12 13 private static int add(Book book) throws Exception{ 14 Connection con=db.getCon();//創建數據庫的鏈接 15 String sql="update db_book set name=?,price=?,author=?,bookTypeId=? where id=?";//生成一條SQL語句 16 PreparedStatement pstmt=con.prepareStatement(sql);//建立Statement對象 17 pstmt.setString(1,book.getName()); 18 pstmt.setFloat(2,book.getPrice()); 19 pstmt.setString(3,book.getAuthor()); 20 pstmt.setInt(4,book.getBookTypeId()); 21 pstmt.setInt(5,book.getId()); 22 int result=pstmt.executeUpdate();//執行SQL語句 23 db.close(pstmt,con); 24 return result; 25 } 26 public static void main(String[] args) throws Exception{ 27 // TODO Auto-generated method stub 28 Book book =new Book(16,"javaweb",222,"小ma",1); 29 int result=add(book); 30 if(result==1){ 31 System.out.println("更新成功"); 32 }else{ 33 System.out.println("更新失敗"); 34 } 35 36 } 37 }
(有圖可見,已經完成更新操做)
4:PreparedStatement刪除操做
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 6 import com.util.Dbutil; 7 8 public class Test7 { 9 10 private static Dbutil db=new Dbutil(); 11 12 private static int add(Book book) throws Exception{ 13 Connection con=db.getCon();//創建數據庫的鏈接 14 String sql="delete from db_book where id=?";//生成一條SQL語句 15 PreparedStatement pstmt=con.prepareStatement(sql);//建立Statement對象 16 pstmt.setInt(1,book.getId()); 17 int result=pstmt.executeUpdate();//執行SQL語句 18 db.close(pstmt,con); 19 return result; 20 } 21 public static void main(String[] args) throws Exception{ 22 // TODO Auto-generated method stub 23 Book book =new Book(16); 24 int result=add(book); 25 if(result==1){ 26 System.out.println("刪除成功"); 27 }else{ 28 System.out.println("刪除失敗"); 29 } 30 31 } 32 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 /*public Book(String name, float price, String author, int bookTypeId) { 42 super(); 43 this.name = name; 44 this.price = price; 45 this.author = author; 46 this.bookTypeId = bookTypeId; 47 }*/ 48 /*public Book(int id, String name, float price, String author, int bookTypeId) { 49 super(); 50 this.id = id; 51 this.name = name; 52 this.price = price; 53 this.author = author; 54 this.bookTypeId = bookTypeId; 55 }*/ 56 public Book(int id) { 57 super(); 58 this.id = id; 59 } 60 61 62 63 64 65 66 }
(執行刪除操做完成後如圖)
1:ResultSet接口的使用
1 package com.ningmeng; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 7 import com.util.Dbutil; 8 9 public class Test8 { 10 11 private static Dbutil db=new Dbutil(); 12 13 public static void select() throws Exception{ 14 Connection con=db.getCon(); 15 String sql="select * from db_book"; 16 PreparedStatement pstmt=con.prepareStatement(sql); 17 ResultSet rs=pstmt.executeQuery(); 18 while(rs.next()){ 19 int id=rs.getInt(1);//id 開發的時候通常使用數據庫字段名 20 String name=rs.getString(2);//name 21 float price=rs.getFloat(3);//price 22 String author=rs.getString(4);//author 23 int bookTypeId=rs.getInt(5);//bookTypeId 24 System.out.println(id+"\t"+name+"\t"+price+"\t"+author+"\t"+bookTypeId); 25 } 26 27 } 28 public static void main(String[] args) throws Exception { 29 // TODO Auto-generated method stub 30 select(); 31 } 32 33 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 /*public Book(String name, float price, String author, int bookTypeId) { 42 super(); 43 this.name = name; 44 this.price = price; 45 this.author = author; 46 this.bookTypeId = bookTypeId; 47 }*/ 48 public Book(int id, String name, float price, String author, int bookTypeId) { 49 super(); 50 this.id = id; 51 this.name = name; 52 this.price = price; 53 this.author = author; 54 this.bookTypeId = bookTypeId; 55 } 56 public Book(int id) { 57 super(); 58 this.id = id; 59 } 60 61 }
2:之後開發使用的舉例
1 package com.ningmeng; 2 3 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.util.Dbutil; 11 12 public class Test9 { 13 14 private static Dbutil db=new Dbutil(); 15 16 private static List<Book> select() throws Exception{ 17 List<Book> list=new ArrayList<Book>(); 18 Connection con=db.getCon(); 19 String sql="select * from db_book"; 20 PreparedStatement pstmt=con.prepareStatement(sql); 21 ResultSet rs=pstmt.executeQuery(); 22 while(rs.next()){ 23 int id=rs.getInt(1);//id 開發的時候通常使用數據庫字段名 24 String name=rs.getString(2);//name 25 float price=rs.getFloat(3);//price 26 String author=rs.getString(4);//author 27 int bookTypeId=rs.getInt(5);//bookTypeId 28 Book book=new Book(id,name,price,author,bookTypeId); 29 list.add(book); 30 } 31 return list; 32 33 } 34 public static void main(String[] args) throws Exception { 35 // TODO Auto-generated method stub 36 List<Book> list=select(); 37 for(Book book : list){ 38 System.out.println(book); 39 } 40 } 41 42 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }
1 package com.ningmeng; 2 3 public class Book { 4 5 private int id; 6 private String name; 7 private float price; 8 private String author; 9 private int bookTypeId; 10 public String getName() { 11 return name; 12 } 13 public void setName(String name) { 14 this.name = name; 15 } 16 public float getPrice() { 17 return price; 18 } 19 public void setPrice(float price) { 20 this.price = price; 21 } 22 public String getAuthor() { 23 return author; 24 } 25 public void setAuthor(String author) { 26 this.author = author; 27 } 28 public int getBookTypeId() { 29 return bookTypeId; 30 } 31 public void setBookTypeId(int bookTypeId) { 32 this.bookTypeId = bookTypeId; 33 } 34 35 public int getId() { 36 return id; 37 } 38 public void setId(int id) { 39 this.id = id; 40 } 41 /*public Book(String name, float price, String author, int bookTypeId) { 42 super(); 43 this.name = name; 44 this.price = price; 45 this.author = author; 46 this.bookTypeId = bookTypeId; 47 }*/ 48 public Book(int id, String name, float price, String author, int bookTypeId) { 49 super(); 50 this.id = id; 51 this.name = name; 52 this.price = price; 53 this.author = author; 54 this.bookTypeId = bookTypeId; 55 } 56 public Book(int id) { 57 super(); 58 this.id = id; 59 } 60 @Override 61 public String toString() { 62 return "Book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + ", bookTypeId=" 63 + bookTypeId + "]"; 64 } 65 66 67 }
1:處理大數據對象
大數據對象處理主要有CLOB(character large object)和BLOB(binary large object)兩種類型的字段;在CLOB中能夠存儲大字符數據對象,好比長篇小說;在BLOB中能夠存放二進制大數據對象,好比圖片,電影,音樂;
1 package com.ningmeng; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.InputStream; 7 import java.sql.Blob; 8 import java.sql.Clob; 9 import java.sql.Connection; 10 import java.sql.PreparedStatement; 11 import java.sql.ResultSet; 12 13 import com.util.Dbutil; 14 15 public class Test10 { 16 17 private static Dbutil db=new Dbutil(); 18 19 private static int add(Book book) throws Exception{ 20 Connection con=db.getCon();//創建數據庫的鏈接 21 String sql="insert into db_book values(null,?,?,?,?,?,?)";//生成一條SQL語句 22 PreparedStatement pstmt=con.prepareStatement(sql);//建立Statement對象 23 pstmt.setString(1,book.getName()); 24 pstmt.setFloat(2,book.getPrice()); 25 pstmt.setString(3,book.getAuthor()); 26 pstmt.setInt(4,book.getBookTypeId()); 27 28 File context=book.getContext(); 29 InputStream input=new FileInputStream(context); 30 pstmt.setAsciiStream(5, input,context.length()); 31 32 File pic=book.getPic(); 33 InputStream input2=new FileInputStream(pic); 34 pstmt.setBinaryStream(6, input2,pic.length()); 35 36 37 int result=pstmt.executeUpdate();//執行SQL語句 38 db.close(pstmt,con); 39 return result; 40 } 41 42 public static void read(Book book) throws Exception{ 43 Connection con=db.getCon(); 44 String sql="select * from db_book where id="+book.getId(); 45 PreparedStatement pstmt= con.prepareStatement(sql); 46 ResultSet rs=pstmt.executeQuery(); 47 while(rs.next()){ 48 int id=rs.getInt("id"); 49 String name=rs.getString("name"); 50 float price=rs.getFloat("price"); 51 String author=rs.getString("author"); 52 int bookTypeId=rs.getInt("bookTypeId"); 53 Clob c=rs.getClob("context"); 54 String context=c.getSubString(1,(int)c.length()); 55 56 Blob b=rs.getBlob("pic"); 57 FileOutputStream fos=new FileOutputStream("G:/1.png"); 58 fos.write(b.getBytes(1, (int) b.length())); 59 fos.close(); 60 61 System.out.println("id"+"\t"+name+"\t"+price+"\t"+author+"\t"+bookTypeId+"\t"+context); 62 63 64 } 65 db.close(pstmt, con); 66 } 67 68 public static void main(String[] args) throws Exception{ 69 // TODO Auto-generated method stub 70 /*File context=new File("F:/子查詢.txt"); 71 File pic=new File("F:/1.png"); 72 Book book=new Book("javaweb",888,"小別",1,context,pic); 73 int result=add(book); 74 if(result==1){ 75 System.out.println("插入成功"); 76 }else{ 77 System.out.println("插入失敗"); 78 }*/ 79 80 Book book2=new Book(19); 81 read(book2); 82 83 84 } 85 86 }
1 package com.ningmeng; 2 3 import java.io.File; 4 5 public class Book { 6 7 private int id; 8 private String name; 9 private float price; 10 private String author; 11 private int bookTypeId; 12 private File context;//處理文本 13 private File pic;//處理圖片的 14 15 public String getName() { 16 return name; 17 } 18 public void setName(String name) { 19 this.name = name; 20 } 21 public float getPrice() { 22 return price; 23 } 24 public void setPrice(float price) { 25 this.price = price; 26 } 27 public String getAuthor() { 28 return author; 29 } 30 public void setAuthor(String author) { 31 this.author = author; 32 } 33 public int getBookTypeId() { 34 return bookTypeId; 35 } 36 public void setBookTypeId(int bookTypeId) { 37 this.bookTypeId = bookTypeId; 38 } 39 40 public int getId() { 41 return id; 42 } 43 public void setId(int id) { 44 this.id = id; 45 } 46 47 48 public File getContext() { 49 return context; 50 } 51 public void setContext(File context) { 52 this.context = context; 53 } 54 55 56 public File getPic() { 57 return pic; 58 } 59 public void setPic(File pic) { 60 this.pic = pic; 61 } 62 63 64 /*public Book(String name, float price, String author, int bookTypeId) { 65 super(); 66 this.name = name; 67 this.price = price; 68 this.author = author; 69 this.bookTypeId = bookTypeId; 70 }*/ 71 public Book(int id, String name, float price, String author, int bookTypeId) { 72 super(); 73 this.id = id; 74 this.name = name; 75 this.price = price; 76 this.author = author; 77 this.bookTypeId = bookTypeId; 78 } 79 public Book(int id) { 80 super(); 81 this.id = id; 82 } 83 @Override 84 public String toString() { 85 return "Book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + ", bookTypeId=" 86 + bookTypeId + "]"; 87 } 88 public Book(String name, float price, String author, int bookTypeId, File context) { 89 super(); 90 this.name = name; 91 this.price = price; 92 this.author = author; 93 this.bookTypeId = bookTypeId; 94 this.context = context; 95 } 96 public Book(String name, float price, String author, int bookTypeId, File context, File pic) { 97 super(); 98 this.name = name; 99 this.price = price; 100 this.author = author; 101 this.bookTypeId = bookTypeId; 102 this.context = context; 103 this.pic = pic; 104 } 105 106 107 108 //構造方法是根據不一樣的初始化對象的須要構造的 109 }
1 package com.util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.Statement; 7 8 public class Dbutil { 9 10 private static String url="jdbc:mysql://localhost:3306/db_person";//生命數據庫的url(地址) 11 private static String user="root";//數據庫帳號 12 private static String password="123456";//數據庫密碼 13 private static String driver="com.mysql.jdbc.Driver";//數據庫的驅動 14 15 /** 16 * 17 * @return 18 * @throws Exception 19 */ 20 public Connection getCon() throws Exception{ 21 Class.forName(driver);//加載數據庫驅動 22 Connection con=DriverManager.getConnection(url, user, password); 23 //創建數據庫的鏈接,得到鏈接對象con 24 return con; 25 } 26 27 /** 28 * 29 * @param con 30 * @throws Exception 31 */ 32 public void close(Statement stmt,Connection con) throws Exception{ 33 if(stmt!=null){ 34 stmt.close(); 35 if(con!=null){ 36 con.close(); 37 } 38 } 39 } 40 41 public void close(PreparedStatement pstmt,Connection con) throws Exception{ 42 if(pstmt!=null){ 43 pstmt.close(); 44 if(con!=null){ 45 con.close(); 46 } 47 } 48 } 49 50 }