Reputation: 1040
Hello I am working on a java code that is suppose to get the count of the total number of users in the user profile table I am working on. Now I am sure I got the sql statement right but for some reason I dont know how to get the numeric value from it. Any help is greatly appreciated and welcomed.I am sure the way I am trying to get the value doesn't work but I dont know how to get the value, Here is the code below and thank you:
public int getUserCount() throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
double encinfo;
conn = DAOFactory.getDatabaseDAO().getConnection();
StringBuffer query = new StringBuffer();
query.append( "Select COUNT (DISTINCT record_id)");
query.append( "From USER_PROFILE");
stmt = conn.createStatement();
rs = stmt.executeQuery( query.toString());
if (!rs.next())
throw new SQLException( SQLException.User_NOT_FOUND,"No User Count found.");
// Should be able to get the user amount or value or total users in DB.
userInfo = rs.getDouble( "USER_VALUE");
m_userCount = (int) userInfo;
}
Upvotes: 1
Views: 2898
Reputation: 66647
This may work:
Add alias to record count:
query.append( "Select COUNT (DISTINCT record_id) AS USER_VALUE");
query.append( " From USER_PROFILE");
Another way could be:
rs.getDouble(1);
Here 1 is the index of column.
Upvotes: 7
Reputation: 7415
rs.getDouble(1);
Since you have 1 column, it will return the value of the current row and 1st column
Upvotes: 3