java代码 要求连接数据库library,有两个表,一个读者表(有账号,密码两个数据)要求有增查的功能。一个图书表(有书名,ISBN,作者 ,出版社四个数据)要求有增删改查的功能
时间: 2024-03-06 13:48:04 浏览: 56
好的,关于连接数据库library,我需要先了解一下你使用的是哪种数据库,比如MySQL、Oracle、SQLite等等。假设你使用的是MySQL数据库,那么可以使用Java的JDBC技术来连接数据库。以下是一个简单的连接MySQL数据库的示例代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySQLConnection {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
}
```
其中,URL是连接MySQL数据库的URL,可以根据实际情况修改;USER和PASSWORD是连接MySQL数据库的用户名和密码,也可以根据实际情况修改。
接下来,我们可以定义一个Reader类和一个Book类来表示读者和图书,然后使用JDBC技术实现增删改查的功能。下面是一个示例代码:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class Library {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 获取数据库连接
conn = MySQLConnection.getConnection();
// 创建读者表
stmt = conn.createStatement();
String createReaderTableSql = "CREATE TABLE IF NOT EXISTS reader (id INT PRIMARY KEY AUTO_INCREMENT, username VARCHAR(20), password VARCHAR(20))";
stmt.executeUpdate(createReaderTableSql);
// 创建图书表
stmt = conn.createStatement();
String createBookTableSql = "CREATE TABLE IF NOT EXISTS book (id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(50), isbn VARCHAR(20), author VARCHAR(50), publisher VARCHAR(50))";
stmt.executeUpdate(createBookTableSql);
// 插入读者数据
pstmt = conn.prepareStatement("INSERT INTO reader (username, password) VALUES (?, ?)");
pstmt.setString(1, "user1");
pstmt.setString(2, "password1");
pstmt.executeUpdate();
// 插入图书数据
pstmt = conn.prepareStatement("INSERT INTO book (title, isbn, author, publisher) VALUES (?, ?, ?, ?)");
pstmt.setString(1, "Java编程思想");
pstmt.setString(2, "978-7-121-21166-9");
pstmt.setString(3, "Bruce Eckel");
pstmt.setString(4, "机械工业出版社");
pstmt.executeUpdate();
// 查询读者数据
List<Reader> readers = new ArrayList<>();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM reader");
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
Reader reader = new Reader(id, username, password);
readers.add(reader);
}
// 查询图书数据
List<Book> books = new ArrayList<>();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM book");
while (rs.next()) {
int id = rs.getInt("id");
String title = rs.getString("title");
String isbn = rs.getString("isbn");
String author = rs.getString("author");
String publisher = rs.getString("publisher");
Book book = new Book(id, title, isbn, author, publisher);
books.add(book);
}
// 更新图书数据
pstmt = conn.prepareStatement("UPDATE book SET title = ? WHERE id = ?");
pstmt.setString(1, "Java核心技术");
pstmt.setInt(2, 1);
pstmt.executeUpdate();
// 删除图书数据
pstmt = conn.prepareStatement("DELETE FROM book WHERE id = ?");
pstmt.setInt(1, 2);
pstmt.executeUpdate();
// 输出查询结果
System.out.println("读者数据:");
for (Reader reader : readers) {
System.out.println(reader);
}
System.out.println("图书数据:");
for (Book book : books) {
System.out.println(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// 关闭连接
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
class Reader {
private int id;
private String username;
private String password;
public Reader(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "Reader{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
class Book {
private int id;
private String title;
private String isbn;
private String author;
private String publisher;
public Book(int id, String title, String isbn, String author, String publisher) {
this.id = id;
this.title = title;
this.isbn = isbn;
this.author = author;
this.publisher = publisher;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", isbn='" + isbn + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
'}';
}
}
```
在上面的代码中,我们首先创建了一个MySQLConnection类用来连接MySQL数据库。然后定义了一个Library类,其中包含了一些增删改查的功能。我们创建了两个表:reader表和book表,并插入了一些数据。然后查询了读者表和图书表的数据,并输出结果。接着,我们更新了图书表的数据,并删除了一条图书数据。最后,关闭了数据库连接。
总的来说,这段代码可以帮助你实现连接MySQL数据库,并实现读者表和图书表的增查改删功能。如果你使用的是其他数据库,可以根据实际情况修改代码。
阅读全文