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

拼搏现实的明天。 2022-05-19 00:37 364阅读 0赞

一、Statement接口引入

作用:用于执行静态SQL语句并返回它所生成结果的对象

int executeUpdate(String sql) 执行给定SQL语句,该语句可能为INSERT、UPDATE、或DELETE语句,或者不返回任何内容的SQL语句(如SQLDDL语句)。

void close() 立即释放此Statement对象的数据库和JDBC资源,而不是等待该对象自动关闭时发生此操作。

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

demo1:

  1. public class Demo1 {
  2. public static void main(String[] args) throws Exception {
  3. DbUtil dbUtil = new DbUtil();
  4. String sql = "insert into t_book values(null,'java牛逼',888,'B哥',1)";
  5. Connection con = dbUtil.getCon();//获取数据库连接
  6. Statement stmt = con.createStatement();//获取Statement
  7. int result = stmt.executeUpdate(sql);
  8. System.out.println("操作的结果:"+result+"数据");
  9. stmt.close();//关闭statement
  10. con.close();//关闭连接
  11. }
  12. }

demo2:

  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. //Demo2
  34. public class Demo2 {
  35. private static DbUtil dbUtil = new DbUtil();
  36. /**
  37. * 添加图书
  38. */
  39. private static int addBook(String bookName,float price,String author,int bookTypeId) throws Exception {
  40. Connection con = dbUtil.getCon();
  41. String sql = "insert into t_book values(null,'"+bookName+"',"+price+",'"+author+"',"+bookTypeId+")";
  42. Statement stmt = con.createStatement();//创建Statement
  43. int result = stmt.executeUpdate(sql);
  44. dbUtil.close(stmt, con);//关闭Statement和连接
  45. return result;
  46. }
  47. public static void main(String[] args) throws Exception {
  48. String bookName = "Java你牛";
  49. float price = 100;
  50. String author = "牛牛";
  51. int bookTypeId = 1;
  52. int result = addBook(bookName, price, author, bookTypeId);
  53. if(result == 1) {
  54. System.out.println("添加成功!");
  55. }else {
  56. System.out.println("添加失败!");
  57. }
  58. }
  59. }

demo3:以对象的方式

  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. 。。。//get和set方法省略
  48. }
  49. //demo3
  50. public class Demo3 {
  51. private static DbUtil dbUtil = new DbUtil();
  52. /**
  53. * 添加图书2 以对象的方式
  54. */
  55. private static int addBook2(Book book) throws Exception{
  56. Connection con = dbUtil.getCon();
  57. String sql = "insert into t_book values(null,'"+book.getBookName()+"',"+book.getPrice()+",'"+book.getAuthor()+"',"+book.getBookTypeId()+")";
  58. Statement stmt = con.createStatement();//创建Statement
  59. int result = stmt.executeUpdate(sql);
  60. dbUtil.close(stmt, con);//关闭Statement和连接
  61. return result;
  62. }
  63. public static void main(String[] args) throws Exception {
  64. Book book = new Book("Java牛牛2", 99, "牛牛2", 2);
  65. int result = addBook2(book);
  66. }
  67. }

#

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

  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 Demo1 {
  58. private static DbUtil dbUtil = new DbUtil();
  59. private static int updateBook(Book book) throws Exception {
  60. Connection conn = dbUtil.getCon();//获取连接
  61. String sql = "update t_book set bookName='"+book.getBookName()
  62. +"',price="+book.getPrice()+",author='"+book.getAuthor()
  63. +"',bookTypeId="+book.getBookTypeId()+" where id="
  64. +book.getId();
  65. Statement stmt = conn.createStatement();
  66. int result = stmt.executeUpdate(sql);
  67. dbUtil.close(stmt, conn);//关闭statemet和连接
  68. return result;
  69. }
  70. public static void main(String[] args) throws Exception {
  71. Book book = new Book(3, "java牛牛222", 123, "牛哥", 1);
  72. int result = updateBook(book);
  73. System.out.println("更新了"+result+"数据");
  74. if(result==1) {
  75. System.out.println("更新成功");
  76. }else {
  77. System.out.println("更新失败");
  78. }
  79. }
  80. }

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

  1. public class Demo2 {
  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 = "+id;
  6. Statement stmt = con.createStatement();//创建Statement
  7. int result = stmt.executeUpdate(sql);
  8. dbUtil.close(stmt, con);
  9. return result;
  10. }
  11. public static void main(String[] args) throws Exception {
  12. int result = deleteBook(3);
  13. if(result ==1) {
  14. System.out.println("删除成功!");
  15. }else {
  16. System.out.println("删除失败!");
  17. }
  18. }
  19. }

发表评论

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

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

相关阅读