Home Tutorial Java Jdbc JDBC Class Not Found Exception


Share on Google+Share on Google+
JDBC Class Not Found Exception
Posted on: October 20, 2010 at 12:00 AM
In this tutorial you will learn how to handle ClassNotFoundException and SQLException

JDBC ClassNotFoundException And SQLException


In JDBC When we try to load a database driver which is not in the driver manager class or wrong driver then it throws  ClassNotFoundException. You should must load the database driver with its fully qualified class name. The driver name is actually a fully qualified class name of a parrticular database application. To load a database driver Class.forName(""); method is used. this method must be present within a proper try catch block.

     catch(ClassNotFoundException e)

If the database driver class not found then it throws ClassNotFoundException. The catch block must handle this class not found exception.


The SQLException is raised when there is any database access error. Generally it arises when there is any error in query string. Therefore whenever you are running  executeQuery(), exeuteUpdate() method, you must use it within try catch block and catch block should handle SQLException, or method within which this method is called should throws a SQLException.

try {
			// Getting Connection
			con = DriverManager.getConnection(conUrl + databaseName, usrName,
			// Creating Statement for query execution
			stmt = con.createStatement();
			// creating Query String
			String query = "SELECT * FROM student";
			// excecuting query
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				// Didplaying data of tables
				System.out.println("Roll No " + rs.getInt("RollNo") + ", Name "
						+ rs.getString("Name") + ", Course "
						+ rs.getString("Course") + ", Address "
						+ rs.getString("Address"));
		} catch (SQLException e) {
		}catch (Exception e) {
An example which handle both above exception is given below.......


package roseindia.net;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCCOnnectionExample {
	Connection connection = null;

	public JDBCCOnnectionExample() {
		try {
		} catch (ClassNotFoundException e) {

	public Connection createConnection() {
		Connection con = null;
		if (connection != null) {
			System.out.println("Cant create a connection");
		} else {
			try {
				con = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/student", "root",
				System.out.println("Connection created Successfully");
				DatabaseMetaData dbMetaData = con.getMetaData();
				ResultSet dbInfo = dbMetaData.getCatalogs();
				System.out.println("Getting Concurrency of MetaData");
			} catch (SQLException e) {
		return con;

	public static void main(String[] args) throws SQLException {
		JDBCCOnnectionExample jdbccOnnectionExample = new JDBCCOnnectionExample();
		Connection conn = jdbccOnnectionExample.createConnection();

When you run this application it will display message as shown below:

Connection created Successfully
Getting Concurrency of MetaData

Download this example code


Related Tags for JDBC Class Not Found Exception:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 20, 2010

Recommend the tutorial

Advertisements Advertisements