好的,下面是一个简单的图书馆管理系统的Java代码示例:import java.sql.*;import javax.swing.*;import java.awt.event.*;public class LibraryManagementSystem extends JFrame { private Connection connection; private Statement statement; private JTextField bookNameField, borrowerNameField, borrowDateField, returnDateField; private JButton addButton, deleteButton, updateButton, searchButton; public LibraryManagementSystem() { // 初始化界面组件 // ... // 创建数据库连接 try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); String url = "jdbc:sqlserver://localhost:1433;databaseName=your_database_name"; String username = "your_username"; String password = "your_password"; connection = DriverManager.getConnection(url, username, password); statement = connection.createStatement(); } catch (Exception e) { e.printStackTrace(); } // 添加按钮点击事件监听器 addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String bookName = bookNameField.getText(); String borrowerName = borrowerNameField.getText(); String borrowDate = borrowDateField.getText(); String returnDate = returnDateField.getText(); try { String sql = "INSERT INTO books (book_name, name, jie_data, huan_data) VALUES ('" + bookName + "', '" + borrowerName + "', '" + borrowDate + "', '" + returnDate + "')"; statement.executeUpdate(sql); JOptionPane.showMessageDialog(null, "添加成功"); } catch (SQLException ex) { ex.printStackTrace(); } } }); // 删除按钮点击事件监听器 deleteButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int bookId = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入要删除的图书ID:")); try { String sql = "DELETE FROM books WHERE id = " + bookId; statement.executeUpdate(sql); JOptionPane.showMessageDialog(null, "删除成功"); } catch (SQLException ex) { ex.printStackTrace(); } } }); // 更新按钮点击事件监听器 updateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int bookId = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入要更新的图书ID:")); String bookName = bookNameField.getText(); String borrowerName = borrowerNameField.getText(); String borrowDate = borrowDateField.getText(); String returnDate = returnDateField.getText(); try { String sql = "UPDATE books SET book_name = '" + bookName + "', name = '" + borrowerName + "', jie_data = '" + borrowDate + "', huan_data = '" + returnDate + "' WHERE id = " + bookId; statement.executeUpdate(sql); JOptionPane.showMessageDialog(null, "更新成功"); } catch (SQLException ex) { ex.printStackTrace(); } } }); // 查询按钮点击事件监听器 searchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int bookId = Integer.parseInt(JOptionPane.showInputDialog(null, "请输入要查询的图书ID:")); try { String sql = "SELECT * FROM books WHERE id = " + bookId; ResultSet resultSet = statement.executeQuery(sql); if (resultSet.next()) { String bookName = resultSet.getString("book_name"); String borrowerName = resultSet.getString("name"); String borrowDate = resultSet.getString("jie_data"); String returnDate = resultSet.getString("huan_data"); // 在界面上显示查询结果 bookNameField.setText(bookName); borrowerNameField.setText(borrowerName); borrowDateField.setText(borrowDate); returnDateField.setText(returnDate); } else { JOptionPane.showMessageDialog(null, "未找到该图书"); } } catch (SQLException ex) { ex.printStackTrace(); } } }); // ... } public static void main(String[] args) { LibraryManagementSystem system = new LibraryManagementSystem(); system.setSize(600, 400); system.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); system.setVisible(true); }}
注意,这只是一个示例代码,你需要根据你的具体需求进行修改和完善。另外,数据库连接相关的信息你需要根据实际情况进行填写。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |