1 of 20

JDBC

2 of 20

Java Database Connectivity(JDBC)

  • JDBC is an Application Programming Interface(API) used to connect Java application with Database.
  • JDBC is used to interact with various type of Database such as Oracle, MS Access, My SQL and SQL Server.
  • JDBC can also be defined as the platform-independent interface between a relational database and Java programming.
  • It allows java program to execute SQL statement and retrieve result from database

3 of 20

4 of 20

Use JDBC�

JDBC API to handle database using Java program and can perform following activities

  • Connect to the database
  • Execute queries and update statements to the database
  • Retrieve the result received from the database.

5 of 20

JDBC Driver�

JDBC Driver is required to process SQL requests and generate result. The following are the different types of driver available in JDBC.

  • Type-1 Driver or JDBC-ODBC bridge
  • Type-2 Driver or Native API Partly Java Driver
  • Type-3 Driver or Network Protocol Driver
  • Type-4 Driver or Thin Driver

6 of 20

JDBC-ODBC bridge�Type-1 Driver act as a bridge between JDBC and other database connectivity mechanism(ODBC). This driver converts JDBC calls into ODBC calls and redirects the request to the ODBC drive

7 of 20

Contd..

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.

8 of 20

Native API Driver�This type of driver make use of Java Native Interface(JNI) call on database specific native client API. These native client API are usually written in C and C++

9 of 20

Contd..

Advantage

  • faster as compared to Type-1 Driver
  • Contains additional features.

Disadvantage

  • Requires native library
  • Increased cost of Application

10 of 20

Network Protocol Driver�This driver translate the JDBC calls into a database server independent and Middleware server-specific calls. Middleware server further translate JDBC calls into database specific calls.

11 of 20

Contd..

Advantage

  • Does not require any native library to be installed.
  • Database Independency.
  • Provide facility to switch over from one database to another database.

Disadvantage

  • Slow due to increase number of network call.

12 of 20

Thin Driver�This is Driver called Pure Java Driver because. This driver interact directly with database. It does not require any native database library, that is why it is also known as Thin Driver.

13 of 20

Contd..

Advantage

  • Does not require any native library.
  • Does not require any Middleware server.
  • Better Performance than other driver.

Disadvantage

  • Slow due to increase number of network call.

14 of 20

Java Database Connectivity with 5 Steps�

There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:

  • Register the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection

15 of 20

16 of 20

Register the Driver�

  • The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.
  • Class.forName("oracle.jdbc.driver.OracleDriver");

17 of 20

Create the connection object

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

Syntax of getConnection() method

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

2) public static Connection getConnection(String url,String name,String password)  throws SQLException 

Connection con=DriverManager.getConnection(

"jdbc:oracle:thin:@localhost:1521:xe","system","password");

18 of 20

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

public Statement createStatement()throws SQLException 

Statement stmt=con.createStatement();

19 of 20

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.

public ResultSet executeQuery(String sql)throws SQLException

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

while(rs.next()){  

System.out.println(rs.getInt(1)+" "+rs.getString(2));  

}  

20 of 20

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.

public void close()throws SQLException 

con.close();