0
import java.io.*;
import java.sql.*;
public class jdbcdemo
{
    public static void main(String args[])throws IOException
    {
        Connection con=null;
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:ebbill","","");
            System.out.println("ConnectionSuccesfull");
            Statement stat=con.createStatement();
            int result=stat.executeUpdate("Insert into tableone values(5,'siva',50,6000)");
            System.out.println("Record Inserted");
            stat.close();
            con.close();
        }
        catch(ClassNotFoundException e)
        {
            System.out.println("Exception:"+e.getMessage());
        }
        catch(SQLException e)
        {
            System.out.println("Exception:"+e.getMessage());
        }
    }
}

I dont understand what happens in this try block. Please explain briefly in simpleton terms.

This line Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); doesnt work in latest java. It works only in java 6 or previous versions.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
Bharat
  • 1
  • 1
  • Please refer to [this answer](https://stackoverflow.com/a/22996068/4039840). The question about the try block purpose: for the first `catch` it is for catch problem about class not found exception. the second `catch` is for catching problem with the sql you are trying to run. – TuyenNTA Mar 14 '18 at 08:12

3 Answers3

2

The line

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

try to load the class sun.jdbc.odbc.JdbcOdbcDriver from the classpath of your application. If the class is not found the exception ClassNotFoundException is thrown.

Since java 8 this class (sun.jdbc.odbc.JdbcOdbcDriver) has been removed as per oracle documentation:

Status of the JDBC-ODBC Bridge The JDBC-ODBC Bridge should be considered a transitional solution; it will be removed in JDK 8. In addition, Oracle does not support the JDBC-ODBC Bridge. Oracle recommends that you use JDBC drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

The purpose of a try block is to handle the error prone code i.e. the code which is inside a try block, tend to throw exceptions during run-time.

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con=DriverManager.getConnection("jdbc:odbc:ebbill","","");
    Statement stat=con.createStatement();

Above piece of code can result in an error when something is not right and it leads to the termination of JVM (in other words the application would have an ERROR). In order to handle the exception caused during the run-time, we include the error prone code in try block and a remedy in the catch block to handle the exception scenarios.

Your code tries to load the class "sun.jdbc.odbc.JdbcOdbcDriver" . If the class is not found, then ClassNotFoundException exception is thrown.

Jayanth
  • 746
  • 6
  • 17
0

In simpleton terms, within the try block, the code attempts to establish a connection to a database. It then tries to insert a record into a table within that database, before disconnecting from the database.

Putting it within a try block and having the catch blocks below, means that if something goes wrong in your code at any point, then the code within the corresponding catch block relevant to the particular error that occurs is executed.

In your case, you have two catch blocks, ClassNotFoundException, and SQLException.

Typically, a ClassNotFoundException may occur when the below lines are executed:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ebbill","","");

And a common cause might be that the JDBC Driver is not installed on your project.

An SQLException might be thrown when the below lines are executed:

Statement stat=con.createStatement();
        int result=stat.executeUpdate("Insert into tableone values(5,'siva',50,6000)");

And would typically occur because something has gone wrong on the database side when trying to access the database or with the SQL query.

Other exceptions could be thrown, however these would result in your program falling over with less useful error logging and no logic to handle the errors. You could just catch Exception (which would catch any exception thrown) however in the real world this can be considered bad practice as you should know what types of exception may be thrown and handle them appropriately.

In your example, let's suppose that for some reason, we know that occasionally, an SQL Exception may get thrown in an edge case scenario when your application is running. Let's also assume it is not programatically feasible to write code to prevent this from happening, and so we instead just want to log the error, provide a message to the user and the continue with the normal flow of execution for running the program.

If however, a ClassNotFoundException is thrown, let's assume we do not want to continue with the normal running of the application (we would not be able to). So we may instead want to log the error, and output a message to the user advising them to contact their system administrator.

So here we have two different responses to the two different types of error, hence the need for the two catch blocks catching the different types of error.

Macca Rooney
  • 13
  • 1
  • 8