3、使用Statement接口实现增,删,改操作
生活随笔
收集整理的這篇文章主要介紹了
3、使用Statement接口实现增,删,改操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、Statement接口引入
作用:用于執(zhí)行靜態(tài) SQL 語句并返回它所生成結(jié)果的對象。
常用方法:
- int executeUpdate(String sql) 執(zhí)行給定 SQL 語句,該語句可能為 INSERT、UPDATE 或DELETE 語句,或者不返回任何內(nèi)容的 SQL 語句(如 SQL DDL 語句)。
- void close() 立即釋放此 Statement 對象的數(shù)據(jù)庫和 JDBC 資源,而不是等待該對象自動關(guān)閉時發(fā)生此操作。
2、使用 Statement 接口實(shí)現(xiàn)添加數(shù)據(jù)操作
實(shí)例1:
創(chuàng)建工具類DbUtil:
運(yùn)行結(jié)果:
實(shí)例2:使用變量,面向?qū)ο蟮姆椒?/strong>
1、創(chuàng)建學(xué)生模型
2、工具類DbUtil
public class DbUtil {private static String jdbcName="com.mysql.jdbc.Driver";private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";private static String dbUserName="root";private static String dbPassword="root";public Connection getCon() throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);return con;}public void close(Statement stmt,Connection con) throws Exception{if(stmt!=null){stmt.close();if(con!=null){con.close();}}} }3、測試類
public class JDBCTest {private static DbUtil dbUtil=new DbUtil();Student student=new Student();/*** 添加學(xué)生* @param student* @return* @throws Exception*/public static int addStudent(Student student)throws Exception{Connection con=dbUtil.getCon();//獲取連接String sql="insert into student values(null,'"+student.getStuName()+"','"+student.getStuSex()+"',"+student.getStuAge()+")";Statement stmt=con.createStatement();//創(chuàng)建Statementint result=stmt.executeUpdate(sql);dbUtil.close(stmt, con);//關(guān)閉Statement和連接return result;}public static void main(String[] args)throws Exception {Student student=new Student("三娃","男",77);int result=addStudent(student);if(result==1){System.out.println("成功");}else{System.out.println("失敗");}} }3、使用 Statement 接口實(shí)現(xiàn)更新數(shù)據(jù)操作
實(shí)例1:通過ID更新數(shù)據(jù)
1、創(chuàng)建學(xué)生模型
2、工具類DbUtil
public class DbUtil {private static String dbUrl="jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=UTF-8";private static String dbUserName="root";private static String dbPassword="root";private static String jdbcName="com.mysql.jdbc.Driver";/*** 獲取數(shù)據(jù)庫連接* @return* @throws Exception*/public Connection getCon() throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword); return con;}/*** 關(guān)閉連接* @param con* @throws Exception*/public void close(Statement stmt,Connection con)throws Exception{if(stmt!=null){stmt.close();if(con!=null){con.close();}}} }3、測試類
public class jdbcTest {private static DbUtil dbUtil=new DbUtil();private static Student student=new Student();/*** 更新數(shù)據(jù)* @param stuName* @param stuSex* @param stuAge* @return*/public static int updateStudent(Student student)throws Exception{Connection con=dbUtil.getCon();//獲取連接String sql="update student set stuName='"+student.getStuName()+"',stuSex='"+student.getStuSex()+"',stuAge="+student.getStuAge()+" where stuId="+student.getId()+"";Statement stmt=con.createStatement();//創(chuàng)建Statementint result=stmt.executeUpdate(sql);//判斷是否成功dbUtil.close(stmt, con);//關(guān)閉Statement和連接return result;}public static void main(String[] args) throws Exception {Student student=new Student(1,"擎天柱","男",999);updateStudent(student);} }運(yùn)行結(jié)果:
第四節(jié):使用 Statement 接口實(shí)現(xiàn)刪除數(shù)據(jù)操作
實(shí)例1:
3、測試類
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的3、使用Statement接口实现增,删,改操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。