Java JDBC

In Java JDBC stands for Java database connection. There are variety of databases available. Some of the popular databases include Oracle, DB2, MySql etc. They are stand alone entities, thus to communicate with these databases from java applications we need some mechanism. This mechanism is called JDBC. JDBC is a Java API to interact with different databases.

There are two types of JDBC API

JDBC API: This provides the application-to-JDBC Manager connection.
JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.

Whats JDBC Driver ?

The JDBC Driver is the one which helps JDBC API to communicate with different underlying databases.The JDBC Driver is in the java.sql.Driver interface. These drivers internally decide how fast the communication with the database occurs.

There are 4 types of JDBC Drivers

Type 1: JDBC-ODBC Bridge Driver

ODBC is a open standard database standard. The JDBC-ODBC Driver will convert the JDBC API calls into the ODBC Standard calls hence the name. This is a bit slow as it needas 2 level of conversion. This is not mostly used.

Type 2: JDBC-Native API

Here the JDBC is converted to the native C/C++ API calls.These are provided by the database vendors.

Type 3: JDBC-Net Pure Java

Here there is a internal middle layer of java which acts as a driver to communicate with the native database. This supports for vide range of databases.

Type 4: 100% Pure Java
This driver directly communicates with the database. Hence this is most widely used and very efficient.

To create a JDBC Connection you need to perform following steps.

1. Load the JDBC Driver
2. Create a Connection to Database
3. Create Statements
4. Execute Statements
5. Fetch Results

  1. import java.sql.* ;
  2. class MyJdbc{
  3. private Connection conn = null;
  4. private Statement st = null;
  5. private String URL = "jdbc:oracle:thin:@amrood:1521:EMP";
  6. private String USER = "username";
  7. private String PASS = "password"
  8.  
  9. MyJdbc(){}
  10. public static void main(String[] args){
  11. try{
  12.  
  13. Class.forName("oracle.jdbc.driver.OracleDriver")
  14. conn = DriverManager.getConnection(URL,USER,PASS);
  15. st = conn.createStatement();
  16. st.executeQuery("Insert Into Test values(1,2,3)");
  17.  
  18. conn.close();
  19. }
  20. }
  21. }

LEAVE A REPLY

Please enter your comment!
Please enter your name here