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

JDBC Drivers

JDBC is a Java API that allows Java programs to connect to databases. There are four types of JDBC drivers: JDBC-ODBC bridge drivers, native API drivers, network protocol drivers, and thin drivers. To connect to a database using JDBC, a program must register the driver class, create a connection, create a statement, execute queries, and close the connection. Statements in JDBC include create statements, prepared statements, and callable statements.

Uploaded by

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

JDBC Drivers

JDBC is a Java API that allows Java programs to connect to databases. There are four types of JDBC drivers: JDBC-ODBC bridge drivers, native API drivers, network protocol drivers, and thin drivers. To connect to a database using JDBC, a program must register the driver class, create a connection, create a statement, execute queries, and close the connection. Statements in JDBC include create statements, prepared statements, and callable statements.

Uploaded by

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

JDBC Drivers

JDBC stands for Java Database Connectivity, which is a standard Java API for
database-independent connectivity between the Java programming language and a
wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that
are commonly associated with database usage.
 Making a connection to a database.
 Creating SQL or MySQL statements.
 Executing SQL or MySQL queries in the database.
 Viewing & Modifying the resulting records.
JDBC Architecture
JDBC Architecture consists of two layers −
 JDBC API − This provides the application-to-JDBC Manager connection.
 JDBC Driver API − This supports the JDBC Manager-to-Driver Connection.
JDBC Driver:-
JDBC Driver is a software component that enables java application to interact with
the database. There are 4 types of JDBC drivers:

 JDBC-ODBC bridge driver


 Native-API driver (partially java driver)
 Network Protocol driver (fully java driver)
 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:
o performance upgraded than JDBC-ODBC bridge driver.

Disadvantage:
o The Native driver needs to be installed on the each client machine.
o 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 depend on the Database.
Java Database Connectivity
There are 5 steps to connect any java application with the database using JDBC.
These steps are as follows:

 Register the Driver class


 Create connection
 Create statement
 Execute queries
 Close 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 :-
public static void forName(String className)throws ClassNotFoundException
Example to register the OracleDriver class
Here, Java program is loading oracle driver to establish database connection.
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:-
public static Connection getConnection(String url) throws SQLException
public static Connection getConnection(String url,String name,String password)
throws SQLException
Example to establish connection with the Oracle database
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 :-
public Statement createStatement() throws SQLException
Example:-
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:-
public ResultSet executeQuery(String sql)throws SQLException
Example:-
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 :-
public void close()throws SQLException
Example:-
con.close();
Types of Statements in JDBC
The statement interface is used to create SQL basic statements in Java it provides
methods to execute queries with the database. There are different types of
statements that are used in JDBC as follows:
 Create Statement
 Prepared Statement
 Callable Statement
1. Create a Statement: From the connection interface, you can create the object
for this interface. It is generally used for general–purpose access to databases and
is useful while using static SQL statements at runtime.
Syntax:
Statement st = con.createStatement();
Implementation: Once the Statement object is created, there are three ways to
execute it.
boolean execute(String SQL): If the ResultSet object is retrieved, then it returns
true else false is returned. Is used to execute SQL DDL statements or for dynamic
SQL.
int executeUpdate(String SQL): Returns number of rows that are affected by the
execution of the statement, used when you need a number for INSERT, DELETE
or UPDATE statements.
ResultSet executeQuery(String SQL): Returns a ResultSet object. Used
similarly as SELECT is used in SQL.
2. Prepared Statement:- It represents a recompiled SQL statement, that can be
executed many times. This accepts parameterized SQL queries. In this, “?” is used
instead of the parameter, one can pass the parameter dynamically by using the
methods of PREPARED STATEMENT at run time.
syntax:-
String query = "INSERT INTO Employees(Name, Salary, Location) VALUES (?,
?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
Implementation: Once the PreparedStatement object is created, there are three
ways to execute it:
execute(): This returns a boolean value and executes a static SQL statement that is
present in the prepared statement object.
executeQuery(): Returns a ResultSet from the current prepared statement.
executeUpdate(): Returns the number of rows affected by the DML statements
such as INSERT, DELETE, and more that is present in the current Prepared
Statement.
3. Callable Statements: are stored procedures which are a group of statements
that we compile in the database for some task, they are beneficial when we are
dealing with multiple tables with complex scenario & rather than sending multiple
queries to the database, we can send the required data to the stored procedure &
lower the logic executed in the database server itself. The Callable Statement
interface provided by JDBC API helps in executing stored procedures.
Syntax:
CallableStatement cstmt = con.prepareCall("{call Procedure_name(?, ?}");
Implementation: Once the callable statement object is created.
execute() is used to perform the execution of the statement.

You might also like