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.
- https://dev.mysql.com/downloads/connector/j/
- https://docs.microsoft.com/en-us/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-2017
- https://www.oracle.com/technetwork/database/application-development/jdbc/downloads/jdbc-ucp-183-5013470.html
With a few steps, we can define how JDBC works
- Import required packages
- Register JDBC driver
- Open a connection
- Execute a query
- Extract data from the result set
- 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
- https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/rzaha/jdbctydr.htm
- https://www.baeldung.com/jni
- http://tutorials.jenkov.com/jdbc/driver-types.html
- https://docs.oracle.com/javase/tutorial/jdbc/basics/gettingstarted.html
- https://www.journaldev.com/2681/jdbc-tutorial
- https://dzone.com/articles/15-reasons-to-choose-hibernate-over-jdbc
- https://www.oracle.com/technetwork/database/application-development/jdbc/overview/quickstart-4308895.html
- https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.jdbc_pg.doc/ids_jdbc_011.htm
- https://docs.microsoft.com/en-us/sql/relational-databases/native-client-odbc-how-to/configuring-the-sql-server-odbc-driver-add-a-data-source?view=sql-server-2017
- https://docs.oracle.com/cd/E19509-01/820-5069/6ngg83nai/index.html
Comments
Post a Comment