user1369975
user1369975

Reputation: 420

Difference between results fetched through command line and JDBC query

I have a SQLITE database containing a table: Service={ID,Port,Name}

I opened the command line and populated it with the following data:

1 23 abc

2 25 xyz

Then I was working on an app that used JDBC bridge using to fetch the results.I used following startegy:

import java.sql.*;

public class SQLiteJDBC
{
  public static void main( String args[] )
  {

    Connection c = null;
    Statement stmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:test.db");
      stmt = c.createStatement();
      ResultSet rs = stmt.executeQuery( "SELECT ID FROM SERVICE WHERE PORT=23;" );
      System.out.println(rs.getInt("ID"));
    }
    catch(Exception e){ }
   }
}

This to my amazement returns 4 as result but when I type the following on command line,the result is 1 that is correct. Whythe difference in results?

Upvotes: 0

Views: 76

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109613

Forgotten was an rs.first/rs.next

while (rs.next()) {
    System.out.println(rs.getInt("ID"));
}
rs.close();
stmt.close();
...
catch (Exception e) {
    e.printStacktrace();
}

Normally one would even expect an SQLException, though evidently that did not happen (you got 4).

Upvotes: 2

Related Questions