【问题标题】:java.sql.SQLException: Illegal operation on empty result set. dor select statementjava.sql.SQLException:对空结果集的非法操作。对于选择语句
【发布时间】:2014-06-23 00:38:35
【问题描述】:

我正在尝试从测试用例表中获取值,但它抛出 java.sql.SQLException: Illegal operation on empty result set 和 这是我的代码,

try {
            tcID=con.prepareStatement("select tc_id,tc_name from testcase where project_id=?  and tc_id=? ");
            tcID.setString(1,project);
            tcID.setString(2,tciD);
            ResultSet rs = tcID.executeQuery();
            System.out.println("Loop TC ID....."+rs.getString("tc_id"));
            System.out.println("Loop TC Name......"+rs.getString("tc_name"));
            while(rs.next()){
                System.out.println("Inside While.....");
                ps = con.prepareStatement("insert into execution_testcases (project_id, Rel_id, testcase_id, testcase_name, Flag) values(?,?,?,?,?)");
                ps.setString(1, project);
                ps.setString(2, RelName);
                ps.setString(3, rs.getString("tc_id"));
                ps.setString(4, rs.getString("tc_name"));
                ps.setString(5, "true");
                ps.executeUpdate();
            }
            String cmd= "java -jar D:\\FrameworkCodeDev\\Backups\\WebDriverJarFiles\\Build Release\\Framework_TAF_V10.jar";
            Runtime.getRuntime().exec(cmd);
        } catch (SQLException e) {          
            e.printStackTrace();
        }

谢谢。

【问题讨论】:

  • 我认为你应该删除 System.out.println("Loop TC ID....."+rs.getString("tc_id")); System.out.println("循环TC名称......"+rs.getString("tc_name"));之前 (rs.next())
  • 我删除并处理它

标签: java sql sql-server jsp servlets


【解决方案1】:

如果没有数据,您将无法从ResultSet 中获取字符串。调用rs.getString()之前需要检查是否有数据,可以使用isBeforeFirst()进行。

    ResultSet rs = tcID.executeQuery();
    if(rs.isBeforeFirst()) {
        System.out.println("Loop TC ID....."+rs.getString("tc_id"));
        System.out.println("Loop TC Name......"+rs.getString("tc_name"));
        while(rs.next()){
            System.out.println("Inside While.....");
            ps = con.prepareStatement("insert into execution_testcases (project_id, Rel_id, testcase_id, testcase_name, Flag) values(?,?,?,?,?)");
            ps.setString(1, project);
            ps.setString(2, RelName);
            ps.setString(3, rs.getString("tc_id"));
            ps.setString(4, rs.getString("tc_name"));
            ps.setString(5, "true");
            ps.executeUpdate();
        }
    } else {
        System.out.println("No data in RS!");
    }

原答案链接:https://stackoverflow.com/a/6813771/1627055

【讨论】:

  • in rs.isBeforeFirst() 对我来说是假的
  • 您手动对您的服务器运行该查询,看看它是否提供任何结果。
  • 我已经设置了这样的值 tcID.setInt(1,1); tcID.setInt(2,250);现在查询执行并返回值,但 wgy 它不适用于 setString()????
  • IIRC 并非所有 SQL 服务器实现都会即时进行类型转换字符串int,因此如果您的project_idint,那么您应该使用setInt(),以避免无关的类型转换和避免将250"250" 进行比较,结果明显为假。
【解决方案2】:

当您尝试打印空结果集的元素时,问题出在您的 System.out.println 语句中。您应该尝试删除它们:

try {
    tcID=con.prepareStatement("select tc_id,tc_name from testcase where project_id=?  and tc_id=? ");
    tcID.setInt(1,project);
    tcID.setInt(2,tciD);
    ResultSet rs = tcID.executeQuery();

    while(rs.next()){
        System.out.println("Inside While.....");
        ps = con.prepareStatement("insert into execution_testcases (project_id, Rel_id, testcase_id, testcase_name, Flag) values(?,?,?,?,?)");
        ps.setString(1, project);
        ps.setString(2, RelName);
        ps.setString(3, rs.getString("tc_id"));
        ps.setString(4, rs.getString("tc_name"));
        ps.setString(5, "true");
        ps.executeUpdate();
    }
    String cmd= "java -jar D:\\FrameworkCodeDev\\Backups\\WebDriverJarFiles\\Build Release\\Framework_TAF_V10.jar";
    Runtime.getRuntime().exec(cmd);
} catch (SQLException e) {          
    e.printStackTrace();
}

【讨论】:

  • 嗨 Guneli,我已经删除了 sout 并运行相同但它没有进入 while 循环,但所有正确的只是从 jquery 中获取准确的参数并在 bd 中有值
  • 嗨。您确定您的查询正在返回数据吗?
  • select tc_id,tc_name from testcase where project_id=1 and tc_id=250; id 1 和 250 来自 UI 我在 sql 工作台中执行的上述查询并返回数据。
  • 不确定这是否有意义,但请您试试 tcID.setInt(1,project); tcID.setInt(2,tciD);而不是 setString。
  • @Suganth,你试过了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 2016-04-30
  • 1970-01-01
  • 2017-09-17
  • 2013-03-28
相关资源
最近更新 更多