Reading:  

Getting started with JDBC


How to handle exceptions

Exception handling allows handling exceptional conditions like  program-defined errors in controlled manner.

Program defined error is handled by Exception handling in controlled manner.Exception is thrown when exception condition is caught. Java exception handling and JDBC exception handling are similar.

SQLException Methods:

A SQLException occur both in driver and database. When exception occur object of type SQLException get passed to catch clause.

Method

Description

getErrorCode( )

Gets error number associated with exception.

getMessage( )

Gets JDBC driver's error message for error handled by driver

getSQLState( )

Gets XOPEN string.

getNextException( )

Gets next Exception object in exception chain.

printStackTrace( )

Prints current exception.

printStackTrace(PrintStream s)

Prints throwable

General Try and Catch Block:-

try {
// code goes here
}
catch(Exception ex) {
// exception handling code
}
finally {
// it gets executed everytime.
}

 

import java.sql.*;

public class JDBCExample {
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/USERLOYEE";

   static final String USER_Name = "username";
   static final String PASS = "password1";
   
   public static void main(String[] args) {
   Connection conn1 = null;
   try{
      Class.forName("com.mysql.jdbc.Driver");

      
      Conn1 = DriverManager.getConnection(DB_URL,USER,PASS);

      Statement stmtmnt = conn1.createStatement();
      String sql_1;
      Sql_1 = "SELECT id, first, last, age FROM USER";
      ResultSet rs_1 = stmtmnt.executeQuery(sql_1);

      //STEP 5: data Extract
      while(rs_1.next()){
         //Based on column name retrieve 
         int e_id  = rs_1.getInt("user_id");
         int e_age = rs_1.getInt("user_age");
         String e_first = rs_1.getString("user_first");
         String e_last = rs_1.getString("user_last");

         // values
         System.out.print("user ID: " + user_id);
         System.out.print(",user Age: " + user_age);
         System.out.print(",user First: " + user_first);
         System.out.println(",user Last: " + user_last);
      }
      //release resources
      rs_1.close();
      stmtmnt.close();
      conn1.close();
   }catch(SQLException se){
      // JDBC Handle errors 
      se.printStackTrace();
   }catch(Exception e){
      // Class.forName Handle errors 
      e.printStackTrace();
   }finally{
      // release resources
      try{
         if(conn1!=null)
            conn1.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
     }
   }
}

 

Compile and run above example as follows:

C:\>javac ExampleJDBC.java
C:\>java ExampleJDBC 
 

user ID: 100, user Age: 5, user First: Yuvraaj, user Last: C

user ID: 101, user Age: 2, user First: Nimrit, user Last: K

 

 

 

 

Description

This tutorial is focused on getting you started with JDBC 4.1. This tutorial has following parts

  1. Introduction
  2. SQL Syntax
  3. Setting up Environment
  4. Getting started with some samples
  5. Connecting to database
  6. Statements
  7. Result Sets
  8. Data Types
  9. Transactions
  10. Exception handling
  11. Batch Processing
  12. Streaming Data
  13. More JDBC Examples

Leave your feedback to help us improve next time. Also let us know if you see any errors that needs to be corrected



Prerequisites

Understanding of Java language a must to understand various things in this tutorial

Audience

Absolute beginners who wants to get started with JDBC

Author: Subject Coach
Added on: 8th Mar 2015

You must be logged in as Student to ask a Question.

None just yet!