日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3、使用Statement接口实现增,删,改操作

發(fā)布時間:2025/3/20 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:

import java.sql.Connection; import java.sql.DriverManager;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(Connection con)throws Exception{if(con!=null){con.close();}} } import java.sql.Connection; import java.sql.Statement;import com.cn.jdbc.Util.DbUtil;public class jdbcTest {public static void main(String[] args) throws Exception {DbUtil dbUtil=new DbUtil();String sql="insert into student values(null,'東方不敗','女',1000) ";Connection con=dbUtil.getCon();//獲取數(shù)據(jù)庫連接Statement stmt=con.createStatement();//獲取Statementint result=stmt.executeUpdate(sql);//判斷是否成功System.out.println("操作的結(jié)果:"+result+"數(shù)據(jù)");stmt.close();//關(guān)閉Statementcon.close();//關(guān)閉連接} }

運(yùn)行結(jié)果:


實(shí)例2:使用變量,面向?qū)ο蟮姆椒?/strong>
1、創(chuàng)建學(xué)生模型

public class Student {private String stuName;private String stuSex;private int stuAge;public Student() {super();// TODO Auto-generated constructor stub}public Student(String stuName, String stuSex, int stuAge) {super();this.stuName = stuName;this.stuSex = stuSex;this.stuAge = stuAge;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getStuSex() {return stuSex;}public void setStuSex(String stuSex) {this.stuSex = stuSex;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;} }

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é)生模型

public class Student {private int id;private String stuName;private String stuSex;private int stuAge;public Student() {super();// TODO Auto-generated constructor stub}public Student(int id, String stuName, String stuSex, int stuAge) {super();this.id = id;this.stuName = stuName;this.stuSex = stuSex;this.stuAge = stuAge;}public String getStuName() {return stuName;}public void setStuName(String stuName) {this.stuName = stuName;}public String getStuSex() {return stuSex;}public void setStuSex(String stuSex) {this.stuSex = stuSex;}public int getStuAge() {return stuAge;}public void setStuAge(int stuAge) {this.stuAge = stuAge;}public int getId() {return id;}public void setId(int id) {this.id = id;} }

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、測試類

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 deleteStudent(int id)throws Exception{Connection con=dbUtil.getCon();//獲取連接String sql="delete from student where stuId="+id;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 {deleteStudent(1);} }

運(yùn)行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的3、使用Statement接口实现增,删,改操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。