JDBC Notes
JDBC Notes
UNIT VI : JDBC
An application should be connected to a database for storing the info provided by the user. This
is referred as database connectivity. The JDBC API is used to connect the application & the database & is
also responsible for the transfer of data between the two. In some cases multiple databases are used to
store the data. In such cases it becomes difficult to write code for establishing application database
connectivity with each of the database. This problem is solved by Ms Standard for comm. With database
called as ODBC API (open database connectivity)
Java programs cannot directly communicate with ODBC drivers. ODBC is written in C language. It
uses pointers & other constructs which are not supported in java.
JDBC v/s ODBC
1. ODBC is not appropriate for direct use from java as it uses C interface.
2. Java does not support pointers & ODBC makes use of it.
3. ODBC driver managers must be manually installed on every client when it is needed where as
JDBC code is automatically installable, portable & secure.
4. ODBC is hard to learn & has complex options even for simple query.
5. ODBC is platform specify whereas JDBC is platform independent.
JDBC Architecture
It consists of two layers as JDBC API & JDBC Driver API. JDBC API provides application to JDBC
Manager Connection & JDBC Driver API supports JDBC Manager to db driver connection.
Two models for data access are supported by JDBC API, they are – 2 tier model & 3 tier model.
In two tier model java application directly talks to the db. This requires JDBC driver that can
communicate with particular DBMS sys being accessed. SQL statements are delivered to
database & results are sent back to the application. It is referred to as client server application.
Page 1 of 6
IF4K/JPR/SMJ
In 3 tier model commands are send to a middle tier of services, which sends SQL statements &
send the result back to middle tier & then to the user. The middle tier architecture can provide
performance advantages.
JDBC Driver
There are 4 types of JDBC drivers:
1. JDBC ODBC bridge driver
This type of JDBC driver provides db access using ODBC driver. As a part of JDBC, sun
microsys provides a driver called as JDBC-ODBC-Bridge to access ODBC data source. The
bridge between JDBC & ODBC drivers translates JDBC operations into ODBC operations.
Page 2 of 6
IF4K/JPR/SMJ
For establishing connection with DSN and retrieving the data procedure is as follows:
(Note : this procedure works for jdk1.7 or prior, Type 1 driver is removed from JDK 1.8
onwards)
Driver can be registered with the syntax as follows :
Class.forName(“Driver name”);
Eg : Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Page 3 of 6
IF4K/JPR/SMJ
Steps :
(Note : Create DSN (Data Source Name) in case of the procedure with DSN connectivity)
1. Import java.sql.*; in the applicatication.
2. Register Database Driver.
3. Connect to database with getConnection().
4. Create a Statement.
5. Execute a Statement .
6. Store the result in a Resultset in case of ‘select’ query.
7. Retrieve result from ResultSet.
8. Close the Statement and Connection.
For establishing connection without DSN and retrieving the data the procedure is as follows:
In this case we have to find proper driver for the environment we are using at the backend.
For eg, if its MySql then the driver can be registered as :
Class.forName("com.mysql.jdbc.Driver");
Here the driver used is for Mysql database environment.
It can be as
Class.forName("com.mysql.cj.jdbc.Driver"); in some version where cj is Connector/J
While compiling the program we require a jar file for getting access to mysql jdbc connector,
which can be downloaded and copied to c:\...\jdk folder\jre\lib\ext and set in the classpath
in environment variable or as set CLASSPATH = <classpath>
Or temporarily it can be used while compiling as :
javac -classpath c:\...\jdk..\jre\lib\ext\ mysql-connector-java-8.0.21 <java sourcecode>
jar filename may differ from version to version.
Page 4 of 6
IF4K/JPR/SMJ
PreparedStatement Object
If an application executes a query according to search criteria specified by the user, it can be
done by a query statement that receives appropriate value in ‘where’ clause at runtime. For this
purpose PreparedStatement object can be created which can take input at runtime and can be
used to search criteria to display required results.
Eg : PreparedStatement ps;
Ps= con.prepareStatement(“select * from emp where deptno =? Or deptno=?”);
Where ? represents placeholder.
Before executing PrepareStatement object, you must get the values for each ‘?’ parameter. It
can be done by the method ‘setXXX(position, value)’ where XXX- datatype.
Eg : setInt(1,20);
setInt(2,30);
setString(5,”abc”);
setFloat(3,35.5);
Name :
Salary :
Insert data
Page 5 of 6
IF4K/JPR/SMJ
8) WAP to create student table with rollno, name and average through a java application.
9) WAP to display all records from student table.
Page 6 of 6