You are on page 1of 13

JDBC

DATABASE HANDLING USING


JDBC
JDBC stands for Java database connectivity.
A standard API for all Java programs to connect to
databases.
Core API java.sql.
JDBC steps are as follows:
Load the driver
Establish connection
Creating and executing statements
Obtain result
Close the connection

Load the Driver


The driver is loaded with the help of a static
method
Class.forName(drivername)
All the JDBC drivers have been classified into
four categories:
Type 1: JDBC ODBC bridge driver
Type 2: Native-API/partly Java driver
Type 3: Net-protocol driver
Type 4: Pure Java driver

Driver Names

JDBC ODBC Bridge Driver

Native API/ Partly Java


Driver

Type 3: Net Protocol Driver

Type 4: Pure Java Driver

Establish a Connection
A connection to the database is established using the
static method getConnection(databaseUrl) of the
DriverManager class.
The DriverManager class is class for managing JDBC
drivers.
The database URL takes the following shape
jdbc:subprotocol:subname.
If any problem occurs during accessing the database,
an SQLException is generated, else a Connection object
is returned which refers to a connection to a database.
Connection is actually an interface in java.sql package.
Connection
con=DriverManager.getConnection(databaseUrl);

Create Statement
The connection is used to send SQL statements to
the database.
Three interfaces are used for sending SQL
statements to databases
Statement and its two sub-interfaces,
PreparedStatement
and
Callable
Statement.
Three methods of the Connection object are used
to return objects of these three statements.
A Statement object is used to send a simple SQL
statement to the database with no parameters.
Statement stmt = con.createStatement();

Create Statement (contd.)


A
PreparedStatement
object
sends
precompiled
statements to the databases with or without IN
parameters.
If n rows need to be inserted, then the same statement
gets compiled n number of times.
So to increase efficiency, we use precompiled
PreparedStatement.
only the values that have to be inserted are sent to the
database again and again.
PreparedStatement ps = con.prepareStatement(String query);

A CallableStatement object is used to call stored


procedures.
CallableStatement cs = con.prepareCall(String query);

Execute Query
Three methods are used
ResultSet executeQuery(String sqlQuery) throws SQLException
int executeUpdate(String sqlQuery) throws SQLException
boolean execute(String sqlQuery) throws SQLException

executeQuery is used for executing SQL statements


that return a single ResultSet, e.g. a select statement.
The rows fetched from database are returned as a single
ResultSet object. For example,
ResultSet rs=stmt.executeQuery(select * from emp);

executeUpdate is used for DDL


and DML SQL
statements like insert,update, delete, and create.
returns an integer value for DML to indicate the number of
rows affected and 0 for DDL statements which do not return
anything.

Closing Connection
To terminate the connection with the
database, close() method is used.
connection.close();

You might also like