Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your searchQuery will return ResultSet. hence the execute method returns 'true'. Try using executeQuery instead.</p> <p>So your code would become:</p> <pre><code>String searchQuery = "select NodesThatJustPinged from JustPinged where NodesThatJustPinged = '" + nodeInfo + "'"; Statement searchToEliminateDuplicates = connection.createStatement(); ResultSet duplicateExists = searchToEliminateDuplicates.executeQuery(searchQuery); if(duplicateExists.next()) { // update the LastPingedAt column in the JustPinged table String updateQuery = "update JustPinged set LastPingedAt='" + pingedAt + "' where NodesThatJustPinged = '" + nodeInfo + "'"; PreparedStatement updateStatement = connection.prepareStatement(updateQuery); updateStatement.executeUpdate();System.out.println("If statement"); } else { // make a new entry into the database String newInsertionQuery = "insert into JustPinged values('" + nodeInfo + "','" + pingedAt + "')"; PreparedStatement insertionStatement = connection.prepareStatement(newInsertionQuery); insertionStatement.executeUpdate();System.out.println("else statement"); } </code></pre> <p>P.S. If you are using PreparedStatement, then use parameters in your query and call ps.setString etc.</p> <p>PPS. Don't use execute() method. Use executeQuery or executeUpdate. execute() is used where you don't know in advance whether your query is INSERT or UPDATE.</p> <p>PPPS Close your resultset and statements as soon as you are done with them.</p> <p>PPPPS A more better approach is to use count aggregate function in your SQL statement i.e. </p> <p>select count(NodesThatJustPinged) from JustPinged where NodesThatJustPinged = '" + nodeInfo + "'";</p> <p>Now you can check whether count is 0 or greater than 1 and branch your code accordingly.</p>
 

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