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 8743883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:45:19+00:00 2026-06-13T11:45:19+00:00

I am currently using Java to try to INSERT a customer in my Database:

  • 0

I am currently using Java to try to INSERT a customer in my Database:

public void saveCustomer(Customer c)  {
    getConnection();

 try {

     CallableStatement pstmt =  con.prepareCall("{call sp_saveCustomer(?,?)}");
     pstmt.setString(1, c.getName());
     pstmt.setString(2, a.getLastName());
     pstmt.executeUpdate();

     closeConnection();
    } catch (SQLException e) {
       // Handle Exception
    }
}

When a user tries to INSERT a duplicate record, the catch block will trigger. What I’m worried about is that I don’t know if this means a performance hit. Isn’t a better way to check if a customer already exists? Something like:

public void saveCustomer(Customer c) {
    getConnection();

 boolean exists = false; 


     CallableStatement pstmt =  con.prepareCall("{call sp_checkCustomerExistence(?,?)}");
     pstmt.setString(1, c.getName());
     pstmt.setString(2, a.getLastName());
     ResultSet rs = pstmt.executeQuery();
     if (rs.next()) exists = true;
     if ( exists ){
     closeConnection(); 
     return;
     } 

     CallableStatement pstmt =  con.prepareCall("{call sp_saveCustomer(?,?)}");
     pstmt.setString(1, c.getName());
     pstmt.setString(2, a.getLastName());
     pstmt.executeUpdate();

     closeConnection();

}

What would be the best solution based on the best performance? Should I check myself if the customer exists or should I let the Exception be thrown?

  • 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-13T11:45:20+00:00Added an answer on June 13, 2026 at 11:45 am

    I think checking the duplicate is preferable than catching the exceptions. Only suggestion I have it to perform the check in your procedure itself if you can i.e. create a procedure as sp_check_and_save_customer which will check for duplicate and save if it doesn’t already exist. This way there will be very less time difference between the check and insert, which becomes more crucial in heavy loaded applications.

    Still, please have your exception handling as well as it is still possible that another process inserts the data in between the check and insert.

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

Sidebar

Related Questions

I am currently using this simple code to try to connect to my database
Currently trying to insert an object into the Customer table using a test SQL
I am currently using Java ME to extract Addressbook (ContactList) fields in a device
I am currently using Ubuntu 11.10 and java SE 1.6.0_26. I am trying to
I'm currently using the ImageJ jar (ij.jar) in a Java application that I am
I am currently looking in to some file uploading using Java Server Faces. I've
we are currently working on a new web application using Java and MySql. We
I am working on an IMAP client using java mail. We currently have a
I am using the URLUTF8Encoder.java class from W3C ( www.w3.org/International/URLUTF8Encoder.java ). Currently, it will
I'm new to Eclipse and am using it currently to play with Java EE.

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.