Reading:  

Getting started with JDBC


Example 1

The JDBC Application involves opening database connection, SQL Query execution and result display.

Creating JDBC Application:

A JDBC application has steps below:

  • Packages Import
  • JDBC driver Register
  • Open Connection
  • Query Execution
  • Result set data Extraction
  • Environment Clean up

 

Example:

Below code can be executed once the database and environment setup is done in previous chapter. Save the example as JavaFirstExample.java compile and run:

//STEP 1. Packages Import
import java.sql.*;

public class JavaFirstExample{

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/Users";
   static final String USER = "my_user";
   static final String PASS = "my_password";
   public static void main(String[] args) {
   Connection connn = null;
   Statement stmt1 = null;
   try{
      //JDBC driver Register
      Class.forName("com.mysql.jdbc.Driver");

      //Open Connection
      connn = DriverManager.getConnection(DB_URL,USER,PASS);

      //Query Execution
      Stmt1 = connn.createStatement();
      String sql1;
      sql1 = "SELECT user_id, user_first, user_last, user_age FROM USER";
      ResultSet rs = stmt.executeQuery(sql1);

      //STEP 5: Result set data Extraction
      while(rs.next()){
         int id_1  = rs.getInt("user_id");
         int age_1 = rs.getInt("user_age");
         String first_1 = rs.getString("user_first");
         String last_2 = rs.getString("user_last");

         //Output results
         System.out.print("USER System ID: " +user_id);
         System.out.print(",USER Current Age: " +user_age);
         System.out.print(",USER First name: " +user_first);
         System.out.println(",USER Last name: " +user_last);
      }
      //STEP 6:  environment Clean-up
      rs.close();
      stmt1.close();
      connn.close();
   }catch(SQLException se){
      se.printStackTrace();
   }catch(Exception e){
      e.printStackTrace();
   }finally{
 
      try{
         if(stmt1!=null)
            stmt1.close();
      }catch(SQLException se2){
      }
      try{
         if(connn!=null)
            connn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }

}
}

Compile example as below:

C:\>javac JavaFirstExample.java

C :\>

Run JavaFirstExample results as shown:

C:\>java JavaFirstExample 

USER System ID: 100, USER Current Age: 5, USER First name: Yuvraaj, USER Last Name: C
USER System ID: 101, USER Current Age: 2, USER First name: Nimrit,USER Last Name: 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!