Sunday, 26 February 2017

What is JDBC ? Explain with example?

For simple JDBC Connectivity we need to follow below step-
1.      Register JDBC driver.
2.      Open a connection using DriverManager.getConnection()
3.      Create a statement connectionObj.createStatment()
4.      Execute query using statement stmtObject.executeQuery(query);
5.      Fetch the result in ResultSet object and iterate to get the values
Refer below java program

Simple JDBC Example:

Code Snippet-
import java.sql.*;

public class JdbcTest {
  public static void main(String args[]) {
       try {
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Test", "root", "admin");
              Statement stmt = con.createStatement();
              ResultSet rs = stmt.executeQuery("SELECT * FROM EMPLOYEE;");
              while (rs.next())
                     System.out.println(rs.getString(1) + "  " + rs.getString(2) + "  " + rs.getString(3)+ "  " + rs.getInt(4));
              con.close();
       } catch (Exception e) {
              System.out.println(e);
       }
  }
}

Types JDBC Driver-

There are 4 types of JDBC driver

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)

JDBC-ODBC bridge driver
JDBC Bridge is used to access ODBC drivers installed on each client machine. This type of driver is recommended only for experimental use or when no other alternative is available.

Native-API driver (partially java driver)
This driver typically provided by the database vendor an used in the same manner as JDBC-ODBC bridge driver.

Network Protocol driver (fully java driver)
JDBC clients use standard network socket to communicate with the middleware application server. The socket information is then translated by the middleware application server into the call format required by the DBMS, and forwarded to the database server. It is extremely flexible.

Thin driver (fully java driver)
In a Type 4 driver, a pure Java-based driver communicates directly with the vendor's database through socket connection. This is the highest performance driver available for the database and is usually provided by the vendor itself.

This kind of driver is extremely flexible; you don't need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

Types of Statement-

1.      Statement
2.      Prepared Statement
3.      Callable Statement

Statement
Use the for general-purpose access to your database. Useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters.

Prepared Statement
Use the when you plan to use the SQL statements many times. The PreparedStatement interface accepts input parameters at runtime.

Callable Statement

Use the when you want to access the database stored procedures. The CallableStatement interface can also accept runtime input parameters.

1 comment: