
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.No | Method & Description |
---|---|
1 |
int getErrorCode() This method returns the exception code for the Exception occurred. |
2 |
SQLException setNextException(SQLException ex) Using this method you can create a chain of exceptions by adding a new exception to the current exception. |
3 |
String getSQLState() This method returns the SQLState of the current exception. |
4 |
Iterator<Throwable> iterator() This method returns an iterator to iterate through the chain of SQLExceptions. |
5 |
void 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
Advertisements