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

JDBC

The document discusses different types of JDBC drivers: JDBC-ODBC bridge driver, Native-API driver, Network Protocol driver, and Thin driver. It also discusses the ResultSetMetaData interface in Java, which provides methods to retrieve metadata from a ResultSet object, such as the total number of columns, column names, and data types. Finally, it covers batch processing in JDBC, which allows executing a batch of SQL statements together to improve performance by reducing communication overhead.

Uploaded by

Aman khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

JDBC

The document discusses different types of JDBC drivers: JDBC-ODBC bridge driver, Native-API driver, Network Protocol driver, and Thin driver. It also discusses the ResultSetMetaData interface in Java, which provides methods to retrieve metadata from a ResultSet object, such as the total number of columns, column names, and data types. Finally, it covers batch processing in JDBC, which allows executing a batch of SQL statements together to improve performance by reducing communication overhead.

Uploaded by

Aman khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

JDBC 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.

In Java 8, the JDBC-ODBC Bridge has been removed.

Oracle does not support the JDBC-ODBC Bridge from Java 8. Oracle recommends that you use JDBC
drivers provided by the vendor of your database instead of the JDBC-ODBC Bridge.

Advantages:
o easy to use.
o can be easily connected to any database.

Disadvantages:
o Performance degraded because JDBC method call is converted into the ODBC function calls.
o The ODBC driver needs to be installed on the client machine.

1
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.

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

Disadvantages:
o Network support is required on client machine.
o Requires database-specific coding to be done in the middle tier.
o 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.

3
Advantage:
o Better performance than all other drivers.
o No software is required at client side or server side.

Disadvantage:
o Drivers depend on the Database.

Java ResultSetMetaData Interface


The metadata means data about data i.e. we can get further information from the data.

If you have to get metadata of a table like total number of column, column name, column type etc.
, ResultSetMetaData interface is useful because it provides methods to get metadata from the
ResultSet object.

Commonly used methods of ResultSetMetaData interface


Method Description

public int getColumnCount()throws it returns the total number of columns in


SQLException the ResultSet object.

public String getColumnName(int index)throws it returns the column name of the


SQLException specified column index.

4
public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.

public String getTableName(int index)throws it returns the table name for the specified
SQLException column index.

How to get the object of ResultSetMetaData:


The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData.
Syntax:

1. public ResultSetMetaData getMetaData()throws SQLException

Example of ResultSetMetaData interface :


1. import java.sql.*;
2. class Rsmd{
3. public static void main(String args[]){
4. try{
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6. Connection con=DriverManager.getConnection(
7. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
8.
9. PreparedStatement ps=con.prepareStatement("select * from emp");
10. ResultSet rs=ps.executeQuery();
11. ResultSetMetaData rsmd=rs.getMetaData();
12.
13. System.out.println("Total columns: "+rsmd.getColumnCount());
14. System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
15. System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
16.
17. con.close();
18. }catch(Exception e){ System.out.println(e);}
19. }
20. }
Output:Total columns: 2
Column Name of 1st column: ID
Column Type Name of 1st column: NUMBER

Java ResultSetMetaData Interface


The metadata means data about data i.e. we can get further information from the data.

5
If you have to get metadata of a table like total number of column, column name, column type etc.
, ResultSetMetaData interface is useful because it provides methods to get metadata from the
ResultSet object.

Commonly used methods of ResultSetMetaData interface


Method Description

public int getColumnCount()throws SQLException it returns the total number of columns in


the ResultSet object.

public String getColumnName(int index)throws it returns the column name of the specified
SQLException column index.

public String getColumnTypeName(int it returns the column type name for the
index)throws SQLException specified index.

public String getTableName(int index)throws it returns the table name for the specified
SQLException column index.

How to get the object of ResultSetMetaData:


The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData. Syntax:

1. public ResultSetMetaData getMetaData()throws SQLException

Example of ResultSetMetaData interface :


1. import java.sql.*;
2. class Rsmd{
3. public static void main(String args[]){
4. try{
5. Class.forName("oracle.jdbc.driver.OracleDriver");
6. Connection con=DriverManager.getConnection(
7. "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
8.
9. PreparedStatement ps=con.prepareStatement("select * from emp");
10. ResultSet rs=ps.executeQuery();
11. ResultSetMetaData rsmd=rs.getMetaData();
12.
13. System.out.println("Total columns: "+rsmd.getColumnCount());
14. System.out.println("Column Name of 1st column: "+rsmd.getColumnName(1));
15. System.out.println("Column Type Name of 1st column: "+rsmd.getColumnTypeName(1));
16.
6
17. con.close();
18. }catch(Exception e){ System.out.println(e);}
19. }
20. }
Output:Total columns: 2
Column Name of 1st column: ID
Column Type Name of 1st column: NUMBER

Batch Processing in JDBC


Instead of executing a single query, we can execute a batch (group) of queries. It makes the
performance fast. It is because when one sends multiple statements of SQL at once to the database,
the communication overhead is reduced significantly, as one is not communicating with the
database frequently, which in turn results to fast performance.

The java.sql.Statement and java.sql.PreparedStatement interfaces provide methods for batch


processing.

Advantage of Batch Processing


Fast Performance

Methods of Statement interface


The required methods for batch processing are given below:

Method Description

void addBatch(String query) The addBatch(String query) method of the


CallableStatement, PreparedStatement, and
Statement is used to single statements to a
batch.

int[] executeBatch() The executeBatch() method begins the


execution of all the grouped together
statements. The method returns an integer
array, and each of the element of the array
represents the updated count for respective
update statement.

boolean If the target database facilitates the batch


DatabaseMetaData.supportsBatchUpdates() update processing, then the method returns
throws SQLException true.

7
void clearBatch() The method removes all the statements that one
has added using the addBatch() method.

Example of batch processing in JDBC


Let's see the simple example of batch processing in JDBC. It follows following steps:

o Load the driver class


o Create Connection
o Create Statement
o Add query in the batch
o Execute Batch
o Close Connection

FileName: FetchRecords.java

1. import java.sql.*;
2. class FetchRecords{
3. public static void main(String args[])throws Exception{
4. Class.forName("oracle.jdbc.driver.OracleDriver");
5. Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
6. con.setAutoCommit(false);
7.
8. Statement stmt=con.createStatement();
9. stmt.addBatch("insert into user420 values(190,'abhi',40000)");
10. stmt.addBatch("insert into user420 values(191,'umesh',50000)");
11.
12. stmt.executeBatch();//executing the batch
13.
14. con.commit();
15. con.close();
16. }}

If you see the table user420, two records have been added.

You might also like