图书管理系统GUI界面连接数据库JAVA代码
时间: 2023-09-04 19:08:44 浏览: 120
java图书管理系统(GUI)图形界面版
5星 · 资源好评率100%
好的,以下是一个基本的Java图书管理系统GUI界面连接数据库的代码示例。这个示例使用了Java Swing作为GUI界面,使用了MySQL作为数据库。
```java
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
public class BookManagementSystem extends JFrame {
private JLabel titleLabel = new JLabel("图书管理系统");
private JLabel nameLabel = new JLabel("书名:");
private JLabel authorLabel = new JLabel("作者:");
private JLabel yearLabel = new JLabel("出版年份:");
private JTextField nameTextField = new JTextField(20);
private JTextField authorTextField = new JTextField(20);
private JTextField yearTextField = new JTextField(20);
private JButton addButton = new JButton("添加");
private JButton deleteButton = new JButton("删除");
private JButton updateButton = new JButton("更新");
private JButton searchButton = new JButton("搜索");
private DefaultTableModel tableModel = new DefaultTableModel();
private JTable table = new JTable(tableModel);
private Connection connection;
private PreparedStatement preparedStatement;
public BookManagementSystem() {
try {
// 连接MySQL数据库
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/book_management_system", "root", "");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
// 设置GUI界面
setTitle("图书管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(10, 10, 10, 10);
// 标题
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 2;
panel.add(titleLabel, constraints);
// 书名
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
panel.add(nameLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(nameTextField, constraints);
// 作者
constraints.gridx = 0;
constraints.gridy = 2;
panel.add(authorLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 2;
panel.add(authorTextField, constraints);
// 出版年份
constraints.gridx = 0;
constraints.gridy = 3;
panel.add(yearLabel, constraints);
constraints.gridx = 1;
constraints.gridy = 3;
panel.add(yearTextField, constraints);
// 操作按钮
constraints.gridx = 0;
constraints.gridy = 4;
panel.add(addButton, constraints);
constraints.gridx = 1;
constraints.gridy = 4;
panel.add(deleteButton, constraints);
constraints.gridx = 0;
constraints.gridy = 5;
panel.add(updateButton, constraints);
constraints.gridx = 1;
constraints.gridy = 5;
panel.add(searchButton, constraints);
// 表格
tableModel.addColumn("书名");
tableModel.addColumn("作者");
tableModel.addColumn("出版年份");
JScrollPane scrollPane = new JScrollPane(table);
constraints.gridx = 0;
constraints.gridy = 6;
constraints.gridwidth = 2;
panel.add(scrollPane, constraints);
add(panel);
// 添加按钮事件处理
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
String author = authorTextField.getText();
String year = yearTextField.getText();
try {
preparedStatement = connection.prepareStatement("INSERT INTO books (name, author, year) VALUES (?, ?, ?)");
preparedStatement.setString(1, name);
preparedStatement.setString(2, author);
preparedStatement.setString(3, year);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "添加成功");
refreshTable();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败");
}
}
});
// 删除按钮事件处理
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
String name = (String) table.getValueAt(selectedRow, 0);
try {
preparedStatement = connection.prepareStatement("DELETE FROM books WHERE name = ?");
preparedStatement.setString(1, name);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "删除成功");
refreshTable();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "删除失败");
}
} else {
JOptionPane.showMessageDialog(null, "请选择要删除的书籍");
}
}
});
// 更新按钮事件处理
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow != -1) {
String name = (String) table.getValueAt(selectedRow, 0);
String author = authorTextField.getText();
String year = yearTextField.getText();
try {
preparedStatement = connection.prepareStatement("UPDATE books SET author = ?, year = ? WHERE name = ?");
preparedStatement.setString(1, author);
preparedStatement.setString(2, year);
preparedStatement.setString(3, name);
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "更新成功");
refreshTable();
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "更新失败");
}
} else {
JOptionPane.showMessageDialog(null, "请选择要更新的书籍");
}
}
});
// 搜索按钮事件处理
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameTextField.getText();
String author = authorTextField.getText();
String year = yearTextField.getText();
try {
preparedStatement = connection.prepareStatement("SELECT * FROM books WHERE name LIKE ? AND author LIKE ? AND year LIKE ?");
preparedStatement.setString(1, "%" + name + "%");
preparedStatement.setString(2, "%" + author + "%");
preparedStatement.setString(3, "%" + year + "%");
ResultSet resultSet = preparedStatement.executeQuery();
tableModel.setRowCount(0);
while (resultSet.next()) {
String bookName = resultSet.getString("name");
String bookAuthor = resultSet.getString("author");
String bookYear = resultSet.getString("year");
tableModel.addRow(new Object[] {bookName, bookAuthor, bookYear});
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "搜索失败");
}
}
});
// 刷新表格
refreshTable();
}
// 刷新表格
private void refreshTable() {
try {
preparedStatement = connection.prepareStatement("SELECT * FROM books");
ResultSet resultSet = preparedStatement.executeQuery();
tableModel.setRowCount(0);
while (resultSet.next()) {
String name = resultSet.getString("name");
String author = resultSet.getString("author");
String year = resultSet.getString("year");
tableModel.addRow(new Object[] {name, author, year});
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new BookManagementSystem();
}
}
```
这个代码示例包含了一个基本的图书管理系统GUI界面,并且连接了MySQL数据库。你可以根据你的需求进行修改和扩展。注意,你需要在你的电脑上安装MySQL数据库,并且创建一个名为"book_management_system"的数据库。同时,你需要在代码中修改连接数据库的用户名和密码。
阅读全文