AdvancedJavaProgramming-SLIDES01-UNIT2-FP2005-Ver 1.0
AdvancedJavaProgramming-SLIDES01-UNIT2-FP2005-Ver 1.0
(J2EE LC)
Unit -2, JDBC
Session Plan
– Different drivers
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 2
Technologies Ltd Version 1.00
JDBC
The JDBC (Java Database Connectivity) API helps a Java program to access
a database in a standard way
JDBC is a specification that tells the database vendors how to write a driver
program to interface Java programs with their database
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 3
Technologies Ltd Version 1.00
JDBC
All related classes and interfaces are present in the java.sql package
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.00
JDBC Drivers
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.00
Type1 Driver (JDBC-ODBC bridge driver)
JDBC API
ODBC Driver
DataBase
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.00
Type2 Driver (Native-API driver )
JDBC API
DataBase
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 7
Technologies Ltd Version 1.00
Type3 Driver (Network-protocol driver )
Calling Java Application
JDBC API
Network-Protocol driver
(Type III Driver)
MiddleWare
(Application Server)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.00
Type4 Driver (Native-protocol driver )
JDBC API
DataBase
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 9
Technologies Ltd Version 1.00
Database interaction
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.00
Statement
Three kinds :
– Statement
– Execute simple SQL without parameters
– PreparedStatement
– Used for pre-compiled SQL statements with or without parameters
– CallableStatement
– Execute a call to a database stored procedure or function
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 11
Technologies Ltd Version 1.00
JDBC - classes and interfaces
DriverManager class -
– Manages all the JDBC Drivers that are loaded in the memory
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.00
JDBC - classes and interfaces
– registerDriver(java.sql.Driver)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
– A connection session includes the SQL statements that are executed and the
results that are returned over that connection.
– A single application can have one or more connections with a single database,
or it can have many connections with many different databases.
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 15
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 16
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd...)
Statement interface - defines methods that are used to interact with
database via the execution of SQL statements.
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
– getByte(int) - Get the value of a column in the current row as a Java byte.
– getDouble(int) - Get the value of a column in the current row as a Java double.
– getInt(int) - Get the value of a column in the current row as a Java int.
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 18
Technologies Ltd Version 1.00
Using Statement and ResultSet
import java.sql.*;
java.sql.*;
class JDBCTest{
JDBCTest{
public static void main(String args[])
args[]) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
("oracle.jdbc.driver.OracleDriver");
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@DB,
("jdbc:oracle:thin:@DB,
IPaddress:port_no:host string",“
string",“uid",“
uid",“password");
password");
Statement statement = connection.createStatement();
connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from
Student");
while(resultSet.next()){
System.out.println(resultSet.getInt("ClassNo"));
System.out.println(resultSet.getInt("ClassNo"));
}
}
catch(Exception exception) {
System.out.println(exception)
System.out.println(exception)
}
}
}
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 19
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 20
Technologies Ltd Version 1.00
Using PreparedStatement
import java.sql.*;
java.sql.*;
class PreparedStatementTest{
PreparedStatementTest{
public static void main(String args[])
args[]) throws Exception{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection(“url",
url",
“UID", “password");
PreparedStatement preparedStatement =
connection.prepareStatement("select * from Emp where ename=?");
ename=?");
preparedStatement.setString(1, 7521);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
while(resultSet.next()){
System.out.println(resultSet.getString("ename"));
("ename"));
}
}
catch(Exception exception){
System.out.println(exception);
System.out.println(exception);
}
}
}
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 21
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
callableStatement.setInt(50);
callableStatement.execute();
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 23
Technologies Ltd Version 1.00
Using CallableStatement
Example - Calling a stored procedure named GetSalary. The procedure queries on the
Employee table and returns the salary of an employee. It has one input parameter that
takes the EmpCode and an out parameter that returns the salary
CallableStatement callableStatement =
connection.prepareCall("begin GetSalary(?,?);
GetSalary(?,?); end;");
callableStatement.setInt(1,29418);
// OUT parameters must be registered.
callableStatement.registerOutParameter(2,Types.DOUBLE);
callableStatement.execute();
System.out.println("Salary : " + callableStatement.getDouble(2));
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 24
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)
ResultSetMetaData Interface - holds information on the types and
properties of the columns in a ResultSet. Provides information about the
database as a whole. Constructed from the Connection object
– getColumnType()
– getColumnLabel(count)
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.00
Transaction Management using JDBC
...
connection.setAutoCommit(false);
(false); //by default it is true
try{
//Statements
connection.commit();
}
catch(Exception exception){
connection.rollback();
}
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 26
Technologies Ltd Version 1.00
Summary
– Different drivers
ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.00