完整代碼地址
1.運行效果圖


2.主要代碼
2.1.連接數據庫
package com.my.homework.utils;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class JDBCUtils {public static Connection getConnection() throws Exception {//1.注冊驅動Class.forName("com.mysql.cj.jdbc.Driver");//全路徑//2.獲取數據庫連接String url="jdbc:mysql://localhost:3306/library-management?useUnicode=true&useSSL=false&characterEncoding=gbk&serverTimezone=Asia/Shanghai" ;Connection c = DriverManager.getConnection(url,"root","root");//3.返回給調用者return c;}
}
2.2.實體類
package com.my.homework.entity;public class Book {private int id;private String name;private String author;private String isBorrowed;private String type;private String price;public Book() {}public Book(String name, String author, String type, String price) {this.name = name;this.author = author;this.type = type;this.price = price;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getIsBorrowed() {return isBorrowed;}public void setIsBorrowed(String isBorrowed) {this.isBorrowed = isBorrowed;}public String getType() {return type;}public void setType(String type) {this.type = type;}public String getPrice() {return price;