0% found this document useful (0 votes)
12 views27 pages

AdvancedJavaProgramming-SLIDES01-UNIT2-FP2005-Ver 1.0

The document discusses Java Database Connectivity (JDBC) and how to connect to and interact with databases from Java programs. It describes the JDBC API and driver types, how to establish a connection using the DriverManager class, and how to execute SQL statements and process result sets using Statement, PreparedStatement, and ResultSet interfaces and classes. The goal is to provide an overview of setting up and using JDBC to access databases from Java applications in a standard way.

Uploaded by

loga prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views27 pages

AdvancedJavaProgramming-SLIDES01-UNIT2-FP2005-Ver 1.0

The document discusses Java Database Connectivity (JDBC) and how to connect to and interact with databases from Java programs. It describes the JDBC API and driver types, how to establish a connection using the DriverManager class, and how to execute SQL statements and process result sets using Statement, PreparedStatement, and ResultSet interfaces and classes. The goal is to provide an overview of setting up and using JDBC to access databases from Java applications in a standard way.

Uploaded by

loga prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Advanced Java Programming

(J2EE LC)
Unit -2, JDBC
Session Plan

Java Data Base Connectivity


– JDBC API

– Different drivers

– Set up a connection to a database from Java

– Create a database application

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

A Driver written according to this standard is called the JDBC Driver

All related classes and interfaces are present in the java.sql package

All JDBC Drivers implement the interfaces of java.sql

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.00
JDBC Drivers

There are 4 types of drivers --Type1, Type2, Type3,Type4

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.00
Type1 Driver (JDBC-ODBC bridge driver)

Calling Java Application

JDBC API

JDBC Driver Manager

JDBC - ODBC Bridge


(Type I Driver)

ODBC Driver

Database Library APIs

DataBase

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.00
Type2 Driver (Native-API driver )

Calling Java Application

JDBC API

JDBC Driver Manager

Native - API driver


(Type II Driver)

Database Library APIs

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

JDBC Driver Manager

Network-Protocol driver
(Type III Driver)

MiddleWare
(Application Server)

Different DataBase Vendors

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.00
Type4 Driver (Native-protocol driver )

Calling Java Application

JDBC API

JDBC Driver Manager

Native - Protocol driver


(Type 4 Driver)

direct calls using specific


database protocol

DataBase

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 9
Technologies Ltd Version 1.00
Database interaction

The steps involved in a database interaction are:


– Loading the specific driver

– Making a connection to the database

– Sending SQL statements to the database

– Processing the results

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.00
Statement

A statement object is used to send SQL statements to a database.

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

– Helps in dynamic loading of Drivers

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.00
JDBC - classes and interfaces

Methods in DriverManager class -


– getConnection() : to establish a connection to a database.
• Connection getConnection(String url, Properties info)

• Connection getConnection(String url)

• Connection getConnection(String url, String userID, String password)

– registerDriver(java.sql.Driver)

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)

Please refer to Notes page for more explanation on previous slide

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)

Connection interface - defines methods for interacting with the database


via the established connection.
– A connection object represents a connection with a database.

– 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…)

The different methods of Connection interface are:


– close() - closes the database connection

– createStatement() - creates an SQL Statement object

– prepareStatement() - creates an SQL PreparedStatement object.


(PreparedStatement objects are precompiled SQL statements)

– prepareCall() - creates an SQL CallableStatement object using an SQL string.

(CallableStatement objects are SQL stored procedure call statements)

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.

The different methods are:


– executeQuery(String sql) - executes an SQL statement (SELECT) that queries
a database and returns a ResultSet object.

– executeUpdate(String sql) - executes an SQL statement (INSERT,UPDATE,or


DELETE) that updates the database and returns an int, the row count associated
with the SQL statement

– execute(String sql) - executes an SQL statement that is written as String object

– getResultSet() - used to retrieve the ResultSet object

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)

ResultSet Interface - maintains a pointer to a row within the tabular results.


The next() method is used to successively step through the rows of the
tabular results.

The different methods are:


– getBoolean(int) - Get the value of a column in the current row as a Java
boolean.

– 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…)

PreparedStatement interface -- helps us to work with precompiled SQL


statements

Precompiled SQL statements are faster than normal statements

So, if a SQL statement is to be repeated, it is better to use PreparedStatement

Some values of the statement can be represented by a ? character which can


be replaced later using setXXX method

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 interface -- helps us to call stored procedures and


functions
CallableStatement callableStatement = connection.prepareCall(“execute proc ?”);

callableStatement.setInt(50);

callableStatement.execute();

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.00
JDBC - classes and interfaces (Contd…)

The out parameters are to be registered


callableStatement.registerOutParameter(int parameterIndex, int SQLType);

To get the value stored in the out parameter--


callableStatement.getXXX(int parameterIndex);

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

The different methods are:


– getColumnName()

– getColumnType()

– getColumnLabel(count)

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.00
Transaction Management using JDBC

By default, auto commit mode of the connection reference is set to true

A transaction can be done as follows using methods of the Connection


interface

...
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

Java Data Base Connectivity


– JDBC API

– Different drivers

– Set up a connection to a database from Java

– Create a database application

ER/CORP/CRS/LA22/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.00

You might also like