tags:

views:

103

answers:

3

I want to get to the value I am finding using the COUNT command of SQL. Normally I enter the column name I want to access into the getInt() getString() method, what do I do in this case when there is no specific column name.

I have used 'AS' in the same manner as is used to alias a table, I am not sure if this is going to work, I would think not.

Statement stmt3 = con.createStatement();
ResultSet rs3 = stmt3.executeQuery("SELECT COUNT(*) FROM "+lastTempTable+") AS count");
    while(rs3.next()){
    count = rs3.getInt("count");
    }
+4  A: 

Use aliases:

SELECT COUNT(*) AS total FROM ..

and then

rs3.getInt("total")
Bozho
+3  A: 

I would expect this query to work with your program:

"SELECT COUNT(*) AS count FROM "+lastTempTable+")"

(You need to alias the column, not the table)

Brabster
+4  A: 

The answers provided by Bohzo and Brabster will obviously work, but you could also just use:

rs3.getInt(1);

to get the value in the first, and in your case, only column.

Eric Eijkelenboom
From Javadoc: "Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient."
Andrea Polci