In this tutorial you will learn how to handle ClassNotFoundException and SQLException
In this tutorial you will learn how to handle ClassNotFoundException and SQLExceptionClassNotFoundException
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.
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e)
{
System.out.println(e.toString());
}
If the database driver class not found then it throws ClassNotFoundException. The catch block must handle this class not found exception.
SQLException
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, usrPass); // 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) { System.out.println(e.toString()); }catch (Exception e) { System.out.println(e.toString()); }
An example which handle both above exception is given below.......
JDBCCOnnectionExample.java
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 { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } } 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", "root"); System.out.println("Connection created Successfully"); DatabaseMetaData dbMetaData = con.getMetaData(); ResultSet dbInfo = dbMetaData.getCatalogs(); System.out.println("Getting Concurrency of MetaData"); System.out.println(dbInfo.getConcurrency()); } catch (SQLException e) { System.out.println(e.toString()); } } return con; } public static void main(String[] args) throws SQLException { JDBCCOnnectionExample jdbccOnnectionExample = new JDBCCOnnectionExample(); Connection conn = jdbccOnnectionExample.createConnection(); conn.close(); } }
Connection created Successfully Getting Concurrency of MetaData 1007 |