Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Count into a Java var 1

Status
Not open for further replies.

Smarty

Programmer
Apr 12, 2001
191
BE
I have the following line:
int newmes = s.executeUpdate("SELECT COUNT(*) FROM messages where MessageTo = " + rs.getInt("MemberID"));

But it always returns 0 and not the count value. I assume 0 is the state of execution, but how can I have the count because I need to use that in the rest of my program!

Thanx in advance

Smarty
 
If you look at the javadocs for executeUpdate(), it states:

Code:
"Returns:  either the row count for INSERT, UPDATE or DELETE statements; or 0 for SQL statements that return nothing"

That's why you're always getting a return value of 0.

To remedy this, you want to call the executeQuery() method of the PreparedStatement instance. This returns a ResultSet instance on which you would then call
Code:
getInt("COUNT(*)")
.

Hope this helps,
Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top