0% found this document useful (0 votes)
128 views

Java JDBC Tutorial

This document provides information about connecting Java applications to databases using JDBC. It discusses the different types of JDBC drivers, including JDBC-ODBC bridge drivers, native API drivers, network protocol drivers, and thin drivers. It also outlines the 5 steps to connect to a database in Java: 1) register the driver class, 2) create the connection object, 3) create the statement object, 4) execute queries, and 5) close the connection. An example is given connecting to both Oracle and MySQL databases.

Uploaded by

Amritranjan Das
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Java JDBC Tutorial

This document provides information about connecting Java applications to databases using JDBC. It discusses the different types of JDBC drivers, including JDBC-ODBC bridge drivers, native API drivers, network protocol drivers, and thin drivers. It also outlines the 5 steps to connect to a database in Java: 1) register the driver class, 2) create the connection object, 3) create the statement object, 4) execute queries, and 5) close the connection. An example is given connecting to both Oracle and MySQL databases.

Uploaded by

Amritranjan Das
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Java JDBC Tutorial

Java JDBC is a java API to connect and execute query with the database. JDBC API uses
jdbc drivers to connect with the database.

Why use JDBC


Before JDBC, ODBC API was the database API to connect and execute query with the
database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform
dependent and unsecured). That is why Java has defined its own API (JDBC API) that
uses JDBC drivers (written in Java language).

Do You Know
o

How to connect Java application with Oracle and Mysql database using JDBC?

What is the difference between Statement and PreparedStatement interface?

How to print total numbers of tables and views of a database using JDBC ?

How to store and retrieve images from Oracle database using JDBC?

How to store and retrieve files from Oracle database using JDBC?

What is API
API (Application programming interface) is a document that contains description of all
the features of a product or software. It represents classes and interfaces that software
programs can follow to communicate with each other. An API can be created for
applications, libraries, operating systems, etc

JDBC Driver
1. JDBC Drivers
1.

JDBC-ODBC bridge driver

2.

Native-API driver

3.

Network Protocol driver

4.

Thin driver

JDBC Driver is a software component that enables java application to interact with
the database.There are 4 types of JDBC drivers:
1. JDBC-ODBC bridge driver
2. Native-API driver (partially java driver)
3. Network Protocol driver (fully java driver)
4. Thin driver (fully java driver)

1) JDBC-ODBC bridge driver


The JDBC-ODBC bridge driver uses ODBC driver to connect to the database. The
JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls.
This is now discouraged because of thin driver.

Advantages:

easy to use.

can be easily connected to any database.

Disadvantages:

Performance degraded because JDBC method call is converted into the ODBC
function calls.

The ODBC driver needs to be installed on the client machine.

2) Native-API driver
The Native API driver uses the client-side libraries of the database. The driver
converts JDBC method calls into native calls of the database API. It is not written
entirely in java.

Advantage:

performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:

The Native driver needs to be installed on the each client machine.

The Vendor client library needs to be installed on client machine.

3) Network Protocol driver


The Network Protocol driver uses middleware (application server) that converts JDBC
calls directly or indirectly into the vendor-specific database protocol. It is fully written in
java.

Advantage:

No client side library is required because of application server that can perform
many tasks like auditing, load balancing, logging etc.

Disadvantages:

Network support is required on client machine.

Requires database-specific coding to be done in the middle tier.

Maintenance of Network Protocol driver becomes costly because it requires


database-specific coding to be done in the middle tier.

4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol.
That is why it is known as thin driver. It is fully written in Java language.

Advantage:

Better performance than all other drivers.

No software is required at client side or server side.

Disadvantage:

Drivers depends on the Database.

5 Steps to connect to the database in java


1. 5 Steps to connect to the database in java
1.

Register the driver class

2.

Create the connection object

3.

Create the Statement object

4.

Execute the query

5.

Close the connection object

There are 5 steps to connect any java application with the database in java using
JDBC. They are as follows:
Register the driver class

Creating connection

Creating statement

Executing queries

Closing connection

1) Register the driver class


The forName() method of Class class is used to register the driver class. This method
is used to dynamically load the driver class.

Syntax of forName() method


1.

public static void forName(String className)throws ClassNotFoundException

Example to register the OracleDriver class


1.

Class.forName("oracle.jdbc.driver.OracleDriver");

2) Create the connection object


The getConnection() method of DriverManager class is used to establish connection
with the database.

Syntax of getConnection() method


1.
2.
3.

1) public static Connection getConnection(String url)throws SQLException


