Reputation: 1236
I'm trying to get the number of times where a username has been found in a table, the thing is that I am just getting a value of "1" every time, even if the username is not found (where the value should be 0) I think the problem is that I am using rsMetaData.getColumnCount();
and that is just counting the numbers of columns found and not the amount of times shown in the column?
Here is the query just in case there is something wrong in it:
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM user_names WHERE name like '%"+givenName+"%'");
Upvotes: 1
Views: 1006
Reputation: 3732
You are right, rsMetaData.getColumnCount();
will just get you the number of columns returned from the query, which is of course always 1. What you should be doing instead is rs.getInt(0)
to get the contents of the first column (the one with index 0) as an integer
Upvotes: 1
Reputation: 6826
rsMetaData.getColumnCount();
As the method says this will return the number of columns which in your query is 1 (the count one column).
To get the desired data simply assign a name on the count fetch the result.
To do this add the desired name
ResultSet rs = st.executeQuery("SELECT COUNT(*) as totalCount FROM user_names WHERE name like '%"+givenName+"%'");
and then fetch the result using
rs.getInt("totalCount");
Upvotes: 2