Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

JDBC Exception

In this Tutorial we want to describe you a code that help you to understand 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

                         

» View all related tutorials
Related Tags: c ant jdbc tutorial ria this example to exam fetch e des from in m nt wan j dbc db

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.