05-JDBC连接MySQL数据库【删除数据】

忘是亡心i 2022-06-15 02:38 242阅读 0赞

JDBC自学教程–终篇总结:

地址:http://blog.csdn.net/baidu_37107022/article/details/72600018

1.实现修改步骤

这里写图片描述

前三个步骤:注册、获得连接,创建statement对象方法,见上一节:

02-JDBC实战–JDBC查询数据库MySQL–http://blog.csdn.net/baidu_37107022/article/details/72597975

2.使用jdbc删除数据库中的数据

这里使用的是queryDemo数据库,表格为demo1student,表中数据如下:
这里写图片描述

1)删除单个数据

代码演示

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.util.Properties;
  6. import org.junit.Test;
  7. public class Test11 {
  8. // 删除数据
  9. //删除单个数据
  10. @Test
  11. public void deleteOne() {
  12. Connection connection = null;
  13. PreparedStatement ps = null;
  14. try {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. String url = "jdbc:mysql://localhost:3306/queryDemo";
  17. Properties info = new Properties();
  18. info.put("user", "root");
  19. info.put("password", "123");
  20. connection = DriverManager.getConnection(url, info);
  21. String sql = "delete from demo1student where id between ? and ? ";
  22. ps = connection.prepareStatement(sql);
  23. ps.setInt(1, 13);
  24. ps.setInt(2, 17);
  25. int num = ps.executeUpdate();
  26. if (num > 0) {
  27. // 如果删除成功,则打印success
  28. System.out.println("Sucess");
  29. } else {
  30. // 如果删除失败,则打印Failure
  31. System.out.println("Failure");
  32. }
  33. } catch (ClassNotFoundException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } catch (SQLException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. } finally {
  40. // 5.关闭资源
  41. if (connection != null) {
  42. try {
  43. connection.close();
  44. } catch (SQLException e) {
  45. // TODO Auto-generated catch block
  46. e.printStackTrace();
  47. }
  48. }
  49. if (ps != null) {
  50. try {
  51. ps.close();
  52. } catch (SQLException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. }
  59. }

运行结果:

1.删除前

这里写图片描述

2.删除后
这里写图片描述


2)删除表格中所有数据

代码演示

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.util.Properties;
  6. import org.junit.Test;
  7. public class Test12 {
  8. // 删除表格中所有数据
  9. @Test
  10. public void deleteAll() {
  11. Connection connection = null;
  12. PreparedStatement ps = null;
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15. String url = "jdbc:mysql://localhost:3306/queryDemo";
  16. Properties info = new Properties();
  17. info.put("user", "root");
  18. info.put("password", "123");
  19. connection = DriverManager.getConnection(url, info);
  20. String sql = "delete from demo1student";
  21. ps = connection.prepareStatement(sql);
  22. int num = ps.executeUpdate();
  23. if (num > 0) {
  24. // 如果删除成功,则打印success
  25. System.out.println("Sucess");
  26. } else {
  27. // 如果删除失败,则打印Failure
  28. System.out.println("Failure");
  29. }
  30. } catch (ClassNotFoundException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. } catch (SQLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. } finally {
  37. // 5.关闭资源
  38. if (connection != null) {
  39. try {
  40. connection.close();
  41. } catch (SQLException e) {
  42. // TODO Auto-generated catch block
  43. e.printStackTrace();
  44. }
  45. }
  46. if (ps != null) {
  47. try {
  48. ps.close();
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. }
  56. }

运行结果:
1.删除前
这里写图片描述

2.删除后

这里写图片描述

发表评论

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

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

相关阅读