The_dream1121 2024-05-13 01:21 采纳率: 88.2%
浏览 3
已结题

Java代码出现问题请求帮助


import org.apache.commons.dbutils.DbUtils;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class DataOperateDemo extends JFrame {
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/mysql";
    static final String USER = "root";
    static final String PASS = "gena20031121";

    private JTable table;
    private JScrollPane scrollPane;
    private JButton addButton;
    private JButton deleteButton;
    private JButton updateButton;
    private JButton searchButton;

    public DataOperateDemo() {
        setTitle("数据表操作");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);

        // 创建JTable和滚动条
        table = new JTable();
        scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane, BorderLayout.CENTER);

        // 创建操作按钮
        addButton = new JButton("增加");
        deleteButton = new JButton("删除");
        updateButton = new JButton("修改");
        searchButton = new JButton("查询");
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        buttonPanel.add(updateButton);
        buttonPanel.add(searchButton);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        addButton.addActionListener(e -> addData());
        deleteButton.addActionListener(e -> deleteData());
        updateButton.addActionListener(e -> updateData());
        searchButton.addActionListener(e -> searchData());

        setVisible(true);
    }

    // 增加数据
    private void addData() {
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            int id = Integer.parseInt(JOptionPane.showInputDialog("请输入ID"));
            String name = JOptionPane.showInputDialog("请输入姓名");
            int age = Integer.parseInt(JOptionPane.showInputDialog("请输入年龄"));

            String sql = "INSERT INTO student (id, name, age) VALUES (" + id + ", '" + name + "', " + age + ")";
            stmt.executeUpdate(sql);

            JOptionPane.showMessageDialog(this, "添加成功");
        } catch (SQLException | ClassNotFoundException se) {
            se.printStackTrace();
            JOptionPane.showMessageDialog(this, "添加失败:" + se.getMessage());
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    // 删除数据
    private void deleteData() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请先选择一行数据");
            return;
        }

        int id = Integer.parseInt(table.getValueAt(selectedRow, 0).toString());

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            String sql = "DELETE FROM student WHERE id=" + id;
            stmt.executeUpdate(sql);

            JOptionPane.showMessageDialog(this, "删除成功");
        } catch (SQLException | ClassNotFoundException se) {
            se.printStackTrace();
            JOptionPane.showMessageDialog(this, "删除失败:" + se.getMessage());
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    // 修改数据
    private void updateData() {
        int selectedRow = table.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请先选择一行数据");
            return;
        }

        int id = Integer.parseInt(table.getValueAt(selectedRow, 0).toString());
        String name = JOptionPane.showInputDialog("请输入姓名");
        int age = Integer.parseInt(JOptionPane.showInputDialog("请输入年龄"));

        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            String sql = "UPDATE student SET name='" + name + "', age=" + age + " WHERE id=" + id;
            stmt.executeUpdate(sql);

            JOptionPane.showMessageDialog(this, "修改成功");
        } catch (SQLException | ClassNotFoundException se) {
            se.printStackTrace();
            JOptionPane.showMessageDialog(this, "修改失败:" + se.getMessage());
        } finally {
            try {
                if (stmt != null) stmt.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
            try {
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    // 查询数据
    private void searchData() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName(JDBC_DRIVER);
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            stmt = conn.createStatement();

            String sql = "SELECT * FROM student";
            rs = stmt.executeQuery(sql);

            // 将查询结果添加到JTable中
            table.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException | ClassNotFoundException se) {
            se.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                if (conn != null) conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        new DataOperateDemo();
    }
}

java: 找不到符号
符号: 方法 resultSetToTableModel(java.sql.ResultSet)
位置: 类 org.apache.commons.dbutils.DbUtils

  • 写回答

4条回答 默认 最新

  • 笑面虎 2024-05-13 01:21
    关注

    以下内容由CHATGPT及笑面虎共同生成、有用望采纳:


    【您想要解决的问题】:您提问的目的是解决Java代码中出现的编译错误,具体来说是找不到org.apache.commons.dbutils.DbUtils类中的resultSetToTableModel方法。

    【问题出现原因】:问题出现的原因是在您的代码中使用了DbUtils.resultSetToTableModel(rs)这行代码,但是在您的项目中没有包含Apache Commons DbUtils库,或者该库的版本不支持这个方法。

    【问题解决方案】:为了解决这个问题,您需要确保您的项目中包含了正确版本的Apache Commons DbUtils库,该库应该包含resultSetToTableModel方法。如果您已经包含了该库但仍然出现问题,可能是因为您使用的库版本不支持这个方法。您可以尝试更新到一个支持该方法的版本。

    【提供代码】:由于您的问题主要是关于库的使用,而不是代码逻辑的问题,所以不需要提供完整的代码。您只需要确保您的项目依赖中包含了Apache Commons DbUtils库。

    【代码运行方式】:要运行您的代码,您需要:

    1. 确保您的开发环境中安装了Java Development Kit (JDK)。
    2. 将Apache Commons DbUtils库添加到您的项目依赖中。如果您使用Maven或Gradle,您可以在项目的pom.xmlbuild.gradle文件中添加相应的依赖。
    3. 确保您的数据库服务正在运行,并且数据库连接信息(URL、用户名、密码)是正确的。
    4. 在IDE中运行main方法,或者在命令行中编译并运行您的Java程序。

    【代码预期运行结果】:如果问题解决,您的Java程序应该能够成功编译并运行,允许您通过图形界面增加、删除、修改和查询数据库中的数据。

    【推荐相关链接】:

    • Apache Commons DbUtils官方文档:http://commons.apache.org/proper/commons-dbutils/
    • Maven中央仓库DbUtils页面:https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils
    • Gradle中央仓库DbUtils页面:https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils

    请根据您的项目构建工具选择合适的依赖添加方式,并确保您的项目使用的是支持resultSetToTableModel方法的DbUtils版本。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 创建了问题 5月13日