Reputation: 619
I am trying to return the count value in the attached Java, this is what I have..
package com.example.tests;
import java.awt.geom.GeneralPath;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestNew {
public void generateid(){
int count;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://IP:PORT;DatabaseName=blabla;integratedSecurity=true;");
System.out.println("Connection Successfull");
System.out.println(conn);
//--------------------------------------------------------------------
Statement stmt = conn.createStatement();
String query1= "SELECT COUNT(*) from dbo.vSubscriberReporting where [SubscriberEmailAddressStatusID] in (5,1,6,7) and [SubscriberStatusID] = 1 AND [SubscriberAddresseeStatusID] = 1 and CompanySizeCodeID IN (4)";
ResultSet rs= stmt.executeQuery(query1);
count = rs.getInt(1);
//i cant store the count value from query in this variable... Please help me.
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
TestNew gen = new TestNew();
gen.generateid();
}
}
the query runs, but comes back with
com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
though I know there should be data, just a single COUNT record..
I would like to be able to store the result to a variable i guess and use it later to verify a field in a selenium script?
Upvotes: 0
Views: 1494
Reputation: 2740
As it s a single result; you can also do this way :
rs.moveFirst();
count = rs.getInt(1);
Upvotes: 0
Reputation: 1129
Call rs.next()
before trying to call getInt(1)
if(rs.next()) //Expecting one row.
{
count = rs.getInt(1);
}
Upvotes: 3