Java & MySQL - Exceptions Handling

Exception handling allows you to handle exceptional conditions such as program-defined errors in a controlled fashion.
When an exception condition occurs, an exception is thrown. The term thrown means that current program execution stops, and the control is redirected to the nearest
applicable catch clause. If no applicable catch clause exists, then the program's execution ends.
JDBC Exception handling is very similar to the Java Exception handling but for JDBC, the most common exception you'll deal with is java.sql.SQLException.
An SQLException can occur both in the driver and the database. When such an exception occurs, an object of type SQLException will be passed to the catch clause.
The passed SQLException object has the following methods available for retrieving additional information about the exception −
By utilizing the information available from the Exception object, you can catch an exception and continue your program appropriately. Here is the general form of a try block −
try {
   // Your risky code goes between these curly braces!!!
}
catch(Exception ex) {
   // Your exception handling code goes between these 
   // curly braces
}
finally {
   // Your must-always-be-executed code goes between these 
   // curly braces. Like closing database connection.
}
Study the following example code to understand the usage of try....catch...finally blocks.
This code has been written based on the environment and database setup done in the previous chapter.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class TestApplication {
   static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
   static final String USER = "guest";
   static final String PASS = "guest123";
   static final String QUERY = "{call getEmpName (?, ?)}";

   public static void main(String[] args) {
      // Open a connection
      try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
         CallableStatement stmt = conn.prepareCall(QUERY);
      ) {		      
         // Bind values into the parameters.
         stmt.setInt(1, 1);  // This would set ID
         // Because second parameter is OUT so register it
         stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
         //Use execute method to run stored procedure.
         System.out.println("Executing stored procedure..." );
         stmt.execute();
         //Retrieve employee name with getXXX method
         String empName = stmt.getString(2);
         System.out.println("Emp Name with ID: 1 is " + empName);
      } catch (SQLException e) {
         e.printStackTrace();
      } 
   }
}
Now let us compile the above example as follows −
C:\>javac TestApplication.java
C:\>

When you run TestApplication, it produces the following result if there is no problem, otherwise the corresponding error would be caught and error message would be displayed −
C:\>java TestApplication
Executing stored procedure...
Emp Name with ID: 1 is Zara
C:\>


    16 Lectures 
     2 hours 

 Malhar Lathkar

    19 Lectures 
     5 hours 

 Malhar Lathkar

    25 Lectures 
     2.5 hours 

 Anadi Sharma

    126 Lectures 
     7 hours 

  Tushar Kale

    119 Lectures 
     17.5 hours 

 Monica Mittal

    76 Lectures 
     7 hours 

 Arnab Chakraborty
 Print
 Add Notes
 Bookmark this page