【问题标题】:I am getjava.sql.SQLException: After end of result set [closed]我是getjava.sql.SQLException:结果集结束后[关闭]
【发布时间】:2017-01-02 10:21:26
【问题描述】:
package database;

import java.sql.*;


public class MysqlCon {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/neha","root","root");

    Statement stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery("select * from student");
    while(rs.next());
    System.out.println(rs.getInt(1)+" "+rs.getString(2));
    con.close();
}
catch(Exception e)
{
    System.out.println(e);
    }
    }

}

【问题讨论】:

  • java.sql.SQLException: 结果集结束后
  • 您在 while 之后立即有一个 ;,因此您的代码不会像您认为的那样做。这也是为什么一致的缩进和总是使用块是可取的。投票结束是一个错字。

标签: java mysql jdbc


【解决方案1】:
while(rs.next());

那行正在遍历整个结果集,并且没有做任何事情(因为分号结束了语句)。然后你的下一行代码在没有下一项的结果集上执行,因此失败。

删除该行中的分号,然后它将起作用

【讨论】:

    【解决方案2】:

    认为问题在于您通过执行while(rs.next()); 迭代整个结果集。删除分号以遍历所有记录

    【讨论】:

      【解决方案3】:

      试试这个:

      try{
              Class.forName("com.mysql.jdbc.Driver");
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/neha","root","root");
      
              Statement stmt=con.createStatement();
              ResultSet rs=stmt.executeQuery("select * from student");
              while(rs.next()){
              System.out.println(rs.getInt(1)+" "+rs.getString(2));
          }    
          con.close();
          }
          catch(Exception e)
          {
              System.out.println(e);
              }
              }
      
          }
      

      【讨论】:

      • 请不要只是转储代码,解释什么是错误的以及您的代码如何解决它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 2013-01-03
      相关资源
      最近更新 更多