Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:53:19+00:00 2026-06-16T19:53:19+00:00

Through the following snippet, I am trying to run a query that either updates

  • 0

Through the following snippet, I am trying to run a query that either updates the data or inserts a new data into the table named JustPinged. The table contains a column named NodesThatJustPinged and LastPingedAt. If there is already a node in NodesThatJustPinged then the time in milliseconds in LastPingedAt is updated. Otherwise a new node information is inserted.

The problem is, that the following snippet is unable to insert the data into the database’s table. The reason is the statement:

boolean duplicateExists = searchToEliminateDuplicates.execute();

returns true to start with. (Initially the table is empty) Why does this statement return true? According to the documentation it returns true if the first result is a ResultSet object; false if the first result is an update count or there is no result. So here the boolean should contain a false value. But it contains a true value and thus the if statement always works. (And in if section,update query works when there is nothing to update !)

String searchQuery = "select NodesThatJustPinged from JustPinged where NodesThatJustPinged = '" + nodeInfo + "'";
PreparedStatement searchToEliminateDuplicates = connection.prepareStatement(searchQuery);
boolean duplicateExists = searchToEliminateDuplicates.execute();

if(duplicateExists) {
    // 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");              
}

So how should I edit the code, so that duplicate values are updated and new values are inserted?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:53:20+00:00Added an answer on June 16, 2026 at 7:53 pm

    Your searchQuery will return ResultSet. hence the execute method returns ‘true’. Try using executeQuery instead.

    So your code would become:

    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");              
          }
    

    P.S. If you are using PreparedStatement, then use parameters in your query and call ps.setString etc.

    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.

    PPPS Close your resultset and statements as soon as you are done with them.

    PPPPS A more better approach is to use count aggregate function in your SQL statement i.e.

    select count(NodesThatJustPinged) from JustPinged where NodesThatJustPinged = ‘” + nodeInfo + “‘”;

    Now you can check whether count is 0 or greater than 1 and branch your code accordingly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a JQGrid for a simple table. After following through
I have the following snippet of code that loops through the jQuery animate function
I'm new to asp.net and now following through the http://nerddinnerbook.s3.amazonaws.com/Part1.htm howto. All is fine
I have a simple model forms, that is being rendered through the following jinja2
I wanted to write following query through codeigniter's db helper class, guide me plz
I have the following snippet of java code: final Class<?> junitCoreClass = AccessController.doPrivileged( new
I have the following snippet that works fine on Heroku (production) for enforcing SSL:
The following snippet does work for what I need. I believe though that there
I am trying to loop through the below shown JS object with the following
I have the following HTML that I'm trying to parse using the HTML Agility

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.