【问题标题】:Java Sql Exception After End of Result Set结果集结束后的 Java Sql 异常
【发布时间】:2019-11-07 14:45:41
【问题描述】:

我的代码运行良好,但是当我尝试运行代码时,它首先显示 java.sql.SQLException:After end of result set。我想知道是什么原因造成的,以及如何解决这个问题,因为这是一个分级项目。

public GenerateBill() 
{
    initComponents();
    try 
  {
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore","root","root");
        Statement stmt=(Statement)con.createStatement();
        String query, product;
        query="select * from store;";
        ResultSet rs=stmt.executeQuery(query);
        while(rs.next());
        {
            product=rs.getString("productname");
            jComboBox1.addItem(product);
        }
  } 
    catch(Exception e) 
  {
    JOptionPane.showMessageDialog(null,e.toString());
  }
}

当我执行代码时,Message Dialog Box 首先出现。 当我单击“确定”时,我尝试创建的页面将打开并正常执行。 所以,我很困惑这意味着什么。另外,我是这个网站的新手,所以我真的不知道我需要添加多少代码。其余代码用于不同的 jButton。该页面用于生成账单/收据。

【问题讨论】:

  • 良好的代码缩进将帮助我们阅读代码,更重要的是它将帮助您调试您的代码 Take a quick look at a coding standard 为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
  • 请始终向我们展示所有错误消息而不是摘要。这些消息中的有用信息通常比您提供的更多
  • @StarDragon23 就像上面提到的那些人,拥有完整的代码和错误消息应该会更好,更有用。但是,确保您的代码(如您给我们的示例中一样)在您成功声明后缺少关闭连接等,所以请也看看如何正确关闭连接,stackoverflow.com/questions/2225221/…
  • @dZ 我不知道如何关闭连接,或者我们甚至需要关闭它们,因为这是我在学校所学的全部内容。你能帮我了解更多吗?
  • 如果删除查询字符串中的分号会怎样?

标签: mysql netbeans-8 database-connectivity


【解决方案1】:

您的代码中有些部分可能会更好。具体来说,

  1. 使用 com.mysql.jdbc.Driver,因为您的数据库是 MySQL,而不是 java.sql.DriverManager

  2. 无需转换您的 Connection 对象。

  3. /bookstore 之后你可以添加?useSSL=false,虽然这不是强制性的,所以像jdbc:mysql://localhost:3306/bookstore?useSSL=false 之类的东西

  4. 使用java.sql.PreparedStatement 而不是简单的Statement

  5. catch 后在 finally 块中关闭您的连接。

最终,您的代码应该如下所示,

public GenerateBill() {

    initComponents();

    Connection con = null;
    ResultSet rs = null;
    PreparedStatement stmt = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false","root","root");

        String query = "select * from store";
        stmt = con.prepareStatement(query);

        String product;

        rs = stmt.executeQuery();

        while(rs.next())
        {
            product = rs.getString("productname");
            jComboBox1.addItem(product);
        }
  } catch(Exception e) {
        JOptionPane.showMessageDialog(null,e.toString());
  } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (Exception e) {
            LOG.error("Error closing the connection with the database...");
            e.printStackTrace();
        }
  }
}

试试上面的方法,如果可以,请告诉我。如果没有,请发布整个异常以查看导致问题的原因。

【讨论】:

  • 感谢您的帮助。它现在工作正常。关于异常的消息消失了。
  • @StarDragon23 很高兴我能帮上忙! :) 您必须记住的最重要的是在您成功操作数据库后关闭所有连接(ResultSet、PreparedStatement、Connection)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多