2) public static Connection getConnection(String url,String name,String passwor
d)

throws SQLException

Example to establish connection with the Oracle


database
1.
2.

Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","password");

3) Create the Statement object


The createStatement() method of Connection interface is used to create statement.
The object of statement is responsible to execute queries with the database.

Syntax of createStatement() method


1.

public Statement createStatement()throws SQLException

Example to create the statement object


1.

Statement stmt=con.createStatement();

4) Execute the query


The executeQuery() method of Statement interface is used to execute queries to the
database. This method returns the object of ResultSet that can be used to get all the
records of a table.

Syntax of executeQuery() method


1.

public ResultSet executeQuery(String sql)throws SQLException

Example to execute query


1.
2.
3.
4.
5.

ResultSet rs=stmt.executeQuery("select * from emp");


while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}

5) Close the connection object


By closing connection object statement and ResultSet will be closed automatically.
The close() method of Connection interface is used to close the connection.

Syntax of close() method


1.

public void close()throws SQLException

Example to close connection


1.

con.close();

Example to connect to the Oracle database


For connecting java application with the oracle database, you need to follow 5 steps
to perform database connectivity. In this example we are using Oracle10g as the
database. So we need to know following informations for the oracle database:
1. Driver class: The driver class for the oracle database
is oracle.jdbc.driver.OracleDriver.
2. Connection URL: The connection URL for the oracle10G database
is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the
database, thin is the driver, localhost is the server name on which oracle is
running, we may also use IP address, 1521 is the port number and XE is the
Oracle service name. You may get all these informations from the
tnsnames.ora file.
3. Username: The default username for the oracle database is system.
4. Password: Password is given by the user at the time of installing the oracle
database.

1.

Let's first create a table in oracle database.


create table emp(id number(10),name varchar2(40),age number(3));

Example to Connect Java Application with Oracle


database
In this example, system is the username and oracle is the password of the Oracle
database.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
//step2 create the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
//step3 create the statement object

13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.

Statement stmt=con.createStatement();
//step4 execute query
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
//step5 close the connection object
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The above example will fetch all the records of emp table.

To connect java application with the Oracle database ojdbc14.jar file is required to be
loaded.

Two ways to load the jar file:


1. paste the ojdbc14.jar file in jre/lib/ext folder
2. set classpath

1) paste the ojdbc14.jar file in JRE/lib/ext folder:


Firstly, search the ojdbc14.jar file then go to JRE/lib/ext folder and paste the jar file
here.

2) set classpath:
There are two ways to set the classpath:
temporary

permanent

How to set the temporary classpath:


1.

Firstly, search the ojdbc14.jar file then open command prompt and write:
C:>set classpath=c:\folder\ojdbc14.jar;.;

How to set the permanent classpath:

Go to environment variable then click on new tab. In variable name


write classpath and in variable value paste the path to ojdbc14.jar by appending
ojdbc14.jar;.; as
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;.;

Example to connect to the mysql database


For connecting java application with the mysql database, you need to follow 5 steps to
perform database connectivity.
In this example we are using MySql as the database. So we need to know following
informations for the mysql database:
1. Driver class: The driver class for the mysql database
is com.mysql.jdbc.Driver.
2. Connection URL: The connection URL for the mysql database
is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the
database, localhost is the server name on which mysql is running, we may also
use IP address, 3306 is the port number and sonoo is the database name. We
may use any database, in such case, you need to replace the sonoo with your
database name.
3. Username: The default username for the mysql database is root.
4. Password: Password is given by the user at the time of installing the mysql
database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to
create database first.
1.
2.
3.

create database sonoo;


use sonoo;
create table emp(id int(10),name varchar(40),age int(3));

Example to Connect Java Application with mysql


database
In this example, sonoo is the database name, root is the username and password.
1.
2.
3.
4.
5.

import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");

6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The above example will fetch all the records of emp table.

To connect java application with the mysql database mysqlconnector.jar file is required to
be loaded.

Two ways to load the jar file:


1. paste the mysqlconnector.jar file in jre/lib/ext folder
2. set classpath

1) paste the mysqlconnector.jar file in JRE/lib/ext folder:


Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file
here.

2) set classpath:
There are two ways to set the classpath:
temporary

permament

How to set the temporary classpath

1.

open comman prompt and write:


C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;

How to set the permanent classpath


Go to environment variable then click on new tab. In variable name write classpath and
in variable value paste the path to the mysqlconnector.jar file by appending
mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;

You might also like