完整代码地址
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;