帮我用Java编写程序实现对图书信息表的增删改查操作。需求分析:现有图书信息表bookinfo(序号,图书编号,图书名称,作者,出版社,单价,出版日期,ISBN,库存数量)。写程序实现对该表的增删改查操作。
时间: 2024-12-28 15:40:33 浏览: 19
在Java中,我们可以使用JDBC (Java Database Connectivity) 来连接数据库并实现对图书信息表的操作。下面是一个简单的示例,展示了如何创建连接、执行SQL命令以及关闭连接:
```java
import java.sql.*;
public class BookInfoManager {
private static final String URL = "jdbc:mysql://localhost:3306/library";
private static final String USER = "username";
private static final String PASSWORD = "password";
// 创建数据库连接
private Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
// 关闭连接
private void closeConnection(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加新书
public void addBook(BookInfo book) {
String sql = "INSERT INTO bookinfo VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setInt(1, book.getSerialNo());
pstmt.setString(2, book.getBookId());
pstmt.setString(3, book.getTitle());
pstmt.setString(4, book.getAuthor());
pstmt.setString(5, book.getPublisher());
pstmt.setDouble(6, book.getPrice());
pstmt.setDate(7, book.getPublishDate());
pstmt.setString(8, book.getISBN());
pstmt.setInt(9, book.getStockQuantity());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除一本书
public void deleteBook(int serialNo) {
String sql = "DELETE FROM bookinfo WHERE SerialNo = ?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setInt(1, serialNo);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 更新书籍信息
public void updateBook(BookInfo updatedBook) {
String sql = "UPDATE bookinfo SET Title=?, Author=?, Publisher=?, Price=?, PublishDate=?, ISBN=?, StockQuantity=? WHERE SerialNo=?";
try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, updatedBook.getTitle());
pstmt.setString(2, updatedBook.getAuthor());
pstmt.setString(3, updatedBook.getPublisher());
pstmt.setDouble(4, updatedBook.getPrice());
pstmt.setDate(5, updatedBook.getPublishDate());
pstmt.setString(6, updatedBook.getISBN());
pstmt.setInt(7, updatedBook.getStockQuantity());
pstmt.setInt(8, updatedBook.getSerialNo());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 查询所有图书信息
public List<BookInfo> getAllBooks() {
List<BookInfo> books = new ArrayList<>();
try (Statement stmt = connection.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM bookinfo");
while (rs.next()) {
BookInfo book = new BookInfo(
rs.getInt("SerialNo"),
rs.getString("BookId"),
rs.getString("Title"),
rs.getString("Author"),
rs.getString("Publisher"),
rs.getDouble("Price"),
rs.getDate("PublishDate"),
rs.getString("ISBN"),
rs.getInt("StockQuantity")
);
books.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
// 使用try-with-resources自动关闭连接
private Connection connection;
{
try {
connection = getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 定义BookInfo类
class BookInfo {
// 省略getter和setter...
private int serialNo;
private String bookId;
// ...
}
```
这个例子中,`BookInfoManager`类包含了添加、删除、更新和查询图书信息的方法,每个方法都对应着一条SQL命令。在实际应用中,你需要处理异常,并根据实际情况调整数据库连接配置。
阅读全文