JDBC Notes
JDBC Notes
JDBC Notes
JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and
execute the query with the database. It is a part of JavaSE (Java Standard Edition).
JDBC API uses JDBC drivers to connect with the database. There are four types of
JDBC drivers:
○ Native Driver,
○ Thin Driver
We can use JDBC API to access tabular data stored in any relational database. By the
help of JDBC API, we can save, update, delete and fetch data from the database. It is
like Open Database Connectivity (ODBC) provided by Microsoft.
The java.sql package contains classes and interfaces for JDBC API. A list of popular
interfaces of JDBC API are given below:
○ Driver interface
○ Connection interface
○ Statement interface
○ PreparedStatement interface
○ CallableStatement interface
○ ResultSet interface
○ ResultSetMetaData interface
○ DatabaseMetaData interface
○ RowSet interface
● DriverManager class
● Blob class
● Clob class
● Types class
Before JDBC, ODBC API was the database API to connect and execute the 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). We can use JDBC API to handle
database using Java program and can perform the following activities:
The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC
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
Advantage:
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:
4) Thin driver
The thin driver converts JDBC calls directly into the vendor-specific database protocol.
Advantage:
Disadvantage:
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
database. This method returns the object of ResultSet that can be used to get all the
records of a table.
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
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.
4. Password: It is the password 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 a database first.
use upes;
import java.sql.*;
class MysqlCon{
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/upes","root","root");
Statement stmt=con.createStatement();
while(rs.next())
con.close();
To connect java application with the mysql database, mysqlconnector.jar file is required
to be loaded.
download the jar file mysql-connector.jar Two ways to load the jar file:
here.
2) Set classpath:
There are two ways to set the classpath:
○ temporary
○ permanent
1. C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
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;.;