一、实验目的
    1、掌握JAVA连接和操作MySQL数据库的方法;
    2、掌握程序调试方法;

    二、实验环境
    装有软件MySQL5.7或更高版本的PC电脑一台;

    三、实验步骤及实验要求
    按照下面的实验内容要求完成作业,将作业结果的每一步截图粘贴到word文档中即可。每一个实验都必须编写实验报告,要求如下:
    1、实验报告用word编写;
    2、word文件命名的格式统一要求:为以“杨健”同学19号为例,他所做的第4次实验的实验报告应该命令为:“DB实验4— 19号—杨健”(即格式是:实验序号—课内序号—姓名);课内序号现在是一个人一门课一个号,同一个人在不同课序号会不同,回头我会将课程名单发到群里,自己查阅你自己在本门课中的序号。
    3、实验报告用统一的封面,封面模板到时发给大家;
    4、报告中截取每题主要步骤结果的截图、实验结果截图
    5、实验报告最后要加上实验总结,总结部分必须写出自己的切身体会
    6、实验报告如有雷同、抄袭现象,后果自负;
    7、实验报告上交截止时间:上机后一周之内
    8、实验上交方式:由学委收集齐全后,统一交付老师:

    四、实验内容
    1、基于上述“简易图书管理系统”数据库,利用java语言实现如下功能:
    首先写四个实体类

    1. package org.example.bean;
    2. import java.util.Date;
    3. /**
    4. * 图书信息对象 book
    5. *
    6. * @author 翁修林
    7. * @date 2022-10-14
    8. */
    9. public class Book {
    10. private static final long serialVersionUID = 1L;
    11. /** 图书序号 */
    12. private String bkId;
    13. /** 书名 */
    14. private String bkName;
    15. /** 作者 */
    16. private String bkAuthor;
    17. /** 出版社 */
    18. private String bkPress;
    19. /** 价格 */
    20. private Double bkPrice;
    21. /** 图书状态 */
    22. private Integer bkStatus;
    23. public Book() {
    24. }
    25. public Book(String bkId, String bkName, String bkAuthor, String bkPress, Double bkPrice, Integer bkStatus) {
    26. this.bkId = bkId;
    27. this.bkName = bkName;
    28. this.bkAuthor = bkAuthor;
    29. this.bkPress = bkPress;
    30. this.bkPrice = bkPrice;
    31. this.bkStatus = bkStatus;
    32. }
    33. public String getBkId() {
    34. return bkId;
    35. }
    36. public void setBkId(String bkId) {
    37. this.bkId = bkId;
    38. }
    39. public String getBkName() {
    40. return bkName;
    41. }
    42. public void setBkName(String bkName) {
    43. this.bkName = bkName;
    44. }
    45. public String getBkAuthor() {
    46. return bkAuthor;
    47. }
    48. public void setBkAuthor(String bkAuthor) {
    49. this.bkAuthor = bkAuthor;
    50. }
    51. public String getBkPress() {
    52. return bkPress;
    53. }
    54. public void setBkPress(String bkPress) {
    55. this.bkPress = bkPress;
    56. }
    57. public Double getBkPrice() {
    58. return bkPrice;
    59. }
    60. public void setBkPrice(Double bkPrice) {
    61. this.bkPrice = bkPrice;
    62. }
    63. public Integer getBkStatus() {
    64. return bkStatus;
    65. }
    66. public void setBkStatus(Integer bkStatus) {
    67. this.bkStatus = bkStatus;
    68. }
    69. @Override
    70. public String toString() {
    71. return "Book{" +
    72. "bkId='" + bkId + '\'' +
    73. ", bkName='" + bkName + '\'' +
    74. ", bkAuthor='" + bkAuthor + '\'' +
    75. ", bkPress='" + bkPress + '\'' +
    76. ", bkPrice=" + bkPrice +
    77. ", bkStatus=" + bkStatus +
    78. '}';
    79. }
    80. }
    1. package org.example.bean;
    2. import java.util.Date;
    3. /**
    4. * 借阅信息对象 borrow
    5. *
    6. * @author 翁修林
    7. * @date 2022-10-14
    8. */
    9. public class Borrow {
    10. private static final long serialVersionUID = 1L;
    11. /** 读者序号 */
    12. private String rdId;
    13. /** 图书序号 */
    14. private String bkId;
    15. /**
    16. * 借书日期
    17. */
    18. private Date DateBorrow;
    19. /**
    20. * 应还日期
    21. */
    22. private Date DateLendPlan;
    23. /**
    24. * 实际还书日期
    25. */
    26. private Date DateLendAct;
    27. public Borrow() {
    28. }
    29. public Borrow(String rdId, String bkId, Date dateBorrow, Date dateLendPlan, Date dateLendAct) {
    30. this.rdId = rdId;
    31. this.bkId = bkId;
    32. DateBorrow = dateBorrow;
    33. DateLendPlan = dateLendPlan;
    34. DateLendAct = dateLendAct;
    35. }
    36. public String getRdId() {
    37. return rdId;
    38. }
    39. public void setRdId(String rdId) {
    40. this.rdId = rdId;
    41. }
    42. public String getBkId() {
    43. return bkId;
    44. }
    45. public void setBkId(String bkId) {
    46. this.bkId = bkId;
    47. }
    48. public Date getDateBorrow() {
    49. return DateBorrow;
    50. }
    51. public void setDateBorrow(Date dateBorrow) {
    52. DateBorrow = dateBorrow;
    53. }
    54. public Date getDateLendPlan() {
    55. return DateLendPlan;
    56. }
    57. public void setDateLendPlan(Date dateLendPlan) {
    58. DateLendPlan = dateLendPlan;
    59. }
    60. public Date getDateLendAct() {
    61. return DateLendAct;
    62. }
    63. public void setDateLendAct(Date dateLendAct) {
    64. DateLendAct = dateLendAct;
    65. }
    66. @Override
    67. public String toString() {
    68. return "Borrow{" +
    69. "rdId='" + rdId + '\'' +
    70. ", bkId='" + bkId + '\'' +
    71. ", DateBorrow=" + DateBorrow +
    72. ", DateLendPlan=" + DateLendPlan +
    73. ", DateLendAct=" + DateLendAct +
    74. '}';
    75. }
    76. }
    1. package org.example.bean;
    2. import java.util.Date;
    3. /**
    4. * 读者信息对象 reader
    5. *
    6. * @author 翁修林
    7. * @date 2022-10-14
    8. */
    9. public class Reader {
    10. private static final long serialVersionUID = 1L;
    11. /** 读者编号 */
    12. private String rdId;
    13. /** 读者类别 */
    14. private Long rdType;
    15. /** 读者姓名 */
    16. private String rdName;
    17. /** 单位代码 */
    18. private String rdDept;
    19. /**
    20. * QQ
    21. */
    22. private String rdQQ;
    23. /**
    24. * 借书数量
    25. */
    26. private Integer rdBorrowQty;
    27. public Reader() {
    28. }
    29. public Reader(String rdId, Long rdType, String rdName, String rdDept, String rdQQ, Integer rdBorrowQty) {
    30. this.rdId = rdId;
    31. this.rdType = rdType;
    32. this.rdName = rdName;
    33. this.rdDept = rdDept;
    34. this.rdQQ = rdQQ;
    35. this.rdBorrowQty = rdBorrowQty;
    36. }
    37. public String getRdId() {
    38. return rdId;
    39. }
    40. public void setRdId(String rdId) {
    41. this.rdId = rdId;
    42. }
    43. public Long getRdType() {
    44. return rdType;
    45. }
    46. public void setRdType(Long rdType) {
    47. this.rdType = rdType;
    48. }
    49. public String getRdName() {
    50. return rdName;
    51. }
    52. public void setRdName(String rdName) {
    53. this.rdName = rdName;
    54. }
    55. public String getRdDept() {
    56. return rdDept;
    57. }
    58. public void setRdDept(String rdDept) {
    59. this.rdDept = rdDept;
    60. }
    61. public String getRdQQ() {
    62. return rdQQ;
    63. }
    64. public void setRdQQ(String rdQQ) {
    65. this.rdQQ = rdQQ;
    66. }
    67. public Integer getRdBorrowQty() {
    68. return rdBorrowQty;
    69. }
    70. public void setRdBorrowQty(Integer rdBorrowQty) {
    71. this.rdBorrowQty = rdBorrowQty;
    72. }
    73. @Override
    74. public String toString() {
    75. return "Reader{" +
    76. "rdId='" + rdId + '\'' +
    77. ", rdType=" + rdType +
    78. ", rdName='" + rdName + '\'' +
    79. ", rdDept='" + rdDept + '\'' +
    80. ", rdQQ='" + rdQQ + '\'' +
    81. ", rdBorrowQty=" + rdBorrowQty +
    82. '}';
    83. }
    84. }
    1. package org.example.bean;
    2. /**
    3. * 读者类别对象 readertype
    4. *
    5. * @author 翁修林
    6. * @date 2022-10-14
    7. */
    8. public class Readertype {
    9. private static final long serialVersionUID = 1L;
    10. /** 读者类别 */
    11. private Integer rdtType;
    12. /** 读者类别名称 */
    13. private String rdtTypeName;
    14. /** 可借书天数 */
    15. private Integer rdtLendDay;
    16. private Integer canlendDay;
    17. public Readertype() {
    18. }
    19. public Readertype(Integer rdtType, String rdtTypeName, Integer rdtLendDay, Integer canlendDay) {
    20. this.rdtType = rdtType;
    21. this.rdtTypeName = rdtTypeName;
    22. this.rdtLendDay = rdtLendDay;
    23. this.canlendDay = canlendDay;
    24. }
    25. public Integer getRdtType() {
    26. return rdtType;
    27. }
    28. public void setRdtType(Integer rdtType) {
    29. this.rdtType = rdtType;
    30. }
    31. public String getRdtTypeName() {
    32. return rdtTypeName;
    33. }
    34. public void setRdtTypeName(String rdtTypeName) {
    35. this.rdtTypeName = rdtTypeName;
    36. }
    37. public Integer getRdtLendDay() {
    38. return rdtLendDay;
    39. }
    40. public void setRdtLendDay(Integer rdtLendDay) {
    41. this.rdtLendDay = rdtLendDay;
    42. }
    43. public Integer getCanlendDay() {
    44. return canlendDay;
    45. }
    46. public void setCanlendDay(Integer canlendDay) {
    47. this.canlendDay = canlendDay;
    48. }
    49. @Override
    50. public String toString() {
    51. return "Readertype{" +
    52. "rdtType=" + rdtType +
    53. ", rdtTypeName='" + rdtTypeName + '\'' +
    54. ", rdtLendDay=" + rdtLendDay +
    55. ", canlendDay=" + canlendDay +
    56. '}';
    57. }
    58. }

    写一个通用CRUD

    1. package org.example.base;
    2. import org.apache.commons.dbutils.QueryRunner;
    3. import org.apache.commons.dbutils.handlers.BeanHandler;
    4. import org.apache.commons.dbutils.handlers.BeanListHandler;
    5. import org.apache.commons.dbutils.handlers.ScalarHandler;
    6. import java.lang.reflect.ParameterizedType;
    7. import java.lang.reflect.Type;
    8. import java.sql.Connection;
    9. import java.sql.SQLException;
    10. import java.util.List;
    11. /**
    12. * 定义一个用来被继承的对数据库进行基本操作的Dao
    13. *
    14. * 引入 pom commons.dbutils
    15. *
    16. * @author HanYanBing
    17. *
    18. * @param <T>
    19. */
    20. public abstract class BaseDao<T> {
    21. private QueryRunner queryRunner = new QueryRunner();
    22. // 定义一个变量来接收泛型的类型
    23. private Class<T> type;
    24. // 获取T的Class对象,获取泛型的类型,泛型是在被子类继承时才确定
    25. public BaseDao() {
    26. // 获取子类的类型
    27. Class clazz = this.getClass();
    28. // 获取父类的类型
    29. // getGenericSuperclass()用来获取当前类的父类的类型
    30. // ParameterizedType表示的是带泛型的类型
    31. ParameterizedType parameterizedType = (ParameterizedType) clazz.getGenericSuperclass();
    32. // 获取具体的泛型类型 getActualTypeArguments获取具体的泛型的类型
    33. // 这个方法会返回一个Type的数组
    34. Type[] types = parameterizedType.getActualTypeArguments();
    35. // 获取具体的泛型的类型·
    36. this.type = (Class<T>) types[0];
    37. }
    38. /**
    39. * 通用的增删改操作
    40. *
    41. * @param sql
    42. * @param params
    43. * @return
    44. */
    45. public int update(Connection conn,String sql, Object... params) {
    46. int count = 0;
    47. try {
    48. count = queryRunner.update(conn, sql, params);
    49. } catch (SQLException e) {
    50. e.printStackTrace();
    51. }
    52. return count;
    53. }
    54. /**
    55. * 获取一个对象
    56. *
    57. * @param sql
    58. * @param params
    59. * @return
    60. */
    61. public T getBean(Connection conn,String sql, Object... params) {
    62. T t = null;
    63. try {
    64. t = queryRunner.query(conn, sql, new BeanHandler<T>(type), params);
    65. } catch (SQLException e) {
    66. e.printStackTrace();
    67. }
    68. return t;
    69. }
    70. /**
    71. * 获取所有对象
    72. *
    73. * @param sql
    74. * @param params
    75. * @return
    76. */
    77. public List<T> getBeanList(Connection conn,String sql, Object... params) {
    78. List<T> list = null;
    79. try {
    80. list = queryRunner.query(conn, sql, new BeanListHandler<T>(type), params);
    81. } catch (SQLException e) {
    82. e.printStackTrace();
    83. }
    84. return list;
    85. }
    86. /**
    87. * 获取一个但一值得方法,专门用来执行像 select count(*)...这样的sql语句
    88. *
    89. * @param sql
    90. * @param params
    91. * @return
    92. */
    93. public Object getValue(Connection conn,String sql, Object... params) {
    94. Object count = null;
    95. try {
    96. // 调用queryRunner的query方法获取一个单一的值
    97. count = queryRunner.query(conn, sql, new ScalarHandler<>(), params);
    98. } catch (SQLException e) {
    99. e.printStackTrace();
    100. }
    101. return count;
    102. }
    103. }

    写一个JDBC连接池

    1. package org.example.utils;
    2. import com.alibaba.druid.pool.DruidDataSourceFactory;
    3. import javax.sql.DataSource;
    4. import java.sql.Connection;
    5. import java.sql.ResultSet;
    6. import java.sql.SQLException;
    7. import java.sql.Statement;
    8. import java.util.Properties;
    9. /**
    10. * Druid连接池工具类,将来dao层调用
    11. */
    12. public class JDBCUtils {
    13. private static DataSource dataSource; //定义成员变量DataSource
    14. static {
    15. try {
    16. //加载配置文件
    17. Properties properties = new Properties();
    18. properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
    19. //获取DataSource
    20. dataSource = DruidDataSourceFactory.createDataSource(properties);
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. /**
    26. * 获取连接
    27. */
    28. public static Connection getConnection() throws SQLException {
    29. return dataSource.getConnection();
    30. }
    31. /**
    32. * 释放资源
    33. */
    34. public static void close(Statement statement,Connection connection) {
    35. close(null,statement,connection);
    36. }
    37. public static void close(ResultSet resultSet, Statement statement, Connection connection) {
    38. if (resultSet != null) {
    39. try {
    40. resultSet.close();
    41. } catch (SQLException e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. if (statement != null) {
    46. try {
    47. statement.close();
    48. } catch (SQLException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. if (connection != null) {
    53. try {
    54. connection.close();//归还连接
    55. }catch (SQLException e) {
    56. e.printStackTrace();
    57. }
    58. }
    59. }
    60. /**
    61. * 获取连接池方法
    62. */
    63. public static DataSource getDataSource() {
    64. return dataSource;
    65. }
    66. }

    (1)读者类别管理:添加读者类别、修改读者类别、删除读者类别;

    1. /**
    2. * 添加读者类别
    3. */
    4. @Test
    5. public void saveReadertypeTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Readertype readertype = new Readertype(5,"测试添加读者类别",20,50);
    11. readertypeDaoimpl.saveReadertype(conn,readertype);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    8-3.png

    1. /**
    2. * 修改读者类别
    3. */
    4. @Test
    5. public void updateReadertypeTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Readertype readertype = new Readertype(5,"测试修改读者类别",66666666,200);
    11. readertypeDaoimpl.updateReadertype(conn,readertype);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    8-4.png

    1. /**
    2. * 删除读者类别
    3. */
    4. @Test
    5. public void deleteReadertypeByIdTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. // Readertype readertype = new Readertype(5,"测试修改读者类别",66666666,200);
    11. readertypeDaoimpl.deleteReadertypeById(conn,"5");
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    8-5.png

    (2)图书管理:添加图书、修改图书、查找图书、删除图书;

    1. /**
    2. * 添加图书
    3. */
    4. @Test
    5. public void saveBookTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Book book = new Book("bk2017009","测试添加图书","翁修林","长江大学",100.0,1);
    11. bookDaoimpl.saveBook(conn,book);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    image.png

    1. /**
    2. * 修改图书
    3. */
    4. @Test
    5. public void updateBookTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Book book = new Book("bk2017009","测试修改图书","修改图书","修改图书",100.0,1);
    11. bookDaoimpl.updateBook(conn,book);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    image.png

    1. /**
    2. * 查找图书
    3. */
    4. @Test
    5. public void getBookByIdTest(){
    6. Book book =null;
    7. try {
    8. //1.获取连接的操作(
    9. conn = JDBCUtils.getConnection();
    10. //2.对数据表进行一系列CRUD操作
    11. book = bookDaoimpl.getBookById(conn,"bk2017009");
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. System.out.println(book);
    29. }

    8-1.png

    1. /**
    2. * 删除图书
    3. */
    4. @Test
    5. public void deleteBookByIdTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. bookDaoimpl.deleteBookById(conn,"bk2017009");
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. try {
    14. //回滚数据
    15. conn.rollback();
    16. } catch (SQLException e1) {
    17. e1.printStackTrace();
    18. }
    19. }finally{
    20. //3.关闭连接等操作
    21. try {
    22. conn.close();
    23. } catch (SQLException e) {
    24. throw new RuntimeException(e);
    25. }
    26. }
    27. }

    8-2.png

    (3)读者管理:添加读者、修改读者、删除读者;

    1. /**
    2. * 添加读者
    3. */
    4. public void AddReaderTest(){
    5. try {
    6. //1.获取连接的操作(
    7. conn = JDBCUtils.getConnection();
    8. //2.对数据表进行一系列CRUD操作
    9. Reader reader = new Reader("rd2017010",1L,"JDBC测试","计算机科学学院","123456",1);
    10. readerDaoimpl.saveReader(conn,reader);
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. try {
    14. //回滚数据
    15. conn.rollback();
    16. } catch (SQLException e1) {
    17. e1.printStackTrace();
    18. }
    19. }finally{
    20. //3.关闭连接等操作
    21. try {
    22. conn.close();
    23. } catch (SQLException e) {
    24. throw new RuntimeException(e);
    25. }
    26. }
    27. }
    28. }

    image.png

    1. /**
    2. * 修改读者
    3. */
    4. @Test
    5. public void updateTbReaderTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. Reader reader = new Reader("rd2017010",1L,"JDBC测试修改读者","计算机科学学院","123456",1);
    10. //2.对数据表进行一系列CRUD操作
    11. readerDaoimpl.updateReader(conn,reader);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. try {
    15. //回滚数据
    16. conn.rollback();
    17. } catch (SQLException e1) {
    18. e1.printStackTrace();
    19. }
    20. }finally{
    21. //3.关闭连接等操作
    22. try {
    23. conn.close();
    24. } catch (SQLException e) {
    25. throw new RuntimeException(e);
    26. }
    27. }
    28. }

    image.png

    1. /**
    2. * 删除读者
    3. */
    4. @Test
    5. public void deleteReaderByIdTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. readerDaoimpl.deleteReaderById(conn,"rd2017010");
    11. } catch (Exception e) {
    12. e.printStackTrace();
    13. try {
    14. //回滚数据
    15. conn.rollback();
    16. } catch (SQLException e1) {
    17. e1.printStackTrace();
    18. }
    19. }finally{
    20. //3.关闭连接等操作
    21. try {
    22. conn.close();
    23. } catch (SQLException e) {
    24. throw new RuntimeException(e);
    25. }
    26. }
    27. }

    image.png
    (4)借书、还书

    1. /**
    2. * 借书
    3. */
    4. @Test
    5. public void LendBookTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Book book = null;
    11. Reader reader = null;
    12. book = bookDaoimpl.getBookById(conn,"bk2017001");
    13. reader = readerDaoimpl.getReaderById(conn,"rd2017001");
    14. Date date = new Date();
    15. Borrow borrow = new Borrow(reader.getRdId(),book.getBkId(),date,date,date);
    16. if (book != null && reader != null){
    17. borrowDaoimpl.saveBorrow(conn,borrow);
    18. }
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. try {
    22. //回滚数据
    23. conn.rollback();
    24. } catch (SQLException e1) {
    25. e1.printStackTrace();
    26. }
    27. }finally{
    28. //3.关闭连接等操作
    29. try {
    30. conn.close();
    31. } catch (SQLException e) {
    32. throw new RuntimeException(e);
    33. }
    34. }
    35. }

    8-6.png

    1. /**
    2. * 还书
    3. */
    4. @Test
    5. public void huanshuTest(){
    6. try {
    7. //1.获取连接的操作(
    8. conn = JDBCUtils.getConnection();
    9. //2.对数据表进行一系列CRUD操作
    10. Book book = null;
    11. Reader reader = null;
    12. book = bookDaoimpl.getBookById(conn,"bk2017001");
    13. reader = readerDaoimpl.getReaderById(conn,"rd2017001");
    14. Date date = new Date();
    15. Borrow borrow = new Borrow(reader.getRdId(),book.getBkId(),date,date,date);
    16. if (book != null && reader != null){
    17. borrowDaoimpl.deleteBorrowById(conn,book.getBkId(),reader.getRdId());
    18. }
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. try {
    22. //回滚数据
    23. conn.rollback();
    24. } catch (SQLException e1) {
    25. e1.printStackTrace();
    26. }
    27. }finally{
    28. //3.关闭连接等操作
    29. try {
    30. conn.close();
    31. } catch (SQLException e) {
    32. throw new RuntimeException(e);
    33. }
    34. }
    35. }

    8-7.png