1

I am trying to fetch the values from testcase table but its throwing java.sql.SQLException: Illegal operation on empty result set and Here is my code,

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();
        }

Thanks.

Suganth
  • 25
  • 3
  • 11
  • 4
    I think you should remove System.out.println("Loop TC ID....."+rs.getString("tc_id")); System.out.println("Loop TC Name......"+rs.getString("tc_name")); before while (rs.next()) – Guneli May 06 '14 at 11:55
  • that i removed and working on it – Suganth May 06 '14 at 13:02

2 Answers2

1

You are not able to get strings out of ResultSet if there's no data. You need to check if there's data before calling rs.getString(), you can use isBeforeFirst() to do this.

    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!");
    }

Original answer link: https://stackoverflow.com/a/6813771/1627055

Community
  • 1
  • 1
Vesper
  • 18,599
  • 6
  • 39
  • 61
  • in rs.isBeforeFirst() is coming as false for me – Suganth May 06 '14 at 12:41
  • You run that query against your server manually and see if it provides any results. – Vesper May 06 '14 at 12:51
  • I have set the values like this tcID.setInt(1,1); tcID.setInt(2,250); now the query executing and returning the values but wgy its not working for setString()???? – Suganth May 06 '14 at 12:59
  • IIRC not all SQL server implementations do typecasts string<->int on the fly, so if your `project_id` is `int` then you should use `setInt()`, both to avoid extraneous type conversion and avoid comparing `250` to `"250"` with an obvious result of false. – Vesper May 06 '14 at 13:04
0

The problem is in your System.out.println statements as you trying to print the elements of an empty resultset. You should try by removing them:

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
  • 1,691
  • 1
  • 11
  • 17
  • Hi Guneli, I have removed sout and run the same but its not entering into the while loop, but everything corect only am getting exact parameter from the jquery and having values in bd – Suganth May 06 '14 at 12:39
  • Hi. Are you sure that your query is returning data? – Guneli May 06 '14 at 12:41
  • select tc_id,tc_name from testcase where project_id=1 and tc_id=250; the id 1 and 250 is coming from UI the above query i have executed in the sql work benchand its returning the data. – Suganth May 06 '14 at 12:43
  • Not sure if it will make sense, but can you, please, try tcID.setInt(1,project); tcID.setInt(2,tciD); instead of setString. – Guneli May 06 '14 at 12:47
  • @Suganth, did you try this? – Guneli May 06 '14 at 15:48