➤ Intro to JDBC
➤ JDBC vs ODBC
➤ JDBC API
➤ JDBC Driver
Connect to Database
➤ Oracle Connection
➤ MySQL Connection
➤ Using Eclipse IDE
➤ Coding Standards
➤ Close JDBC Object
➤ Java.sql Package
➤ DriverManager Class
➤ Get Connection
➤ Statement Interface
➤ Statement Example
➤ Create a database
➤ Types of statements
➤ Statement vs PreParedStatement
➤ Insert record
➤ Update record
➤ Select record
➤ Create a table
➤ More Examples
➤ Surrogate Key
➤ ResultSetMetaData & ParameterMetaData
JDBC is the Java API that manages connecting to a database, issuing queries and commands, and handling resultset obtained from the database. Working with JDBC driver software is nothing but working JDBC technology which is not installable but we can arrange JDBC driver software as jar files.
Java Database Connectivity (JDBC) is an application programming interface (API) for the Java programming language, which defines how a client may access a database. It is a Java-based data access technology used for Java to database connectivity and it is part of the Java Standard Edition platform and given by Oracle corporation. It provides methods to query and update data in a database and is oriented toward relational databases.
As said by Oracle Corporation, The Java Database Connectivity (JDBC) API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases – SQL databases and other tabular data sources, such as spreadsheets or flat files. The JDBC API provides a call-level API for SQL-based database access.
JDBC technology allows us to use the Java programming language to exploit “Write Once, Run Anywhere” capabilities for applications that require access to enterprise data. With a JDBC technology-enabled driver, we can connect all corporate data even in a heterogeneous environment.
What does JDBC stand for, or what is the JDBC full form in Java?
Initially, JDBC stands for Java Database Connectivity, but currently, JDBC is a Trademark and there is no abbreviation.
Why should We learn JDBC?
Let us understand it from the stretch. The applications which run on a single machine or computer are called Standalone applications. There are two types of standalone applications:- GUI based and CUI based standalone application. Examples:- VLC, Text editors, all basic Java programs. An application that can be executed in a local system with a local call and also from a remote computer via network call (request) is called an internet or web application. The web application is multiple computers and multiple user applications. Examples:- facebook.com, google.com, gmail.com, e.t.c.
In Java, the main method class is a stand-alone application. It can’t be accessible from other computers through the network (LAN/WAN/MAN). So, by default, every Java program is a standalone application. Using basic Java we can develop only standalone applications, not web applications.
In real time Java is mostly used for web applications. And compared to web applications, very less standalone applications are developed by using Java.
If we want to develop web applications then basic Java knowledge is not enough, we need to learn JSE and JEE technologies. There are three editions of Java:- JSE, JEE, and JME.
|Java Standard Edition (JSE)||Basic Java, JDBC|
|Java Enterprise Edition (JEE)||Servlet, JSP|
To develop web applications in Java we need to learn JDBC, Servlet, and JSP technologies. JSP used for presentation logic (the logic to display something on the web) like the login page, the home page. Servlet is used for processing logic. JDBC is used to communicate with the database.
Let us understand it with an example. In our daily life, we visit many websites, and some of them need authentication i.e. username and password to verify the user. Those usernames and passwords are already stored in the database of those websites during account creation. Now, whenever we visit the websites then due to the JSP, the login page displayed on the web browser. when we try to login with some username and password then the Servlet process those data and JDBC check the valid credentials matching with the existing database records.
JDBC return results back to Servlet. If the given username and password were correct then the Servlet process to the next step and display the home/account/other page using JSP.
While learning JDBC we generally work with standalone applications. After learning JDBC, we use them with Servlet and JSP technologies to develop the web applications.
Prerequisite to learn JDBC
- Java Basics ( including OOP, and Exception handling).
- SQL language (basic queries)
JDBC API and JDBC driver
JDBC is Java-based software technology/specification providing JDBC API having a set of rules and guidelines to develop JDBC driver software by using Java language. The JDBC API is given by Sun Microsystem (now Oracle Corporation). It is open-source and free. Anyone can develop their own JDBC driver following the JDBC API. The JDBC technology gives JDBC API in the form of packages having classes and interfaces.
JDBC software/driver is the implementation of JDBC API. Generally, database vendors and third-party vendors develop the JDBC driver by following the JDBC API. It can be free or paid it depending on the vendors who developed them, but most of the JDBC drivers are free so don’t worry. Here you can get the list of all vendors.
Implementation of the JDBC API interface is not the responsibility if the programmers. It is the responsibility of the vendor company that developed the JDBC driver software. All these vendors companies develop JDBC driver software in the form of a huge number of classes implementing the JDBC package API interface in these classes contains logics to locate and interact with database software, send and execute SQL queries in database software.
Vendor companies will release all these classes by packaging into jar files representing JDBC driver software. By adding these Jar files to classpath/project we can use JDBC drivers software in our Java to application.
JDBC API will be used by vendor companies as the rule and guidelines to develop JDBC driver software and the same API will be used by the programmers as a base to develop JDBC persistence logic in JDBC application by activating JDBC driver software.
Every JDBC driver software is identified with its driver class name by specifying that class name in our Java applications, so we can use JDBC driver software in our JDBC application.
For each database, we need a separate JDBC driver. If we want to work with Oracle database then arrange Oracle JDBC driver, for MySQL database arrange MySQL JDBC driver/connector, and for PostgreSQL arrange PostgreSQL JDBC driver.
For one database there can be many JDBC drivers developed by different vendors. For example, if we search “Oracle JDBC driver” then you will get lots of JDBC drivers for Oracle database developed by different-different vendors.
We can understand JDBC API vs JDBC driver from another angle. Assume JDBC API as an architecture of an electric car, which gives rules and guidelines to develop the electric car. Using this architecture multiple companies, can develop many models. Models can have different variations and prices. Assume ourselves as a customer/driver who cares about performance/design/price. Now, we will buy and use only those car which gives better performance, within our budget and e.t.c.
If you know how to drive an X model car developed by any company, then you can drive any car. Only a few changes can be there for different cars. Similarly, all JDBC drivers are developed by following the same JDBC API (rules and guidelines). So, the knowledge we gain by using one JDBC driver can be used for any other JDBC driver.
We discussed JDBC driver versions in detail here.
JDBC Components and Features
JDBC Basic components are,
- Driver:- It works as a translator which converts Java-specific call to database call and database-specific call to Java call.
- Connection:- From Java application to reach database some network connection is required, that network socket is nothing but a connection.
- Statement:- It is used to send the SQL queries to the database.
- ResultSet:- It holds the result of the SQL query.
- It is a standalone API. It is database-independent technologies. JDBC program written for one database can be used for any database.
- It supports WORA (write once and run anywhere). Most JDBC drivers are written in Java only so it can be used on any platform.
- Using JDBC we can easily perform CRUD operations. C=> Create, R=> Retrieve, U=> Update, D=> Delete.
- JDBC technology is open-source so most of the vendors developed their product based on JDBC.
- JDBC is part of JSE but Servlet/JSP are part of JEE.
- We are not responsible to develop JDBC drivers, they are already developed. We will just use them to develop our application.
- Working with JDBC driver is nothing but working with JDBC technologies.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!