我正在尝试从testcase表中获取值,但是它抛出java.sql.SQLException:对空结果集进行非法操作 这是我的代码,
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();
}
感谢。
答案 0 :(得分: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!");
}
答案 1 :(得分:0)
当您尝试打印空结果集的元素时,问题出在您的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();
}