- JDBC(Java Database Connectivity)is part of the Java Standard Edition platform, from Oracle Corporation.
- The stable release of JDBC is JDBC 4.3 / September 21, 2017
- The current developer of JDBC is Oracle Corportation.
- The JDBC API was modeled after ODBC API.
- JDBC is a core part of the Java platform/JavaSE (Java Standard Edition).
- JDBC is a standard Java API for connecting programs written in Java to the data in relational databases(RDBMS), independently.
- JDBC is used to connect java application with database.It provides java classes and interfaces to connect or communicate Java application with database.
- JDBC API enables to access any kind of data stored in a Relational Database. It enables Java programs to execute required SQL statements to fetch the data.
- JDBC is a software component that enables a Java application to interact with a database.
- The function of JDBC is to help the Java-based application to access different types of databases.
- JDBC Driver is a software component that enables Java application to interact with the database.
- It is a Java-based data access technology used for Java database connectivity.
- JDBC provides a secure connection to the database.
- To connect with individual databases, JDBC requires drivers for each database.
- Before JDBC, ODBC API was the database API to connect and execute the query with the database. But, ODBC API uses ODBC driver which is written in C language (i.e. platform dependent and unsecured). That is why Java has defined its own API (JDBC API) that uses JDBC drivers (written in Java language).
- There are two major component of JDBC : Connection Pooling and Data Sources.
- JDBC API uses JDBC drivers to connect with the database.By the help of JDBC API, we can save, update, delete and fetch/search data from the database.
- The java.sql package contains classes and interfaces for JDBC API.
- JDBC is built on the basis of ODBC and hence, some basics of ODBC retain in JDBC.
- The major difference between java.util.Date and java.sql.Date is that, java.sql.Date represents date without time information whereas, java.util.Date represents both date and time information.
- The JDBC architecture supports two-tier and three-tier processing models for accessing a database.In the two-tier model, a Java applet or application communicates directly to the data source.In the three-tier model, the user’s commands or queries are sent to middle-tier services, from which the commands are again sent to the data source. The results are sent back to the middle tier, and from there to the user. This type of model is found very useful by management information system.
- It is a set of java classes that implement the JDBC interfaces to process JDBC calls and return result sets to a Java application.
- The database that stores the data, retrieved by the application using the JDBC Driver.
- A JDBC driver is a small piece of software that allows JDBC to connect to different databases.
- Essentially, a JDBC driver makes it possible to do three things: – Establish a connection with a data source, Send queries and update statements to the data source.
- A JDBC driver uses the JDBC API developed by Sun Microsystems initially, but now the part of Oracle.
Types of JDBC Drivers –
- There are four types of JDBC drivers:
- JDBC-ODBC Bridge Driver
- The JDBC-ODBC bridge driver uses the ODBC driver to connect to the database.
- The JDBC-ODBC bridge driver converts JDBC method calls into the ODBC function calls.
- It is easy to use and can be easily connected to any database.
- Native – API Driver (Partially Java Driver)
- The Native API driver uses the client-side libraries of the database.
- The driver converts JDBC method calls into native calls of the database API.
- It is not written entirely in Java. Its performance is better than JDBC-ODBC bridge driver. However, the native driver must be installed on each client machine.
- Network Protocol Driver (Fully Java Driver)
- The Network Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol.
- It is entirely written in Java.
- There is no requirement of the client-side library because of the application server that can perform many tasks like auditing, load balancing, logging, etc.
- Thin Driver (Fully Java Driver)
- The thin driver converts JDBC calls directly into the vendor-specific database protocol. That is why it is known as the thin driver.
- It is entirely written in Java language.
- Its performance is better than all other drivers however these drivers depend upon the database.
- JDBC-ODBC Bridge Driver
JDBC API Components
- The java.sql package contains following interfaces and classes for JDBC API which are fully responsible for JDBC connectivity.
A list of common classes of JDBC API that involves in JDBC connectivity are –
- DriverManager Class : –
- The DriverManager class acts as an interface between the user and drivers.
- It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver.
- It contains several methods to keep the interaction between the user and drivers.
- Types Class : –
- Blob Class : –
- Blob stands for the binary large object.
- It represents a collection of binary data stored as a single entity in the database management system.
- Clob Class : –
- Clob stands for Character large object.
- It is a data type that is used by various database management systems to store character files.
- It is similar to Blob except for the difference that BLOB represent binary data such as images, audio and video files, etc. whereas Clob represents character stream data such as character files, etc.
- SQLException Class : –
- It is an Exception class which provides information on database access errors.
A list of common interfaces of JDBC API that involves in JDBC connectivity are –
- Driver Interface :
- Connection Interface :
- The Connection object is created by using getConnection() method of DriverManager class. DriverManager is the factory for connection.
- ResultSet Interface :
- The object of ResultSet maintains a cursor pointing to a row of a table.
- Initially, cursor points before the first row.
- The executeQuery() method of Statement interface returns the ResultSet object.
- ResultSetMetaData Interface :
- The object of ResultSetMetaData interface cotains the information about the data (table) such as numer of columns, column name, column type, etc.
- The getMetaData() method of ResultSet returns the object of ResultSetMetaData.
- RowSet Interface :
- DatabaseMetaData Interface:
- DatabaseMetaData interface provides methods to get metadata of a database such as the database product name, database product version, driver name, name of the total number of tables, the name of the total number of views, etc.
- The getMetaData() method of Connection interface returns the object of DatabaseMetaData.
(NB : In JDBC, Statements interface are mainly used to send SQL commands to the database and receive respective data from the database, in different way. There are various methods provided by JDBC statements such as execute(), executeUpdate(), executeQuery, etc. which helps to interact with the database to receive data as per situation. The statement may be – statement, PreparedStatement and CallableStatement which may have different mode of procedure with database.)
- Statement Interface :
- The Statement object is created by using createStatement() method of Connection class.
- The Connection interface is the factory for Statement.
- The Statement interface provides methods to execute queries with the database. The statement interface is a factory of ResultSet; i.e., it provides the factory method to get the object of ResultSet.
- In the case of Statement, the query is compiled each time we run the program.
- The Statement is mainly used in the case when we need to run the static query at runtime.
- Statement is the factory for resultset. It is used for general purpose access to the database. It executes a static SQL query at runtime.
- PreparedStatement Interface :
- The PrepareStatement object is created by using prepareStatement() method of Connection class.
- It is used to execute the parameterized query.
- The PreparedStatement interface is a subinterface of Statement. It is used to execute the parameterized query.
- In the case of PreparedStatement, the query is compiled only once.
- PreparedStatement is used when we need to provide input parameters to the query at runtime.
- The PreparedStatement is used when we need to provide input parameters to the query at runtime.
- CallableStatement Interface:
- CallableStatement interface is used to call the stored procedures and functions.
- We can have business logic on the database through the use of stored procedures and functions that will make the performance better because these are precompiled.
- The prepareCall() method of Connection interface returns the instance of CallableStatement.
- CallableStatement is used when we need to access the database stored procedures. It can also accept runtime parameters.
- The Benefits of using PreparedStatement over Statement interface is given below.
- The PreparedStatement performs faster as compare to Statement because the Statement needs to be compiled everytime we run the code whereas the PreparedStatement compiled once and then execute only on runtime.
- PreparedStatement can execute Parameterized query whereas Statement can only run static queries.
- The query used in PreparedStatement is appeared to be similar every time. Therefore, the database can reuse the previous access plan whereas, Statement inline the parameters into the String, therefore, the query doesn’t appear to be same everytime which prevents cache reusage.
Difference between execute(), executeQuery() and executeUpdate() method : –
|The execute method can be used for any SQL statements(Select and Update both).||The executeQuery method can be used only with the select statement.||The executeUpdate method can be used to update/delete/insert operations in the database.|
|The execute method returns a boolean type value where true indicates that the ResultSet s returned which can later be extracted and false indicates that the integer or void value is returned.||The executeQuery() method returns a ResultSet object which contains the data retrieved by the select statement.||The executeUpdate() method returns an integer value representing the number of records affected where 0 indicates that query returns nothing.|
Difference between ResultSet and Rowset Interface : –
|ResultSet cannot be serialized as it maintains the connection with the database.||RowSet is disconnected from the database and can be serialized.|
|ResultSet object is not a JavaBean object||ResultSet Object is a JavaBean object.|
|ResultSet is returned by the executeQuery() method of Statement Interface.||Rowset Interface extends ResultSet Interface and returned by calling the RowSetProvider.newFactory().createJdbcRowSet() method.|
|ResultSet object is non-scrollable and non-updatable by default.||RowSet object is scrollable and updatable by default.|
Steps of JDBC Connection
- There are following fundamental steps to connect java application with any database using JDBC. These are –
- Import JDBC packages.
- Load and register the JDBC driver class.
- Open & Create a connection to the database.
- Create a statement object to perform a query.
- Execute the statement object and return a query resultset.
- Process the resultset.
- Close the resultset and statement objects.
- Close the connection.
370 total views, 1 views today