Results 1 to 3 of 3
- 01-21-2012, 02:28 PM #1
Member
- Join Date
- Dec 2011
- Location
- Brazil
- Posts
- 26
- Rep Power
- 0
MySQL simple select - PreparedStatement execute() returns true if not found?
The idea:
The user registers himself on the website providing an e-mail address. The system checks if this e-mail is already registered on the database.
The problem:
Well, from my understanding, PreparedStatement.execute() should return false if there are no results, but it's always returning TRUE, even though there's nothing on the table.
According to this: Statement (Java 2 Platform SE v1.4.2)
The code:
Any ideas on this one?Java Code:public boolean userExists(String email) throws SQLException { Database.stmt = Database.conn.prepareStatement("SELECT * FROM " + "glconnect_users" + " WHERE email=?"); Database.stmt.setString(1, "test@test.com"); boolean exists = Database.stmt.execute(); Database.stmt.close(); return exists; }
- 01-21-2012, 05:11 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
Re: MySQL simple select - PreparedStatement execute() returns true if not found?
I think its true because it still returns a ResultSet object, even though there are no results in the ResultSet. Try invoking the getResultSet() method and you will get a non-null object.
- 01-21-2012, 05:34 PM #3
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: MySQL simple select - PreparedStatement execute() returns true if not found?
Read the API for PreparedStatement - you are executing a query, which always returns a ResultSet, thus execute() will always return true. You could call getResultSet or call executeQuery and use the returned ResultSet to check the query result. Lastly, it would be better practice to reform your query away from SELECT *, perhaps something like SELECT count(email) would be more appropriate
Similar Threads
-
my simple boolean code keeps saying true
By shazakala in forum New To JavaReplies: 8Last Post: 03-27-2011, 10:30 AM -
MySQL JDBC query returns no response
By PDXErik in forum New To JavaReplies: 1Last Post: 08-20-2010, 09:07 AM -
SELECT * FROM bla WHERE username IN (?, ?, ...) using PreparedStatement
By flok in forum JDBCReplies: 6Last Post: 03-04-2010, 12:16 PM -
MySQL/JDBC Prepared Statement Select query
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 05:29 PM -
problems about storing binary data to mysql db using PreparedStatement
By xiechao in forum New To JavaReplies: 0Last Post: 04-22-2008, 11:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks