图书管理系统用Java写
时间: 2024-02-10 21:15:33 浏览: 23
好的,这里提供一个基于Java Swing和MySQL数据库的简单图书管理系统示例代码,包含添加、删除、修改和查询图书的功能:
```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class BookManagementSystem extends JFrame {
private JTable table;
private JTextField idField;
private JTextField titleField;
private JTextField authorField;
private JTextField yearField;
private JButton addButton;
private JButton deleteButton;
private JButton updateButton;
private JButton searchButton;
private Connection conn;
public BookManagementSystem() {
setTitle("图书管理系统");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/library";
String user = "root";
String password = "password";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "连接数据库失败!");
}
// 创建UI界面
JPanel panel = new JPanel();
add(panel);
JLabel idLabel = new JLabel("ID:");
panel.add(idLabel);
idField = new JTextField(10);
panel.add(idField);
JLabel titleLabel = new JLabel("Title:");
panel.add(titleLabel);
titleField = new JTextField(20);
panel.add(titleField);
JLabel authorLabel = new JLabel("Author:");
panel.add(authorLabel);
authorField = new JTextField(10);
panel.add(authorField);
JLabel yearLabel = new JLabel("Year:");
panel.add(yearLabel);
yearField = new JTextField(4);
panel.add(yearField);
addButton = new JButton("添加");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String title = titleField.getText();
String author = authorField.getText();
int year = Integer.parseInt(yearField.getText());
addBook(title, author, year);
refreshTable();
JOptionPane.showMessageDialog(BookManagementSystem.this, "添加成功!");
}
});
panel.add(addButton);
deleteButton = new JButton("删除");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
removeBook(id);
refreshTable();
JOptionPane.showMessageDialog(BookManagementSystem.this, "删除成功!");
}
});
panel.add(deleteButton);
updateButton = new JButton("修改");
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int id = Integer.parseInt(idField.getText());
String title = titleField.getText();
String author = authorField.getText();
int year = Integer.parseInt(yearField.getText());
updateBook(id, title, author, year);
refreshTable();
JOptionPane.showMessageDialog(BookManagementSystem.this, "修改成功!");
}
});
panel.add(updateButton);
searchButton = new JButton("查询");
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String title = titleField.getText();
String author = authorField.getText();
int year = 0;
if (!yearField.getText().isEmpty()) {
year = Integer.parseInt(yearField.getText());
}
searchBooks(title, author, year);
}
});
panel.add(searchButton);
// 创建表格
table = new JTable();
JScrollPane tablePane = new JScrollPane(table);
tablePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
panel.add(tablePane);
// 刷新表格
refreshTable();
}
// 添加图书
private void addBook(String title, String author, int year) {
try {
PreparedStatement stmt = conn.prepareStatement("INSERT INTO books (title, author, year) VALUES (?, ?, ?)");
stmt.setString(1, title);
stmt.setString(2, author);
stmt.setInt(3, year);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "添加失败!");
}
}
// 删除图书
private void removeBook(int id) {
try {
PreparedStatement stmt = conn.prepareStatement("DELETE FROM books WHERE id=?");
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "删除失败!");
}
}
// 修改图书
private void updateBook(int id, String title, String author, int year) {
try {
PreparedStatement stmt = conn.prepareStatement("UPDATE books SET title=?, author=?, year=? WHERE id=?");
stmt.setString(1, title);
stmt.setString(2, author);
stmt.setInt(3, year);
stmt.setInt(4, id);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "修改失败!");
}
}
// 查询图书
private void searchBooks(String title, String author, int year) {
try {
String query = "SELECT * FROM books WHERE 1=1";
if (!title.isEmpty()) {
query += " AND title LIKE '%" + title + "%'";
}
if (!author.isEmpty()) {
query += " AND author LIKE '%" + author + "%'";
}
if (year != 0) {
query += " AND year=" + year;
}
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "查询失败!");
}
}
// 刷新表格
private void refreshTable() {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM books");
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "刷新表格失败!");
}
}
public static void main(String[] args) {
BookManagementSystem system = new BookManagementSystem();
system.setVisible(true);
}
}
```
这个系统主要使用了Java Swing UI库来创建UI界面,使用了JTable来显示图书数据,使用了MySQL数据库来存储图书信息。可以根据需要进行修改和扩展。