使用Statement接口实现增,删,改操作
生活随笔
收集整理的這篇文章主要介紹了
使用Statement接口实现增,删,改操作
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Statement 接口引入
作用:用于執(zhí)行靜態(tài) SQL 語(yǔ)句并返回它所生成結(jié)果的對(duì)象。
- int executeUpdate(String sql) 執(zhí)行給定 SQL 語(yǔ)句,該語(yǔ)句可能為 INSERT、UPDATE 或
DELETE 語(yǔ)句,或者不返回任何內(nèi)容的 SQL 語(yǔ)句(如 SQL DDL 語(yǔ)句)。 - void close() 立即釋放此 Statement 對(duì)象的數(shù)據(jù)庫(kù)和 JDBC 資源,而不是等待該對(duì)象自動(dòng)關(guān)閉時(shí)發(fā)生此操作。
使用 Statement 接口實(shí)現(xiàn)添加數(shù)據(jù)操作
添加數(shù)據(jù)時(shí),若添加后的數(shù)據(jù)為???則在連接數(shù)據(jù)庫(kù)時(shí)加上?useUnicode=true&characterEncoding=UTF-8這一句話。如下所示:
private static String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8";
數(shù)據(jù)Book.java
連接數(shù)據(jù)庫(kù)通用方法DbUtil.java
import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; /*** 通用方法* @author Administrator**/ public class DbUtil {//驅(qū)動(dòng)名稱private static String jdbcName="com.mysql.jdbc.Driver";//mysql數(shù)據(jù)庫(kù)地址private static String dbUrl="jdbc:mysql://localhost:3306/db_book?useUnicode=true&characterEncoding=UTF-8";//用戶名private static String dbUserName="root";//密碼private static String dbPassword="root";//獲取數(shù)據(jù)庫(kù)連接的方法public Connection getCon() throws Exception{Class.forName(jdbcName); //加載驅(qū)動(dòng)Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);return con;}//關(guān)閉數(shù)據(jù)庫(kù)連接public void close(Statement stmt,Connection con)throws Exception{if(stmt!=null){stmt.close();if(con!=null){con.close();}}} }插入數(shù)據(jù)demo02.java
import java.sql.Connection; import java.sql.Statement;import com.cn.zj.JDBC.model.Book; import com.cn.zj.JDBCUtil.DbUtil;public class demo02 {private static DbUtil dbUtil=new DbUtil();/*** 添加圖書2* @param book* @return* @throws Exception*/private static int addBook2(Book book) throws Exception{Connection con=dbUtil.getCon(); //獲取連接//SQL語(yǔ)句String sql="insert into t_book values(null,'"+book.getBookName()+"',"+book.getPrice()+",'"+book.getAuthor()+"',"+book.getBookTypeId()+")";Statement stmt=con.createStatement(); //創(chuàng)建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con); //關(guān)閉連接return result;}/*** 添加圖書* @param bookName* @param price* @param author* @param bookTypeId* @return* @throws Exception */public static int addBook(String bookName,float price,String author,int bookTypeId) throws Exception{Connection con=dbUtil.getCon(); //獲取連接String sql="insert into t_book values(null,'"+bookName+"',"+price+",'"+author+"',"+bookTypeId+")";Statement stmt=con.createStatement(); //創(chuàng)建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con); //關(guān)閉連接return result;}public static void main(String[] args) throws Exception {/* int result=addBook("我是你媽媽",99,"我",1);if(result==1){System.out.println("數(shù)據(jù)添加成功");}else{System.out.println("添加失敗!");}*///多行注釋 ctrl+shift+/Book book=new Book("我是你后媽",99,"我兒",1);int result=addBook2(book);if(result==1){System.out.println("數(shù)據(jù)添加成功");}else{System.out.println("添加失敗!");}} }使用 Statement 接口實(shí)現(xiàn)更新數(shù)據(jù)操作
數(shù)據(jù)Book.java
因?yàn)楦鶕?jù)ID更改數(shù)據(jù),所以重載構(gòu)造方法
更改數(shù)據(jù)方法
import java.sql.Connection; import java.sql.Statement;import com.cn.zj.JDBC.model.Book; import com.cn.zj.JDBCUtil.DbUtil; /*** 更新數(shù)據(jù)* @author Administrator**/ public class demo {private static DbUtil dbUtil=new DbUtil();private static int updateBook(Book book)throws Exception{Connection con=dbUtil.getCon(); //獲取連接//根據(jù)ID來(lái)對(duì)數(shù)據(jù)進(jìn)行更新String sql = "update t_book set bookName='" + book.getBookName()+ "',price=" + book.getPrice() + ",author='" + book.getAuthor()+ "',bookTypeId=" + book.getBookTypeId() + " where id="+ book.getId(); // ctrl+a 全選 ctrl+shift+F 格式化代碼Statement stmt=con.createStatement(); //創(chuàng)建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con); //關(guān)閉連接return result;}public static void main(String[] args) throws Exception {Book book=new Book(1,"我是你爹爹",9,"爹爹",2);int result=updateBook(book);if(result==1){System.out.println("數(shù)據(jù)更新成功");}else{System.out.println("更新失敗!");}} }使用 Statement 接口實(shí)現(xiàn)刪除數(shù)據(jù)操作
刪除數(shù)據(jù)方法
import java.sql.Connection; import java.sql.Statement;import com.cn.zj.JDBCUtil.DbUtil; /** 刪除數(shù)據(jù)*/ public class demo {private static DbUtil dbUtil=new DbUtil();private static int deleteBook(int id)throws Exception{Connection con=dbUtil.getCon(); //獲取連接//根據(jù)ID來(lái)對(duì)數(shù)據(jù)進(jìn)行更新String sql ="delete from t_book where id="+id;Statement stmt=con.createStatement(); //創(chuàng)建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con); //關(guān)閉連接return result;}public static void main(String[] args) throws Exception {int result=deleteBook(10017);if(result==1){System.out.println("數(shù)據(jù)更新成功");}else{System.out.println("更新失敗!");}} }總結(jié)
以上是生活随笔為你收集整理的使用Statement接口实现增,删,改操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。