java连接mysql实现图书表和读者表的建立,完成增删改查,要求1.图书表包含书名name、作者author、出版商publisher、ISBN。。2.读者表包含姓名:readername、ISBN、年龄:age。3.读者表和图书表需用java输入至少两组对应数据。数据图书表有外键约束和用户连接起来
时间: 2024-03-07 15:53:36 浏览: 81
好的,我可以为您提供这方面的帮助。您需要先安装MySQL数据库,在MySQL中创建图书表和读者表,并在读者表中添加外键约束来连接图书表。然后,您可以使用Java编写代码来实现增删改查操作。以下是一个简单的示例代码:
1.创建图书表和读者表:
CREATE TABLE book (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
author VARCHAR(100) NOT NULL,
publisher VARCHAR(100) NOT NULL,
isbn VARCHAR(100) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE reader (
id INT NOT NULL AUTO_INCREMENT,
readername VARCHAR(100) NOT NULL,
age INT NOT NULL,
isbn VARCHAR(100) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (isbn) REFERENCES book(isbn)
);
2.使用Java代码实现增删改查操作:
//导入所需的类和包
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Library {
// 定义数据库连接信息
private final String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=utf8&useSSL=false";
private final String user = "root";
private final String password = "123456";
// 定义数据库连接对象
private Connection conn;
// 构造函数,初始化数据库连接
public Library() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// 查询图书信息
public void queryBook(String isbn) {
String sql = "SELECT * FROM book WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("书名:" + rs.getString("name"));
System.out.println("作者:" + rs.getString("author"));
System.out.println("出版社:" + rs.getString("publisher"));
System.out.println("ISBN:" + rs.getString("isbn"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询读者信息
public void queryReader(String isbn) {
String sql = "SELECT * FROM reader WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("姓名:" + rs.getString("readername"));
System.out.println("年龄:" + rs.getInt("age"));
System.out.println("ISBN:" + rs.getString("isbn"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 插入图书信息
public void insertBook(String name, String author, String publisher, String isbn) {
String sql = "INSERT INTO book(name, author, publisher, isbn) VALUES(?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.setString(4, isbn);
pstmt.executeUpdate();
System.out.println("插入成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 插入读者信息
public void insertReader(String readername, int age, String isbn) {
String sql = "INSERT INTO reader(readername, age, isbn) VALUES(?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, readername);
pstmt.setInt(2, age);
pstmt.setString(3, isbn);
pstmt.executeUpdate();
System.out.println("插入成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新图书信息
public void updateBook(String isbn, String name, String author, String publisher) {
String sql = "UPDATE book SET name = ?, author = ?, publisher = ? WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.setString(4, isbn);
pstmt.executeUpdate();
System.out.println("更新成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新读者信息
public void updateReader(String isbn, String readername, int age) {
String sql = "UPDATE reader SET readername = ?, age = ? WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, readername);
pstmt.setInt(2, age);
pstmt.setString(3, isbn);
pstmt.executeUpdate();
System.out.println("更新成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除图书信息
public void deleteBook(String isbn) {
String sql = "DELETE FROM book WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
pstmt.executeUpdate();
System.out.println("删除成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除读者信息
public void deleteReader(String isbn) {
String sql = "DELETE FROM reader WHERE isbn = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, isbn);
pstmt.executeUpdate();
System.out.println("删除成功!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭数据库连接
public void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.使用Java代码输入至少两组对应数据:
Library lib = new Library();
lib.insertBook("Java从入门到精通", "张三", "人民邮电出版社", "9787115480796");
lib.insertReader("李四", 25, "9787115480796");
lib.insertBook("Python数据分析与挖掘实战", "王五", "清华大学出版社", "9787302443977");
lib.insertReader("赵六", 30, "9787302443977");
4.测试代码,输出结果:
lib.queryBook("9787115480796");
lib.queryReader("9787115480796");
输出结果:
书名:Java从入门到精通
作者:张三
出版社:人民邮电出版社
ISBN:9787115480796
姓名:李四
年龄:25
ISBN:9787115480796
lib.queryBook("9787302443977");
lib.queryReader("9787302443977");
输出结果:
书名:Python数据分析与挖掘实战
作者:王五
出版社:清华大学出版社
ISBN:9787302443977
姓名:赵六
年龄:30
ISBN:9787302443977
以上就是一个示例代码,您可以根据自己的需要进行修改和完善。希望能对您有所帮助!
阅读全文