Can you explain SQLException class? What is SQL State in SQL Exception

Can you explain SQLException class? What is SQL State in SQL Exception

The SQLException Class and its subtypes provide information about errors and warnings that occurs when the data source is accessed. This class is an extension of java.lang.exception and provides information related to failure in database connectivity. The following information is available from an SQLException:

1. Text Description
2. SQL State
3. Error Code
4. A reference to any other exception that also occur.

Program for SQL Exception
import java.io.*;

import java.sql.*;

import java.util.*;

class dri

{

   public static void main(String x[])

   {

       try

       {

           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

           System.out.println("Driver registered with the driver manager class");

           Connection con = DriverManager.getConnection("jdbc:odbc:students");

           System.out.println("connected");

           BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

           System.out.println("Enter Roll no");

           int a=Integer.parseInt(br.readLine());

           System.out.println("Enter name");

           String b=br.readLine();

           System.out.println("Enter branch");

           String c=br.readLine();

           System.out.println("Enter Marks");

           int d=Integer.parseInt(br.readLine());

           PreparedStatement p=con.prepareCall("insert into stud values(?,?,?,?)");

           p.setInt(1,a);

           p.setString(2,b);

           p.setString(3,c);

           p.setInt(4,d);

           p.executeUpdate();

           System.out.println("Record inserted");

       }

       catch (SQLException e)

       {

           System.out.println("SQLException exception: ");

           System.out.println("Message:....." + e.getMessage());

           System.out.println("SQLState:...." + e.getSQLState());

           System.out.println("Vendor Code:." + e.getErrorCode());

           e.printStackTrace();

       }

       catch (Exception ex)

       {

           System.out.println("An exception other than an SQLException was thrown: ");

           ex.printStackTrace();

       }

   }

}
SQlState is a method in Java.sql package. It returns SQL state in five digit number. These five digit numbers tell about the status of the SQL statements. The SQLSTATE values consists of two fields. The class, which is the first two characters of the string, and the subclass, which is the terminating three characters of the string.
For example:
08001 where 08 is the class and 001 is the subclass. 08 class means connection exception and 001 means client unable to establish connection.
CallableStatement interface
CallableStatement interface - It is an interface which is the sub interface of Statement Interface....
Batch updates using CallableStatement interface.
Batch updates using CallableStatement interface - Batch update is the ability to add more than one statement at once...
Architecture of a Servet package
Architecture of a Servet package - The javax.servlet package provides interface and classes for writing servlets....
Post your comment