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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4、使用PreparedStatement接口实现增,删,改操作(常用)

發布時間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4、使用PreparedStatement接口实现增,删,改操作(常用) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一節:PreparedStatement接口引入

PreparedStatement 是 Statement 的子接口,屬于預處理操作,與直接使用 Statement 不同的是,PreparedStatement

在操作時,是先在數據表中準備好了一條 SQL 語句,但是此 SQL 語句的具體內容暫時不設置,而是之后再進行設置。

(以后開發一般用 PreparedStatement,不用 Statement)

第二節:使用PreparedStatement接口實現添加數據操作

實例1:
1、學生模型類

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(String stuName, String stuSex, int stuAge) {super();this.stuName = stuName;this.stuSex = stuSex;this.stuAge = stuAge;}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、工具類

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";/*** 獲取數據庫連接* @return* @throws Exception*/public Connection getCon() throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword); return con;}/*** 關閉連接* @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();/*** 添加數據* @param stuName* @param stuSex* @param stuAge* @return*/public static int addStudent(Student student)throws Exception{Connection con=dbUtil.getCon();//獲取連接String sql="insert into student values(null,?,?,?)";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, student.getStuName());//給第一個?設置值pstmt.setString(2, student.getStuSex());//給第二個?設置值pstmt.setInt(3, student.getStuAge());給第三個?設置值int result=pstmt.executeUpdate();dbUtil.close(pstmt, con);//pstmt是子類,可以關閉return result;}public static void main(String[] args) throws Exception {Student student=new Student("大黃蜂","女",8979);addStudent(student);} }

第三節:使用PreparedStatement接口實現更新數據操作

實例:
3、測試類

public class jdbcTest {private static DbUtil dbUtil=new DbUtil();private static Student student=new Student();/*** 更新數據* @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=?,stuSex=?,stuAge=? where stuId=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, student.getStuName());//給第一個?設置值pstmt.setString(2, student.getStuSex());//給第二個?設置值pstmt.setInt(3, student.getStuAge());給第三個?設置值pstmt.setInt(4, student.getId());int result=pstmt.executeUpdate();dbUtil.close(pstmt, con);//pstmt是子類,可以關閉return result;}public static void main(String[] args) throws Exception {Student student=new Student(8,"慕娜美","女",89);updateStudent(student);} }

第四節:使用PreparedStatement接口實現刪除數據操作

實例:
3、測試類

public class jdbcTest {private static DbUtil dbUtil=new DbUtil();private static Student student=new Student();/*** 刪除數據* @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=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setInt(1, id);int result=pstmt.executeUpdate();dbUtil.close(pstmt, con);//pstmt是子類,可以關閉return result;}public static void main(String[] args) throws Exception {deleteStudent(8);} }

運行結果:

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。