java制作简单图书系统代码
时间: 2023-11-21 18:03:57 浏览: 96
Java编写简单的图书系统
5星 · 资源好评率100%
以下是一个基于Java Swing的简单图书管理系统的代码示例:
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
public class BookManagementSystem extends JFrame {
private JPanel contentPane;
private JTextField searchField;
private JTable bookTable;
private DefaultTableModel model;
private Connection conn;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BookManagementSystem frame = new BookManagementSystem();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public BookManagementSystem() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 600, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel searchPanel = new JPanel();
contentPane.add(searchPanel, BorderLayout.NORTH);
searchField = new JTextField();
searchPanel.add(searchField);
searchField.setColumns(10);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String keyword = searchField.getText();
searchBook(keyword);
}
});
searchPanel.add(searchButton);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
bookTable = new JTable();
model = new DefaultTableModel(
new Object[][] {},
new String[] {"ID", "Title", "Author", "Publisher", "Year", "Price"}
);
bookTable.setModel(model);
scrollPane.setViewportView(bookTable);
try {
// Connect to database
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "");
System.out.println("Connected to database.");
// Load all books
loadBooks();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
private void loadBooks() {
try {
// Prepare SQL statement
String sql = "SELECT * FROM books";
PreparedStatement stmt = conn.prepareStatement(sql);
// Execute query
ResultSet rs = stmt.executeQuery();
// Clear table data
model.setRowCount(0);
// Fill in table data
while (rs.next()) {
Vector row = new Vector();
row.add(rs.getInt("id"));
row.add(rs.getString("title"));
row.add(rs.getString("author"));
row.add(rs.getString("publisher"));
row.add(rs.getInt("year"));
row.add(rs.getDouble("price"));
model.addRow(row);
}
// Close result set and statement
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void searchBook(String keyword) {
try {
// Prepare SQL statement
String sql = "SELECT * FROM books WHERE title LIKE ? OR author LIKE ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + keyword + "%");
stmt.setString(2, "%" + keyword + "%");
// Execute query
ResultSet rs = stmt.executeQuery();
// Clear table data
model.setRowCount(0);
// Fill in table data
while (rs.next()) {
Vector row = new Vector();
row.add(rs.getInt("id"));
row.add(rs.getString("title"));
row.add(rs.getString("author"));
row.add(rs.getString("publisher"));
row.add(rs.getInt("year"));
row.add(rs.getDouble("price"));
model.addRow(row);
}
// Close result set and statement
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
说明:
- 该程序使用MySQL数据库,需要在本地安装MySQL并创建名为`library`的数据库,并在其中创建名为`books`的表格,包含`id`、`title`、`author`、`publisher`、`year`和`price`六个字段。
- 程序使用JDBC连接数据库,使用了`com.mysql.jdbc.Driver`驱动。
- `loadBooks()`方法可以从数据库中读取所有图书信息,并填充到表格中。`searchBook(String keyword)`方法可以根据关键字搜索图书并在表格中显示结果。
- 程序中使用了Swing组件,包括`JFrame`、`JPanel`、`JTextField`、`JButton`、`JTable`、`DefaultTableModel`和`JScrollPane`等。
注意:该代码示例只是一个简单的示例,实际使用中需要根据需求进行修改和完善。
阅读全文