JDBC (Java Database Connectivity) : Introduction and Why not ?




JDBC allows java application to access databases. It is the JavaSoft specification of a standard application programming interface (API).  It consists of a set of interfaces and classes written in Java. Programmers can write applications that connect to databases and execute queries. Applications can connect to any database (Mysql, Oracle, Ms SQL server)  using JDBC as long as the driver exists for the particular DBMS.


There are several JDBC driver types based on the technologies they use. The driver should be selected based on the application requirements. Below are the driver types,



  • JDBC-ODBC bridge driver.  ODBC drivers exist for many Relational Database Management System (RDBMS) platforms. The Java Native Interface (JNI) is used to call ODBC functions from the JDBC driver. This method also needs a bridge driver installed and configured before JDBC can be used. The Java Native Interface (JNI) is used to call ODBC functions from the JDBC driver.  This doesn't support applets since applets cannot load native code.

  • Java + Native code driver. Java native methods are used to invoke the API functions that perform database operations.

  • All Java + Middleware translation driver. These drivers use a networking protocol and middleware to communicate with a server. This intermediate server then connects to the database.

  • All Java driver. Java is being used to implement a DBMS vendor networking protocol. DBMS vendors provide the drivers. This method might not support some applications if the underlying protocol does not handle issues such as security and network connectivity.


Below are few links for above-mentioned DBMS drivers.

With a few steps, we can define how JDBC works

  1. Import required packages

  2. Register JDBC driver

  3. Open a connection

  4. Execute a query

  5. Extract data from the result set

  6. Clean-up environment

Check below example, which has above-mentioned steps.


Link for the Github project: https://github.com/h-hub/Java_Practice/tree/master/src/main/java/javaPractice/jdbc

JDBC has features like Transaction Management, Batch Processing, and Stored Procedures. It is capable of handling database queries including Create, Select, Drop and Insert.

Why use ORM(Hibernate) over JDBC?


  • JDBC API has boiler-plate codes and Hibernate removes those. [Read more: https://stackoverflow.com/questions/1072925/remove-boilerplate-from-db-code].

  • Hibernate supports inheritance, associations, and collections.

  • Transaction Management is easy with Hibernate [Read more: https://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html].

  • Hibernate throws only un-checked exceptions and it's transaction management system removes the usage of try-catch blocks. [Read more: https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html]

  • JDBC supports native SQL, but Hibernate Query Language (HQL)  more powerful.

  • Hibernate supports inheritance, associations, and collections.

  • Hibernate supports caching and supports lazy initialization.

  • Hibernate can create database tables as well.

  • Hibernate supports JNDI DataSource which is an important feature in Production applications.

  • Hibernate supports JPA. And it makes the code implementation independent.

  • Hibernate is capable of running native SQL queries and because of this, it can support vendor-specific features.

References

Comments