Reading:  

Getting started with JDBC


Intro to creating Query Statements with JDBC

After establishing connection with the database, JDBC contains interface like Callable Statement, Prepared Statement and Statement which has properties and method that will allow interaction with PL/SQL and SQL commands to receive and send data into the database.

Interfaces

Recommended Use

Callable Statement

It is used while accessing the stored procedure from database. It accepts input parameter runtime.

Prepared Statement

It is used while calling SQL Statements many times from database. It accepts input parameter runtime.

Statement

It is used while calling static SQL Statement. It doesn’t accept input parameter runtime.

 

The Statement Objects: The statement object involves two main parts Creating Statement Object and Closing Statement Object.


Statement stmtmnt = null;
try {  
stmtmnt = conn1.createStatement( );  
. . .
}catch (SQLException e) {  
. . .
}
finally {  
. . .
}


After Creating Statement Object the SQL Statement it can be executed one of three execution methods: 

  • Execute boolean (String SQL): If result set object is retrieved then True Boolean value is returned or else False.
  • int executeUpdate(String SQL) : The number of rows affected by the execution of SQL statement is returned. UPDATE, INSERT or DELETE statement are the example of using this.
  • executeQuery ResultSet (String SQL) : ResultSet Object is returned. The SELECT statement results returned with it.

After the result set is returned in any of the three execution method statement object must be closed. The Close will ensure cleanup of the object. Below code explains it:


Statement stmtmnt = null;
try {  
stmtmnt = conn1.createStatement( );  
. . .
}catch (SQLException e) {  
. . .
}
finally {  
stmtmnt.close();
}

The Prepared Statement Objects: The Prepared Statement provides additional functionality like argument supplying dynamically. The ? allow parameter passing into the statement.The methods execute(), executeQuery() and  executeUpdate() works well with Prepared Statement Objects.

 
PreparedStatement pstmtmnt = null;
try {  
String SQL = "Update USER SET user_age = ? WHERE user_id = ?";  
pstmtmnt = conn.prepareStatement(SQL);  
. . .
}
catch (SQLException e) {  
. . .
}
finally {  
. . .
}


After the result set is returned the Prepared Statement object should be closed. 

PreparedStatement pstmtmnt = null;
try {
String SQL = "Update USER SET user_age = ? WHERE user_id = ?";
pstmtmnt = conn.prepareStatement(SQL);
. . .
}
catch (SQLException e) {
. . .
}
finally {
pstmtmnt.close();
}

 

 

 

The Callable Statement Objects: This can be used while calling Stored Procedure from the database.

CREATE OR REPLACE PROCEDURE get_UserName    
(USER_ID IN NUMBER, USER_FIRST OUT VARCHAR) AS
BEGIN  
  SELECT User_first
  INTO USER_FIRST  
  FROM USER  
  WHERE USER_ID = USER_ID;
END;


The CallableStatement object uses three type of parameter: OUT, IN and INOUT. 

Parameter

Description

OUT

The retrieved Value is outputted from the procedure.

IN

The input Value is inputted into the procedure.

INOUT

Both IN and OUT is passed into the parameter value.

 

The Connection.prepareCall() method is used for initiating it. 

CallableStatement cstmtmnt = null;
try {
String SQL = "{call get_UserName (?, ?)}";
cstmtmnt = conn1.prepareCall (SQL);
. . .
}
catch (SQLException e) {
. . .
}
finally {
. . .
}

  

After the execution method statement object must be closed. The Close will ensure cleanup of the object. Below code explains it:

CallableStatement cstmtmnt = null;
try {
String SQL = "{call get_UserName (?, ?)}";
cstmtmnt = conn.prepareCall (SQL);
. . .
}
catch (SQLException e) {
. . .
}
finally {
cstmtmnt.close();
}
 

 

 

 

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!