0

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?

2 Answers 2

3

Call rs.next() before trying to call getInt(1)

if(rs.next()) //Expecting one row.
{
    count = rs.getInt(1);
}
Sign up to request clarification or add additional context in comments.

Comments

0

As it s a single result; you can also do this way :

rs.moveFirst();
count = rs.getInt(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.