How to handle Exceptions while working with JDBC applications?


Whenever a JDBC application encounters an issue while executing SQL statements an SQLException is thrown.

This class provides information on the errors that occur while interacting with the database.

Following are the main methods of the SQLException class:

Sr.NoMethod & Description
1int getErrorCode()
This method returns the exception code for the Exception occurred.
2SQLException setNextException(SQLException ex)
Using this method you can create a chain of exceptions by adding a new exception to the current exception.
3String getSQLState()
This method returns the SQLState of the current exception.
4Iterator<Throwable> iterator()
This method returns an iterator to iterate through the chain of SQLExceptions.
5void getNextException(SQLException ex)
This method is used to retrieve next SQLException in this exception chain.

Example:

Following example demonstrates how to handle SQL Exceptions. Here we are creating a table which already exists and printing the code, state and, message of the occurred exception.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HandlingExceptions {
   public static void main(String args[]) {
      try {
         //Registering the Driver
         DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
         //Getting the connection
         String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
         Connection con = DriverManager.getConnection(oracleUrl, "system", "password");
         System.out.println("Connected to Oracle database....");
         //Creating the Statement
         Statement stmt = con.createStatement();
         //Executing the statement
         String createTable = "CREATE TABLE Students( " + "Name VARCHAR(255), " + "Age INT NOT NULL, " + "Percentage INT)";
         stmt.execute(createTable);
         PreparedStatement pstmt = con.prepareStatement("INSERT INTO Student VALUES(?, ?, ?)");
         pstmt.setString(1, "Raju");
         pstmt.setInt(2, 19);
         pstmt.setInt(3, 85);
         pstmt.execute();
         pstmt.setString(1, "Raja");
         pstmt.setInt(2, 17);
         pstmt.setInt(3, 67);
         pstmt.execute();
         ResultSet rs = stmt.executeQuery("Select *from Student");
         while(rs.next()) {
            System.out.print("Name: "+rs.getString("Name")+", ");
            System.out.print("Age: "+rs.getInt("Age")+", ");
            System.out.print("Percentage: "+rs.getString("Percentage"));
            System.out.println();
         }
      } catch(SQLException e) {
         //Getting the SQL error code
         System.out.println("Code of the exception: "+e.getErrorCode());
         //Getting the SQL state
         System.out.println("State of the exception: "+e.getSQLState());
         //Getting the message
         System.out.println("Message: "+e.getMessage());
      }
   }
}

Output:

Connected to Oracle database....
Code of the exception: 955
State of the exception: 42000
Message: ORA-00955: name is already used by an existing object

Updated on: 30-Jul-2019

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements