Java编写图形用户界面的图书借阅系统代码
时间: 2023-10-16 21:10:42 浏览: 97
java图书借阅代码
以下是一个简单的Java编写图形用户界面的图书借阅系统的代码示例,仅供参考:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class BookBorrowSystem extends JFrame implements ActionListener {
private JLabel titleLabel, bookNameLabel, authorLabel, publisherLabel;
private JTextField bookNameField, authorField, publisherField;
private JButton searchButton, borrowButton, returnButton;
private JTextArea resultArea;
private Connection conn = null;
private PreparedStatement stmt = null;
public BookBorrowSystem() {
super("图书借阅系统");
// 设置界面布局
Container c = getContentPane();
c.setLayout(new BorderLayout());
// 添加标题
JPanel titlePanel = new JPanel();
titleLabel = new JLabel("图书借阅系统");
titleLabel.setFont(new Font("宋体", Font.BOLD, 24));
titlePanel.add(titleLabel);
c.add(titlePanel, BorderLayout.NORTH);
// 添加图书查询区域
JPanel searchPanel = new JPanel(new GridLayout(2, 4));
bookNameLabel = new JLabel("书名");
authorLabel = new JLabel("作者");
publisherLabel = new JLabel("出版社");
bookNameField = new JTextField(10);
authorField = new JTextField(10);
publisherField = new JTextField(10);
searchButton = new JButton("查询");
searchButton.addActionListener(this);
searchPanel.add(bookNameLabel);
searchPanel.add(bookNameField);
searchPanel.add(authorLabel);
searchPanel.add(authorField);
searchPanel.add(publisherLabel);
searchPanel.add(publisherField);
searchPanel.add(searchButton);
c.add(searchPanel, BorderLayout.CENTER);
// 添加借阅和归还按钮
JPanel buttonPanel = new JPanel();
borrowButton = new JButton("借阅");
borrowButton.addActionListener(this);
returnButton = new JButton("归还");
returnButton.addActionListener(this);
buttonPanel.add(borrowButton);
buttonPanel.add(returnButton);
c.add(buttonPanel, BorderLayout.SOUTH);
// 添加查询结果显示区域
JPanel resultPanel = new JPanel(new BorderLayout());
resultArea = new JTextArea();
resultArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(resultArea);
resultPanel.add(scrollPane);
c.add(resultPanel, BorderLayout.EAST);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book", "root", "123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 设置窗口大小和可见性
setPreferredSize(new Dimension(800, 600));
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
// 查询图书
String bookName = bookNameField.getText();
String author = authorField.getText();
String publisher = publisherField.getText();
try {
String sql = "select * from book where book_name like ? and author like ? and publisher like ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, "%" + bookName + "%");
stmt.setString(2, "%" + author + "%");
stmt.setString(3, "%" + publisher + "%");
ResultSet rs = stmt.executeQuery();
resultArea.setText("");
while (rs.next()) {
String bookId = rs.getString("book_id");
String bookNameResult = rs.getString("book_name");
String authorResult = rs.getString("author");
String publisherResult = rs.getString("publisher");
int amount = rs.getInt("amount");
int borrowed = rs.getInt("borrowed");
resultArea.append("图书编号:" + bookId + "\n");
resultArea.append("书名:" + bookNameResult + "\n");
resultArea.append("作者:" + authorResult + "\n");
resultArea.append("出版社:" + publisherResult + "\n");
resultArea.append("总数:" + amount + "\n");
resultArea.append("已借出:" + borrowed + "\n");
resultArea.append("--------------------\n");
}
rs.close();
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == borrowButton) {
// 借阅图书
String bookId = JOptionPane.showInputDialog(this, "请输入图书编号", "借阅", JOptionPane.PLAIN_MESSAGE);
if (bookId != null) {
try {
String sql = "select * from book where book_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, bookId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int amount = rs.getInt("amount");
int borrowed = rs.getInt("borrowed");
if (borrowed < amount) {
borrowed++;
sql = "update book set borrowed = ? where book_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, borrowed);
stmt.setString(2, bookId);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "借阅成功", "借阅", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "该图书已全部借出", "借阅", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "没有该图书", "借阅", JOptionPane.WARNING_MESSAGE);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == returnButton) {
// 归还图书
String bookId = JOptionPane.showInputDialog(this, "请输入图书编号", "归还", JOptionPane.PLAIN_MESSAGE);
if (bookId != null) {
try {
String sql = "select * from book where book_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, bookId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
int borrowed = rs.getInt("borrowed");
if (borrowed > 0) {
borrowed--;
sql = "update book set borrowed = ? where book_id = ?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, borrowed);
stmt.setString(2, bookId);
stmt.executeUpdate();
JOptionPane.showMessageDialog(this, "归还成功", "归还", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "该图书没有被借出", "归还", JOptionPane.WARNING_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(this, "没有该图书", "归还", JOptionPane.WARNING_MESSAGE);
}
rs.close();
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
new BookBorrowSystem();
}
}
```
以上代码仅为示例,具体实现还需要根据实际需求进行修改和优化。
阅读全文