Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you're hauling the <strong>entire</strong> database table down into Java's memory and testing <em>every</em> row in a while loop. You don't break the loop if a match is found so that it continues overwriting the boolean outcome until with the last row.</p> <p>That said, you really don't want to do the comparison in Java. Just make use of the <a href="http://www.w3schools.com/SQl/sql_where.asp" rel="nofollow noreferrer">SQL <code>WHERE</code> clause</a>. That's <em>much</em> more efficient and really the task a DB is supposed to do. Don't try to take over the DB's work in Java, it's only going to be inefficient.</p> <pre><code>public boolean exists(String username, String password) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; boolean exists = false; try { connection = database.getConnection(); preparedStatement = connection.prepareStatement("SELECT id FROM client WHERE username = ? AND password = ?"); preparedStatement.setString(1, username); preparedStatement.setString(2, password); resultSet = preparedStatement.executeQuery(); exists = resultSet.next(); } finally { close(resultSet); close(preparedStatement); close(connection); } return exists; } </code></pre> <p>You see that I made a few enhancements:</p> <ol> <li>Use preparedstatement.</li> <li>Do not use equalsignorecase. A password of "FooBar" should NOT be the same as "foobar".</li> <li>Gently acquire and close resources in same scope to avoid leaking.</li> <li>Have it in an independent and reuseable non-static DAO method.</li> </ol> <p>To learn more about using JDBC the proper way you may find <a href="http://balusc.blogspot.com/2008/07/dao-tutorial-data-layer.html" rel="nofollow noreferrer">this basic tutorial</a> useful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload