Results 1 to 3 of 3
  1. #1
    KingdomX is offline Member
    Join Date
    Dec 2011
    Location
    Brazil
    Posts
    26
    Rep Power
    0

    Question 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:
    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;
        }
    Any ideas on this one?

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

    Default 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.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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

  1. my simple boolean code keeps saying true
    By shazakala in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 10:30 AM
  2. MySQL JDBC query returns no response
    By PDXErik in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 09:07 AM
  3. Replies: 6
    Last Post: 03-04-2010, 12:16 PM
  4. MySQL/JDBC Prepared Statement Select query
    By thelinuxguy in forum Advanced Java
    Replies: 4
    Last Post: 02-12-2009, 05:29 PM
  5. Replies: 0
    Last Post: 04-22-2008, 11:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •