JDBC连接数据库进行增删改查

ゝ一纸荒年。 2024-04-01 12:27 190阅读 0赞

什么是JDBC?

JDBC的全称是Java数据库连接(Java Database connect),它是一套用于执行SQL语句的Java API。应用程序可通过这套API连接到关系数据库,并使用SQL语句来完成对数据库中数据的查询、更新和删除等操作。

0d3b1810fe87491a9c295635bef8b159.jpeg

MySQL代码:

  1. # 创建一个数据库
  2. create database school;
  3. # 使用这个数据库
  4. use school;
  5. # 如果student这个表存在就删除这个表
  6. drop table if exists student;
  7. # 创建一个student表
  8. create table student(
  9. stuId int primary key auto_increment,
  10. stuName varchar(16) not null,
  11. stuSex varchar(8) not null,
  12. stuAddr varchar(16) not null
  13. )
  14. # 向student这个表添加数据
  15. insert into student values(1,'剑圣','男','召唤师峡谷')
  16. insert into student(stuName,stuSex,stuAddr) values('小红','女','China')
  17. insert into student(stuName,stuSex,stuAddr) values('D.va','女','OW')
  18. insert into student(stuName,stuSex,stuAddr) values('美羊羊','女','羊村')

JDBC操作数据库的步骤

1.首先在根目录创建一个lib文件夹,放入JDBC驱动程序,然后Add As Library

e32d648f884741558119c47f98a39231.png522e3985d0ac47809ae3c8765b4783a7.png

2.加载数据库驱动,使用反射加载

  1. Class.forName("com.mysql.cj.jdbc.Driver");// 注意!如果JDBC驱动程序是版本5的 Driver的地址为:com.mysql.jdbc.Driver

3.使用驱动管理器来获得连接——获得一个数据库连接对象Connection

  1. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC", username, password);// username为你的数据库用户名,password为你的数据库密码

4.使用Connection创建PreparedStatement预处理对象,使用PreparedStatement来执行sql语句

  1. String sql = "select * from student";
  2. PreparedStatement ps = con.prepareStatement(sql);

5.操作判断——增删改返回的是影响的行数(返回值是int),只有查询获得结果集(返回值是ResultSet)

  1. // 增删改的操作判断
  2. int a = ps.executeUpdate();
  3. if (a > 0) {
  4. System.out.println("成功");
  5. } else {
  6. System.out.println("失败");
  7. }
  8. // 查询的操作判断
  9. ResultSet re = ps.executeQuery();
  10. while (re.next()) {
  11. int stuId = re.getInt(1);
  12. String stuName = re.getString(2);
  13. String stuSex = re.getString(3);
  14. String stuAddr = re.getString(4);
  15. System.out.println(stuId + "----" + stuName + "----" + stuSex + "----" + stuAddr);
  16. }

6.回收资源

  1. if (re != null) {
  2. re.close();
  3. }
  4. if (ps != null) {
  5. ps.close();
  6. }
  7. if (con != null) {
  8. con.close();
  9. }

全部代码:

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.util.Scanner;
  6. public class Main {
  7. private static String driver = "com.mysql.cj.jdbc.Driver";
  8. private static String url = "jdbc:mysql://localhost:3306/school?useSSL=false&serverTimezone=UTC";
  9. private static String username = "root";
  10. private static String password = "123456";
  11. public static void main(String[] args) throws Exception {
  12. Scanner scanner = new Scanner(System.in);
  13. System.out.println("1--查询表");
  14. System.out.println("2--删除数据");
  15. System.out.println("3--修改数据");
  16. System.out.println("4--增加数据");
  17. int a = scanner.nextInt();
  18. if (a == 1) {
  19. test();
  20. } else if (a == 2) {
  21. testDelete();
  22. } else if (a == 3) {
  23. testUpdate();
  24. } else if (a == 4) {
  25. testAdd();
  26. } else {
  27. System.out.println("请输入正确的数字");
  28. }
  29. }
  30. /*
  31. * 查询
  32. * */
  33. // @Test
  34. public static void test() throws Exception {
  35. Class.forName(driver);
  36. Connection con = DriverManager.getConnection(url, username, password);
  37. String sql = "select * from student";
  38. PreparedStatement ps = con.prepareStatement(sql);
  39. ResultSet re = ps.executeQuery();
  40. while (re.next()) {
  41. int stuId = re.getInt(1);
  42. String stuName = re.getString(2);
  43. String stuSex = re.getString(3);
  44. String stuAddr = re.getString(4);
  45. System.out.println(stuId + "----" + stuName + "----" + stuSex + "----" + stuAddr);
  46. }
  47. if (re != null) {
  48. re.close();
  49. }
  50. if (ps != null) {
  51. ps.close();
  52. }
  53. if (con != null) {
  54. con.close();
  55. }
  56. }
  57. /*
  58. * 添加
  59. * */
  60. // @Test
  61. public static void testAdd() throws Exception {
  62. Class.forName(driver);
  63. Connection conn = DriverManager.getConnection(url, username, password);
  64. String sql = "insert into student(stuName,stuSex,stuAddr) values (?,?,?)";
  65. PreparedStatement ps = conn.prepareStatement(sql);
  66. ps.setString(1, "Jack");
  67. ps.setString(2, "男");
  68. ps.setString(3, "漂亮国");
  69. int a = ps.executeUpdate();
  70. if (a > 0) {
  71. System.out.println("添加成功,已添加Jack");
  72. } else {
  73. System.out.println("添加失败,表中已有Jack");
  74. }
  75. if (ps != null) {
  76. ps.close();
  77. }
  78. if (conn != null) {
  79. conn.close();
  80. }
  81. }
  82. /*
  83. * 删除
  84. * */
  85. // @Test
  86. public static void testDelete() throws Exception {
  87. Class.forName(driver);
  88. Connection conn = DriverManager.getConnection(url, username, password);
  89. String sql = "delete from student where stuId = ?";
  90. PreparedStatement ps = conn.prepareStatement(sql);
  91. ps.setInt(1, 4);
  92. int a = ps.executeUpdate();
  93. if (a > 0) {
  94. System.out.println("删除成功,已删除编号为4的同学");
  95. } else {
  96. System.out.println("删除失败,表中没有编号为4的同学");
  97. }
  98. if (ps != null) {
  99. ps.close();
  100. }
  101. if (conn != null) {
  102. conn.close();
  103. }
  104. }
  105. /*
  106. * 修改
  107. * */
  108. // @Test
  109. public static void testUpdate() throws Exception {
  110. Class.forName(driver);
  111. Connection conn = DriverManager.getConnection(url, username, password);
  112. String sql = "update student set stuName = ? where stuName = ?";
  113. PreparedStatement ps = conn.prepareStatement(sql);
  114. ps.setString(1, "Michael");
  115. ps.setString(2, "Jack");
  116. int a = ps.executeUpdate();
  117. if (a > 0) {
  118. System.out.println("修改成功,已将Jack改为Michael");
  119. } else {
  120. System.out.println("修改失败");
  121. }
  122. if (ps != null) {
  123. ps.close();
  124. }
  125. if (conn != null) {
  126. conn.close();
  127. }
  128. }
  129. }

发表评论

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

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

相关阅读