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

  • SEARCH
  • Home
  • 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 6689049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:28:27+00:00 2026-05-26T05:28:27+00:00

I was working on a servlet that will generate a unique code and update

  • 0

I was working on a servlet that will generate a unique code and update that in a mySQL database.
Now, in that, I want to catch any exception thrown in case that unique code already exists in the mySQL table and generate a new code and try updating the database. The problem is I want to do this WITHIN the for loop itself. The code is as follows:

    try
    {
    connection = datasource.getConnection();
    SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+unique_code+"','08-10-2011 04:48:48','0')";
      PreparedStatement ps1 = connection.prepareStatement(SQLUpdate);
      ps1.executeUpdate();
      ResultSet r = ps1.getResultSet(); // this is where I'm checking if it's a duplicate
      if(r==null)
          out.println("This is a duplicate");
      else out.println("Updated");
      trial12= "08-10-2011 04:48:480.03999855056924717a";
      SQLUpdate = "INSERT INTO Voucher_dump VALUES( '"+trial12+"','08-10-2011 04:48:48','0')";

      ps1 = connection.prepareStatement(SQLUpdate);
      ps1.executeUpdate();
      r = ps1.getResultSet();
      if(r==null)
          out.println("This is a duplicate");
      else out.println("Updated");

    }
    catch (SQLException sqle)
    {
        sqle.printStackTrace();
    }

I don’t want to wait till the end of the entire loop to catch the SQLException (I have already defined this key in mySQL as primary). The moment, the result comes back as a duplicate entry, I want to re-generate this key and attempt the update again.My output for this particular code is coming blank on my output page (all other parameters are showing correctly). Neither is “This is a duplicate” displayed nor is “Updated”. Maybe, ResultSet is not the best way to do it. Could you guys give me some advice on what would be the best way forward ?

  • 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-05-26T05:28:28+00:00Added an answer on May 26, 2026 at 5:28 am

    Some advice in no particular order:

    1. Close the connection in a finally block.
    2. Close statements individually if you’ll be creating many of them before closing the connection. (“Many” is defined by your DBAs.)
    3. Format your code.
    4. Don’t use stdout and/or stderr from real code. Pick a logging framework.
    5. Consider using some helper classes to simplify (and correct) your database access, like Spring’s JdbcTemplate.
    6. Make sure to include relevant context when you post example code.

    Due to #6, I don’t know what out is, but I suspect the reason you’re not seeing anything is that you’re inserting a duplicate value with the first statement, which will cause a SQLException from that line, not at getResultSet(), where you seem to expect it. Since the error is written to stdout, it’ll show up in your server logs somewhere, but nothing will be written to out. I’m not sure why you think getResultSet() will return null or not null depending on whether there was a constraint violation. Take a look at the javadoc for that method.

    Update: 7. As BalusC points out, never, ever concatenate a string directly into a JDBC Statment. Use PreparedStatment’s placeholders and set* methods. For info on SQL injection, see Wikipedia and XKCD.

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

Sidebar

Related Questions

I'm working on a small django project that will be deployed in a servlet
I'm working on an application. I have a servlet (writeDataBase.class) that writes some information
Where should I locate the code for validating an employee ID (badge) that will
I have memory leak in the web application (servlet) that I am working on.
I am working on a servlet (runs on tomcat) which receives requests that contains
I have a decent working web application (Java/Servlet/Jsp) that I would like to improve
I'm currently working on a report generation servlet that agglomerates information from several tables
I have a servlet. But it is not working as desired. Hence for debugging
Working with a SqlCommand in C# I've created a query that contains a IN
I've created a servlet that changes the binaries of a SWF file and output

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.