Dan
Dan

Reputation: 2100

Printing a query from a database

I am trying to access a database and print off a query. I am trying to access a DEVICE table and print off the DEVICE_ID, but i am unsuccessful so far.

Here is my code at the moment;

public static void main(String[] args) throws FileNotFoundException {
    try {
        Class.forName("org.hsqldb.jdbc.JDBCDriver");

        Preferences sysRoot = Preferences.systemRoot();
        Preferences prefs = sysRoot.node("com/davranetworks/zebu");
        url = prefs.get("dburl", "jdbc:hsqldb:E:\\eem\\eemdb");

    } catch (Exception e) {
        e.printStackTrace();
    }

    Connection c = getConnection();

    try {
        c.setAutoCommit(true);
        Statement s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet rs = s.executeQuery("SELECT * FROM eem_db.device");

        ResultSet deviceId = s.executeQuery("select device_id from eem_db.device");
        System.out.println(deviceId);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static Connection getConnection() {
    Connection c = null;
    try {
        c = DriverManager.getConnection(url);
    } catch (SQLException e) {
        System.out.println("Error initialising connection" + e);
    }
    return c;
}

The returned value is org.hsqldb.jdbc.JDBCResultSet@1d3d68df. I don't know what this value relates to as I was expecting 3 integer values. Can anyone help me on this?

Upvotes: 0

Views: 173

Answers (5)

juanluisrp
juanluisrp

Reputation: 237

You have to iterate over the rows contained in the ResultSet and for each row get the column you want:

ResultSet deviceIdRS = s.executeQuery("select device_id from eem_db.device");
while(deviceIdRS.next()) {
    System.out.println(deviceIdRS.getString("device_id"));
}

You must use the ResultSet getXXX method that correspond with your column type, for example, getInt, getString, getDate...

Upvotes: 2

ChiranjeeviIT
ChiranjeeviIT

Reputation: 529

You are printing object of ResultSet, it won't give you the right values. You need to iterate the loop like below

while(deviceId.next()) {
    int integerValue = deviceId.getInt(1);
    System.out.println("content" + integerValue)
}
deviceId.close();
s.close();

Upvotes: 0

Java Man
Java Man

Reputation: 1860

 while (deviceId.next()) 
 {
    // Printing results to the console
      System.out.println("device_id- "+ deviceId.getInt("device_id");

 }

iterate object using resultset.

Upvotes: 0

shrekDeep
shrekDeep

Reputation: 2328

s.executeQuery("select device_id from eem_db.device"); is returning a resultSet, you must find out the value from result set. like

int device_id = resultset["deviceId"];

Upvotes: 0

Paul Lo
Paul Lo

Reputation: 6138

That ResultSet deviceId is actually an object contains rows of result from your sql, so you only can see the memory address when you print it out. You need something like:

while(deviceId.next()){
     System.out.print(deviceId.getInt(1));         
}

Upvotes: 0

Related Questions