JDBC之——PreparedStatement实现对数据库的增删改操作

傷城~ 2022-05-19 01:09 272阅读 0赞

一、PreparedStatement接口引入

PreparedStatement是Statement的子接口,属于预处理操作,与直接使用Statement不同的是,PreparedStatement在操作时,是现在数据表中准备好了一条SQL语句,但是此SQL语句的具体内容暂时不设置,二十之后再进行设置。

(以后开发一部用PreparedStatement,不用Statement)

二、使用PreparedStatement接口实现添加数据操作

  1. //工具
  2. public class DbUtil {
  3. //数据库地址
  4. private static String dbUrl="jdbc:mysql://localhost:3306/db_book";
  5. //用户名
  6. private static String dbUserName="root";
  7. //密码
  8. private static String dbPassword="root";
  9. //驱动名称
  10. private static String jdbcName="com.mysql.jdbc.Driver";
  11. /**
  12. * 获取数据库连接
  13. * 1.加载数据库驱动
  14. * 2.获取数据库连接
  15. */
  16. public Connection getCon() throws Exception {
  17. Class.forName(jdbcName);//加载数据库驱动
  18. Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
  19. return con;
  20. }
  21. /**
  22. * 关闭连接
  23. */
  24. public void close(Statement stmt,Connection con) throws Exception {
  25. if(stmt!=null) {
  26. stmt.close();
  27. }
  28. if(con!=null) {
  29. con.close();
  30. }
  31. }
  32. }
  33. //book对象
  34. public class Book {
  35. private int id;
  36. private String bookName;
  37. private float price;
  38. private String author;
  39. private int bookTypeId;
  40. public Book(String bookName, float price, String author, int bookTypeId) {
  41. super();
  42. this.bookName = bookName;
  43. this.price = price;
  44. this.author = author;
  45. this.bookTypeId = bookTypeId;
  46. }
  47. public Book(int id, String bookName, float price, String author, int bookTypeId) {
  48. super();
  49. this.id = id;
  50. this.bookName = bookName;
  51. this.price = price;
  52. this.author = author;
  53. this.bookTypeId = bookTypeId;
  54. }
  55. }
  56. //测试demo
  57. public class Demo01{
  58. private static DbUtil dbUtil = new DbUtil();
  59. private static int addBook(Book book)throws Exception{
  60. Connection con = dbUtil.getCon();
  61. String sql = "insert into t_book values(null,?,?,?,?)";
  62. PreparedStatement pstmt = con.prepareStatement(sql);
  63. pstmt.setString(1, book.getBookName());//给第一个坑设值
  64. pstmt.setFloat(2, book.getPrice());
  65. pstmt.setString(3, book.getAuthor());
  66. pstmt.setInt(4, book.getBookTypeId());
  67. int result = pstmt.executeUpdate();
  68. dbUtil.close(pstmt, con);
  69. return result;
  70. }
  71. public static void main(String[] args) throws Exception {
  72. Book book = new Book("java测试", 99, "测试", 1);
  73. int result = addBook(book);
  74. if(result ==1) {
  75. System.out.println("添加成功");
  76. }else {
  77. System.out.println("添加失败");
  78. }
  79. }
  80. }

#

三、使用PreparedStatement接口实现更新数据操作

  1. public class demo02 {
  2. private static DbUtil dbUtil = new DbUtil();
  3. private static int updateBook(Book book)throws Exception{
  4. Connection con = dbUtil.getCon();
  5. String sql = "update t_book set bookName=?,price=?,author=?,bookTypeId=? where id=?";
  6. PreparedStatement pstmt = con.prepareStatement(sql);
  7. pstmt.setString(1, book.getBookName());
  8. pstmt.setFloat(2, book.getPrice());
  9. pstmt.setString(3, book.getAuthor());
  10. pstmt.setInt(4, book.getBookTypeId());
  11. pstmt.setInt(5, book.getId());
  12. int result = pstmt.executeUpdate();
  13. dbUtil.close(pstmt, con);
  14. return result;
  15. }
  16. public static void main(String[] args) throws Exception {
  17. Book book = new Book(1, "book1", 222, "book作者", 3);
  18. int result = updateBook(book);
  19. if(result ==1) {
  20. System.out.println("更新数据成功");
  21. }else {
  22. System.out.println("更新数据失败");
  23. }
  24. }
  25. }

#

四、使用PreparedStatement接口实现删除数据操作

  1. public class Demo3 {
  2. private static DbUtil dbUtil = new DbUtil();
  3. private static int deleteBook(int id) throws Exception{
  4. Connection con = dbUtil.getCon();
  5. String sql = "delete from t_book where id = ?";
  6. PreparedStatement pstmt = con.prepareStatement(sql);
  7. pstmt.setInt(1, id);
  8. int result = pstmt.executeUpdate();
  9. dbUtil.close(pstmt, con);
  10. return result;
  11. }
  12. public static void main(String[] args) throws Exception {
  13. int result = deleteBook(6);
  14. if(result ==1 ) {
  15. System.out.println("删除成功");
  16. }else {
  17. System.out.println("删除失败");
  18. }
  19. }
  20. }

发表评论

表情:
评论列表 (有 0 条评论,272人围观)

还没有评论,来说两句吧...

相关阅读