用java语言写一个图书管理系统,包括设计操作主界面; (2)使用数据库进行数据存放; (3)能够实现对数据的增删改查等操作代码
时间: 2024-03-21 16:38:43 浏览: 96
非常感谢您的问题,根据您的需求,我为您提供以下Java代码示例,用于实现一个基本的图书管理系统,包括设计用户界面、连接MySQL数据库并实现对数据的增删改查等操作:
1. 用户界面设计
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookManagementSystemUI extends JFrame {
private JLabel titleLabel;
private JLabel idLabel;
private JLabel nameLabel;
private JLabel authorLabel;
private JLabel publisherLabel;
private JLabel dateLabel;
private JTextField idTextField;
private JTextField nameTextField;
private JTextField authorTextField;
private JTextField publisherTextField;
private JTextField dateTextField;
private JButton addButton;
private JButton updateButton;
private JButton deleteButton;
private JButton searchButton;
private JTable bookTable;
private JScrollPane scrollPane;
public BookManagementSystemUI() {
setTitle("图书管理系统");
setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel titlePanel = new JPanel();
titleLabel = new JLabel("图书管理系统");
titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
titlePanel.add(titleLabel);
JPanel inputPanel = new JPanel(new GridLayout(5, 2));
idLabel = new JLabel("图书ID:");
idTextField = new JTextField();
nameLabel = new JLabel("书名:");
nameTextField = new JTextField();
authorLabel = new JLabel("作者:");
authorTextField = new JTextField();
publisherLabel = new JLabel("出版社:");
publisherTextField = new JTextField();
dateLabel = new JLabel("出版日期:");
dateTextField = new JTextField();
inputPanel.add(idLabel);
inputPanel.add(idTextField);
inputPanel.add(nameLabel);
inputPanel.add(nameTextField);
inputPanel.add(authorLabel);
inputPanel.add(authorTextField);
inputPanel.add(publisherLabel);
inputPanel.add(publisherTextField);
inputPanel.add(dateLabel);
inputPanel.add(dateTextField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 4));
addButton = new JButton("添加");
updateButton = new JButton("修改");
deleteButton = new JButton("删除");
searchButton = new JButton("搜索");
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(searchButton);
JPanel tablePanel = new JPanel(new BorderLayout());
bookTable = new JTable();
scrollPane = new JScrollPane(bookTable);
tablePanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(titlePanel, BorderLayout.NORTH);
mainPanel.add(inputPanel, BorderLayout.WEST);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(tablePanel, BorderLayout.CENTER);
add(mainPanel);
setVisible(true);
}
public String getIdTextField() {
return idTextField.getText();
}
public String getNameTextField() {
return nameTextField.getText();
}
public String getAuthorTextField() {
return authorTextField.getText();
}
public String getPublisherTextField() {
return publisherTextField.getText();
}
public String getDateTextField() {
return dateTextField.getText();
}
public void setIdTextField(String id) {
idTextField.setText(id);
}
public void setNameTextField(String name) {
nameTextField.setText(name);
}
public void setAuthorTextField(String author) {
authorTextField.setText(author);
}
public void setPublisherTextField(String publisher) {
publisherTextField.setText(publisher);
}
public void setDateTextField(String date) {
dateTextField.setText(date);
}
public void setBookTable(Object[][] data, Object[] columnNames) {
bookTable.setModel(new javax.swing.table.DefaultTableModel(data, columnNames));
}
public void addAddButtonListener(ActionListener listener) {
addButton.addActionListener(listener);
}
public void addUpdateButtonListener(ActionListener listener) {
updateButton.addActionListener(listener);
}
public void addDeleteButtonListener(ActionListener listener) {
deleteButton.addActionListener(listener);
}
public void addSearchButtonListener(ActionListener listener) {
searchButton.addActionListener(listener);
}
}
```
2. 数据库连接和数据操作
```
import java.sql.*;
public class BookManagementSystem {
private Connection conn;
public BookManagementSystem() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bookdb";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Object[][] getBookList() {
Object[][] data = null;
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM book");
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
data = new Object[rowCount][5];
int i = 0;
while (rs.next()) {
data[i][0] = rs.getString("id");
data[i][1] = rs.getString("name");
data[i][2] = rs.getString("author");
data[i][3] = rs.getString("publisher");
data[i][4] = rs.getString("date");
i++;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
}
public void addBook(String id, String name, String author, String publisher, String date) {
try {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO book VALUES (?, ?, ?, ?, ?)");
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, author);
pstmt.setString(4, publisher);
pstmt.setString(5, date);
pstmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void updateBook(String id, String name, String author, String publisher, String date) {
try {
PreparedStatement pstmt = conn.prepareStatement("UPDATE book SET name=?,author=?,publisher=?,date=? WHERE id=?");
pstmt.setString(1, name);
pstmt.setString(2, author);
pstmt.setString(3, publisher);
pstmt.setString(4, date);
pstmt.setString(5, id);
pstmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void deleteBook(String id) {
try {
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM book WHERE id=?");
pstmt.setString(1, id);
pstmt.executeUpdate();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public Object[][] searchBook(String keyword) {
Object[][] data = null;
try {
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM book WHERE name LIKE ? OR author LIKE ? OR publisher LIKE ?");
pstmt.setString(1, "%" + keyword + "%");
pstmt.setString(2, "%" + keyword + "%");
pstmt.setString(3, "%" + keyword + "%");
ResultSet rs = pstmt.executeQuery();
rs.last();
int rowCount = rs.getRow();
rs.beforeFirst();
data = new Object[rowCount][5];
int i = 0;
while (rs.next()) {
data[i][0] = rs.getString("id");
data[i][1] = rs.getString("name");
data[i][2] = rs.getString("author");
data[i][3] = rs.getString("publisher");
data[i][4] = rs.getString("date");
i++;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return data;
}
}
```
3. 控制器
```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookManagementSystemController {
private BookManagementSystemUI view;
private BookManagementSystem model;
public BookManagementSystemController(BookManagementSystemUI view, BookManagementSystem model) {
this.view = view;
this.model = model;
view.setBookTable(model.getBookList(), new Object[]{"图书ID", "书名", "作者", "出版社", "出版日期"});
view.addAddButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = view.getIdTextField();
String name = view.getNameTextField();
String author = view.getAuthorTextField();
String publisher = view.getPublisherTextField();
String date = view.getDateTextField();
model.addBook(id, name, author, publisher, date);
view.setBookTable(model.getBookList(), new Object[]{"图书ID", "书名", "作者", "出版社", "出版日期"});
}
});
view.addUpdateButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = view.getIdTextField();
String name = view.getNameTextField();
String author = view.getAuthorTextField();
String publisher = view.getPublisherTextField();
String date = view.getDateTextField();
model.updateBook(id, name, author, publisher, date);
view.setBookTable(model.getBookList(), new Object[]{"图书ID", "书名", "作者", "出版社", "出版日期"});
}
});
view.addDeleteButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String id = view.getIdTextField();
model.deleteBook(id);
view.setBookTable(model.getBookList(), new Object[]{"图书ID", "书名", "作者", "出版社", "出版日期"});
}
});
view.addSearchButtonListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String keyword = JOptionPane.showInputDialog("请输入关键字:");
view.setBookTable(model.searchBook(keyword), new Object[]{"图书ID", "书名", "作者", "出版社", "出版日期"});
}
});
}
}
```
在这个例子中,我们使用了Java语言和MySQL数据库实现了一个基本的图书管理系统,包括设计用户界面、连接MySQL数据库并实现对数据的增删改查等操作。我们使用了MVC(模型-视图-控制器)设计模式来组织代码,并使用了Java Swing库来实现用户界面。我们使用了JDBC连接到MySQL数据库,并使用SQL语句来执行数据的增删改查操作。
阅读全文