JDBC Exception

In this Tutorial we want to describe you a code that help you to understand JDBC Exception.

JDBC Exception

JDBC Exception

     

The Exception are the set of condition that occurred when an abnormal condition try to interrupt the normal flow of the code during the execution of program. Exception Occurred in JDBC Connectivity when a connection object do not find a suitable driver to connect a url-database.

Understand with Example

The Tutorial illustrates an example from JDBC Exception. In this Tutorial we want to describe you a code that show an exception in JDBC.For this we have a class JdbcException.

  The first step is to import a package name java.sql include a definition for all the classes that provides you network interface to communicate between front end and back end  application. The next is to load the driver by calling class. for name( ) with driver class passed as argument.

  Driver.getConnection ( ) : This is used to built connection between url and database. In case there is an exception in try block, the subsequent catch block caught and handle the exception, The exception show you no suitable driver is found.

  con.create Statement ( ) :  This is used to create a Sql object. An sql object is used to send and execute the query in backend database.

   executeQuery ( ):  This is used to return the result set obtained from the record set of the database. The select statement is used to retrieve the result set from the database.

  In this program code, the exception occurred in try block, therefore the subsequent catch block show the exception that a suitable driver do not  found using print ln.Once the suitable driver is found the print ln print and display the element of no,name,id and dob.

  rs.getString ( ) -The Result set object call a get String ( ),returns you a record set into a formatted string element.

 

JdbcExceptions.java
import java.sql.*;

public class JdbcExceptions {

    public static void main(String args[]) throws Exception {


        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String url = new String();
        String db = new String();
        String driver = "com.mysql.jdbc.Driver";
        String user = new String();
        String pass = new String();

        Class.forName(driver);

        try {
            con = DriverManager.getConnection(url + db, user, pass);
        } catch (Exception e) {
            System.err.println(e);

            url = "jdbc:mysql://localhost:3306/";
            db = "komal";
            user = "root";
            pass = "root";

            con = DriverManager.getConnection(url + db, user, pass);
            con.setAutoCommit(false);
            st = con.createStatement();

            String sql = "select * from person";
            rs = st.executeQuery(sql);

            System.out.println("no.  \tName      \tDob");
            while (rs.next()) {
                System.out.print(rs.getString("id") + "   \t");
                System.out.print(rs.getString("cname") + "     \t");
                System.out.println(rs.getDate("dob"));
            }
        }
    }
}

Output

java.sql.SQLException: No suitable driver found for 
no.  	Name      	Dob
1   	Girish     	1984-06-02
2   	komal     	1984-10-27

Download code