JDBC Exception

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

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC Exception

Posted on: November 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC Exception   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads