|
发表于 2023-12-23 22:50:48
|
显示全部楼层
好的,以下是修改后的代码示例:import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.*;public class CRUDGUI extends JFrame { private JTextField idField; private JTextField nameField; private JTextField bookField; private JTextField borrowField; private JTextField returnField; private JButton insertButton; private JButton selectButton; private JButton updateButton; private JButton deleteButton; public CRUDGUI() { super("CRUD Operations"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建界面组件 idField = new JTextField(10); nameField = new JTextField(10); bookField = new JTextField(10); borrowField = new JTextField(10); returnField = new JTextField(10); insertButton = new JButton("插入"); selectButton = new JButton("查看"); updateButton = new JButton("修改"); deleteButton = new JButton("删除"); // 设置布局 setLayout(new FlowLayout()); // 添加组件到窗口 add(new JLabel("ID:")); add(idField); add(new JLabel("姓名:")); add(nameField); add(new JLabel("书名:")); add(bookField); add(new JLabel("借书日期:")); add(borrowField); add(new JLabel("还书日期:")); add(returnField); add(insertButton); add(selectButton); add(updateButton); add(deleteButton); // 添加事件处理 insertButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取用户输入的数据 int id = Integer.parseInt(idField.getText()); String name = nameField.getText(); String book_name = bookField.getText(); String jie_data = borrowField.getText(); String huan_data = returnField.getText(); // 将数据插入数据库 insertData(id, name, book_name, jie_data, huan_data); // 清空文本框 clearFields(); } }); selectButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 查询数据 selectData(); } }); updateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取用户输入的数据 int id = Integer.parseInt(idField.getText()); String name = nameField.getText(); String book_name = bookField.getText(); String jie_data = borrowField.getText(); String huan_data = returnField.getText(); // 更新数据 updateData(id, name, book_name, jie_data, huan_data); // 清空文本框 clearFields(); } }); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 获取用户输入的ID int id = Integer.parseInt(idField.getText()); // 删除数据 deleteData(id); // 清空文本框 clearFields(); } }); // 设置窗口大小和可见性 setSize(400, 200); setVisible(true); } private void insertData(int id, String name, String book_name, String jie_data, String huan_data) { Connection connection = null; PreparedStatement statement = null; try { // 建立数据库连接 String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabaseName;"; String username = "YourUsername"; String password = "YourPassword"; connection = DriverManager.getConnection(url, username, password); // 创建预编译的SQL语句 String sql = "INSERT INTO YourTableName (id, name, book_name, jie_data, huan_data) VALUES (?, ?, ?, ?, ?)"; statement = connection.prepareStatement(sql); // 设置参数值 statement.setInt(1, id); statement.setString(2, name); statement.setString(3, book_name); statement.setString(4, jie_data); statement.setString(5, huan_data); // 执行插入操作 statement.executeUpdate(); System.out.println("数据插入成功!"); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } private void selectData() { Connection connection = null; Statement statement = null; try { // 建立数据库连接 String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabaseName;"; String username = "YourUsername"; String password = "YourPassword"; connection = DriverManager.getConnection(url, username, password); // 创建Statement对象 statement = connection.createStatement(); // 定义查询语句 String sql = "SELECT * FROM YourTableName"; // 执行查询语句 ResultSet result = statement.executeQuery(sql); // 输出查询结果 while (result.next()) { int id = result.getInt("id"); String name = result.getString("name"); String book_name = result.getString("book_name"); String jie_data = result.getString("jie_data"); String huan_data = result.getString("huan_data"); System.out.println("ID: " + id + ", Name: " + name + ", Book Name: " + book_name + ", Borrow Date: " + jie_data + ", Return Date: " + huan_data); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } private void updateData(int id, String name, String book_name, String jie_data, String huan_data) { Connection connection = null; PreparedStatement statement = null; try { // 建立数据库连接 String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabaseName;"; String username = "YourUsername"; String password = "YourPassword"; connection = DriverManager.getConnection(url, username, password); // 创建预编译的SQL语句 String sql = "UPDATE YourTableName SET name = ?, book_name = ?, jie_data = ?, huan_data = ? WHERE id = ?"; statement = connection.prepareStatement(sql); // 设置参数值 statement.setString(1, name); statement.setString(2, book_name); statement.setString(3, jie_data); statement.setString(4, huan_data); statement.setInt(5, id); // 执行更新操作 statement.executeUpdate(); System.out.println("数据更新成功!"); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } private void deleteData(int id) { Connection connection = null; PreparedStatement statement = null; try { // 建立数据库连接 String url = "jdbc:sqlserver://localhost:1433;databaseName=YourDatabaseName;"; String username = "YourUsername"; String password = "YourPassword"; connection = DriverManager.getConnection(url, username, password); // 创建预编译的SQL语句 String sql = "DELETE FROM YourTableName WHERE id = ?"; statement = connection.prepareStatement(sql); // 设置参数值 statement.setInt(1, id); // 执行删除操作 statement.executeUpdate(); System.out.println("数据删除成功!"); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } catch (SQLException e) { e.printStackTrace(); } } } private void clearFields() { idField.setText(""); nameField.setText(""); bookField.setText(""); borrowField.setText(""); returnField.setText(""); } public static void main(String args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new CRUDGUI(); } }); }}请根据你的实际情况修改代码中的数据库连接信息、表名和列名。这个程序提供了四个功能:插入数据、查看数据、更新数据和删除数据。你可以在图形界面中输入相关数据并点击相应按钮来执行对应的操作。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|