JDBC+ORM 增删改查 MySQL

本是古典 何须时尚 2024-03-23 10:09 146阅读 0赞

目录

一、数据库

二、封装Animal类

三、增

四、删

五、改

六、查

七、细节过程都在注释里


所谓ORM,即Object Relational Mapping。由于从数据库查询到的结果集(ResultSet)在进行遍历时,逐行遍历,然后一个字段一个字段的读取数据,取出的都是零散的数据,然后将零散数据一个一个输出,这样比较麻烦,不利于操作。所以在实际应用开发中,我们需要将零散的数据进行封装整理。

一、数据库

  1. CREATE TABLE Animal(
  2. aid INT(10) PRIMARY KEY AUTO_INCREMENT,
  3. `name` VARCHAR(10) NOT NULL,
  4. age INT(2),
  5. num VARCHAR(10) NOT NULL UNIQUE,
  6. birthday DATE NOT NULL );
  7. INSERT INTO Animal VALUES(1001,'花小猫',2,'10001','2021-1-2');
  8. INSERT INTO Animal VALUES(NULL,'壮小狗',3,'10002','2020-11-22');
  9. INSERT INTO Animal VALUES(NULL,'皮小鼠',1,'10003','2022-3-18');

二、封装Animal类

  1. package com.bdqn.animal;
  2. import java.sql.Date;
  3. public class Animal {
  4. /*
  5. aid INT(10) PRIMARY KEY AUTO_INCREMENT,
  6. `name` VARCHAR(10) NOT NULL,
  7. age INT(2),
  8. num VARCHAR(10) NOT NULL UNIQUE,
  9. birthday DATE NOT NULL );
  10. */
  11. private int aid;
  12. private String name;
  13. private int age;
  14. private String num;
  15. private Date birthday;
  16. public Animal() {
  17. }
  18. public Animal(int aid, String name, int age, String num, Date birthday) {
  19. this.aid = aid;
  20. this.name = name;
  21. this.age = age;
  22. this.num = num;
  23. this.birthday = birthday;
  24. }
  25. /**
  26. * 获取
  27. * @return aid
  28. */
  29. public int getAid() {
  30. return aid;
  31. }
  32. /**
  33. * 设置
  34. * @param aid
  35. */
  36. public void setAid(int aid) {
  37. this.aid = aid;
  38. }
  39. /**
  40. * 获取
  41. * @return name
  42. */
  43. public String getName() {
  44. return name;
  45. }
  46. /**
  47. * 设置
  48. * @param name
  49. */
  50. public void setName(String name) {
  51. this.name = name;
  52. }
  53. /**
  54. * 获取
  55. * @return age
  56. */
  57. public int getAge() {
  58. return age;
  59. }
  60. /**
  61. * 设置
  62. * @param age
  63. */
  64. public void setAge(int age) {
  65. this.age = age;
  66. }
  67. /**
  68. * 获取
  69. * @return num
  70. */
  71. public String getNum() {
  72. return num;
  73. }
  74. /**
  75. * 设置
  76. * @param num
  77. */
  78. public void setNum(String num) {
  79. this.num = num;
  80. }
  81. /**
  82. * 获取
  83. * @return birthday
  84. */
  85. public Date getBirthday() {
  86. return birthday;
  87. }
  88. /**
  89. * 设置
  90. * @param birthday
  91. */
  92. public void setBirthday(Date birthday) {
  93. this.birthday = birthday;
  94. }
  95. public String toString() {
  96. return "Animal{aid = " + aid + ", name = " + name + ", age = " + age + ", num = " + num + ", birthday = " + birthday + "}";
  97. }
  98. }

三、增

  1. package com.bdqn.animal;
  2. import java.sql.*;
  3. import java.util.Calendar;
  4. public class Insert {
  5. public static void main(String[] args) {
  6. // 1.加载驱动
  7. try {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. } catch (ClassNotFoundException e) {
  10. throw new RuntimeException(e);
  11. }
  12. // 2.准备工作
  13. String url = "jdbc:mysql://127.0.0.1:3306/jdbcdatabase";//localhost也可写成127.0.0.1,jdbcdatabase为数据库名
  14. String user = "root";//登录数据库系统账号
  15. String password = "123456";//登录数据库密码
  16. // animal对象需要一个SQL的日期
  17. Calendar calendar = Calendar.getInstance();
  18. calendar.set(2021, Calendar.APRIL,5);
  19. // sql类型的Date()需要参数为long的时间戳
  20. long timeInMillis = calendar.getTimeInMillis();
  21. // 创建Animal对象
  22. Animal animal = new Animal(1004, "花小猪", 2, "10004",
  23. new Date(timeInMillis));
  24. // 编写SQL语句
  25. String sql = "insert into Animal " +
  26. "values(null,'"+animal.getName()+"',"+animal.getAge()+",'"+animal.getNum()+"','"+animal.getBirthday()+"');";
  27. // 使用try-with 自动关闭资源
  28. try (
  29. // 3.获取连接对象
  30. Connection connection = DriverManager.getConnection(url, user, password);
  31. // 4.获取发送对象
  32. Statement statement = connection.createStatement()) {
  33. // 5.执行SQL
  34. int result = statement.executeUpdate(sql);
  35. // 6.接收反馈信息
  36. System.out.println(result != 0 ? "插入成功" : "插入失败");
  37. } catch (SQLException e) {
  38. throw new RuntimeException(e);
  39. }
  40. }
  41. }

