VOOZH about

URL: https://www.geeksforgeeks.org/dbms/how-to-handle-duplicate-key-violations-in-jdbc/

⇱ How to handle Duplicate Key Violations in JDBC? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to handle Duplicate Key Violations in JDBC?

Last Updated : 23 Jul, 2025

The JDBC is the Application Programming Interface for Java programming language. In this article, we will learn how to handle duplicate key violations in JDBC by using try and catch block. If we insert new data with an already existing primary key, we get an SQL exception then we need to handle that exception properly. Otherwise, the new data is successfully inserted into the database. Below we have explained this concept with proper examples for both duplicate key violations and successful insertion.

Handle Duplicate key violations in JDBC

To handle duplicate key violations in JDBC. Here, we have used the primary key concept as well as the try-and-catch concept in Java to handle that duplicate key exception.

  • In Java, the try-catch concept is used for handling exceptions, and the primary key concept is used for handling duplicate data in the table.
  • The primary key is a column that uniquely identifies each row in the table.
  • We have created one table with a name book in the database.
  • This Table contains four columns namely id, author, name, and price.
  • Here, we have used primary for the id column.
  • This means If we insert data with an existing ID in the table, then we get an error message like duplicate data found.

Below we provide the table meta information for reference.

Table Structure:

In the below table, we have six columns and four fields:

👁 Table Metadata

Table Data:

👁 Data in Table

Java Program to handle Duplicate Key Violations in JDBC

Example 1: Duplicate key Violations

In this example, inside class, we define the error code to handle the duplicate key violations in JDBC. By using this error code, we will provide the handling message to the end users for understanding what the exact problem is.

Output:

Below we can refer to the console output.

👁 Console Output

Explanation of the Program:

Here, the error code is 1062 which indicates the error while duplicate key violations is happening.

  • In main method, we have started developing required logic. First, we started with database connection with required configuration.
  • After successful database connection, we have inserted one new row data into the Table with already existing id in the table.
  • Here, we inserted with duplicate key, so we got exception.
  • Then, it handles that SQL exception by using error code in the catch block.
  • Once SQL exceptions is raised, then we check the exception, then it prints the Error message.
  • Otherwise, the new data successfully inserted into table.

Example 2: Successful Insertion

Output:

Below we can see the output in console.

👁 Output in Console

Database Table:

👁 Database Table

Explanation of the above Program:

  • We provide a new row of data with non-existing id in the table.
  • This means, we have inserted the data with a new id.
  • Then the data is successfully inserted into the Table without any exceptions.
  • If any exceptions are raised, then then the catch block logic will handle that exception.
Comment
Article Tags:

Explore