-2

I am using the following code in selenium, but I am not able to retrieve data from database. I am getting the errors "The result set has no current row"

My code is

public class Auto {

    public static void main(String[] args) throws InterruptedException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {


        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("Driver loaded");


        Connection con=DriverManager.getConnection("jdbc:sqlserver://192.168.*****\\SQLEXPRESS/*****_DW/EPS:****;user=******;password=******; ");

        System.out.println("Driver loadedd");


    java.sql.Statement stmt= con.createStatement();
    ResultSet rs=stmt.executeQuery("select * from [*****_DW].[dbo].dim_grantee ");

    System.out.println("Query executed");

    while (rs.next());
    {
        String Name=rs.getString("NAME");

        System.out.println("Name is");
    }


}

2 Answers 2

1

Change

while (rs.next());
{
    String Name=rs.getString("NAME");

    System.out.println("Name is");
}

to

while (rs.next())
{
    String Name=rs.getString("NAME");
    System.out.println("Name is");
}

i.e. remove semicolon from while line

0

The short version: Your query is not returning any rows.

The long version: You need to find out why your query is not returning any rows. The first step is to either step through the code with a debugger, or add a lot of print statements to determine what exactly is going on.

I suspect that either the table is empty or the connection is not properly formed. You might want to look at the answers to this Stack Overflow question for more information.

3
  • Why does rs.next() return true in that case?
    – Alexey R.
    Feb 11, 2019 at 14:13
  • We don't know from the description whether the error is being thrown on rs.next() or not. My assumption was that the error was being thrown by the rs.next() check.
    – Kate Paulk
    Feb 11, 2019 at 15:08
  • Kate, Connection is proper because for metdata it is working fine i can get the number of column count and all...that means connection is correct...
    – Shridhar
    Feb 12, 2019 at 5:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.