四、删

  1. package com.bdqn.animal;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. public class Delete {
  7. public static void main(String[] args) {
  8. // 1.加载驱动
  9. try {
  10. Class.forName("com.mysql.jdbc.Driver");
  11. } catch (ClassNotFoundException e) {
  12. throw new RuntimeException(e);
  13. }
  14. // 2.准备数据
  15. String url = "jdbc:mysql://127.0.0.1:3306/jdbcdatabase";
  16. String user = "root";
  17. String password = "123456";
  18. String sql = "delete from animal where name = '花小猪';";
  19. try (
  20. // 3.获取连接对象
  21. Connection connection = DriverManager.getConnection(url, user, password);
  22. // 4.获取发送对象
  23. Statement statement = connection.createStatement()) {
  24. // 5.执行SQL
  25. int result = statement.executeUpdate(sql);
  26. // 6.接收反馈信息
  27. System.out.println(result != 0 ? "删除成功" : "删除失败");
  28. } catch (SQLException e) {
  29. throw new RuntimeException(e);
  30. }
  31. }
  32. }

五、改

  1. package com.bdqn.animal;
  2. import java.sql.*;
  3. public class Update {
  4. public static void main(String[] args) {
  5. // 1.加载驱动
  6. try {
  7. Class.forName("com.mysql.jdbc.Driver");
  8. } catch (ClassNotFoundException e) {
  9. throw new RuntimeException(e);
  10. }
  11. // 2.准备工作
  12. String url = "jdbc:mysql://127.0.0.1:3306/jdbcdatabase";
  13. String user = "root";
  14. String password = "123456";
  15. Animal animal = new Animal();
  16. animal.setBirthday(new Date(121,2,4));
  17. String sql = "update animal set `birthday` = '"+animal.getBirthday()+"' where name='花小猫'";
  18. try (
  19. // 3.获取连接对象
  20. Connection connection = DriverManager.getConnection(url, user, password);
  21. // 4.获取发送对象
  22. Statement statement = connection.createStatement()) {
  23. // 5.执行SQL
  24. int result = statement.executeUpdate(sql);
  25. // 6.接收反馈信息
  26. System.out.println(result != 0 ? "修改成功" : "修改失败");
  27. } catch (SQLException e) {
  28. throw new RuntimeException(e);
  29. }
  30. }
  31. }

六、查

  1. package com.bdqn.animal;
  2. import java.sql.*;
  3. import java.util.ArrayList;
  4. public class SelectAll {
  5. public static void main(String[] args) {
  6. // 1.加载驱动
  7. try {
  8. Class.forName("com.mysql.jdbc.Driver");
  9. } catch (ClassNotFoundException e) {
  10. throw new RuntimeException(e);
  11. }
  12. // 2.准备数据
  13. String url = "jdbc:mysql://127.0.0.1:3306/jdbcdatabase";
  14. String user = "root";
  15. String password = "123456";
  16. String sql = "select * from animal";
  17. try(
  18. // 3.获取连接对象
  19. Connection connection = DriverManager.getConnection(url, user, password);
  20. // 4.获取发送对象
  21. Statement statement = connection.createStatement()
  22. ) {
  23. // 5.执行SQL
  24. ResultSet resultSet = statement.executeQuery(sql);
  25. // 6.获取集合中的一条记录
  26. // 创建ArrayList集合 存放Animal对象
  27. ArrayList<Animal> animalArrayList = new ArrayList<>();
  28. while (resultSet.next()){
  29. // 按索引获取,从1开始 也可以使用使用字段名如getInt(aid)
  30. int aid = resultSet.getInt(1);
  31. String name = resultSet.getString(2);
  32. int age = resultSet.getInt(3);
  33. String num = resultSet.getString(4);
  34. Date birthday = resultSet.getDate(5);
  35. // 创建Animal对象
  36. Animal animal = new Animal(aid, name, age, num, birthday);
  37. animalArrayList.add(animal);
  38. }
  39. // 7.遍历ArrayList集合
  40. for(Animal a : animalArrayList)
  41. System.out.println(a);
  42. } catch (SQLException e) {
  43. throw new RuntimeException(e);
  44. }
  45. }
  46. }

七、细节过程都在注释里

发表评论

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

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

相关阅读

    相关 MySQL 增删

    MySQL 增删改查 虽然经常对数据库进行操作,但有时候一些sql语句还是会忘记,因此总结一下,方便记忆。 一 对库操作 1 创建数据库 > create dat