JDBC MySQL Connection – Java program5 min read

Introduction to JDBC

JDBC stands for (Java Database Connectivity). It is a Java API that allows you to connect to a database, issue queries and commands, and handle database result sets. JDBC was one of the first components designed for the Java persistence layer, and it was released as part of JDK 1.1 in 1997. In this tutorial, we will look at an example of a JDBC MySQL connection. JDBC was originally intended to be a client-side API that allowed a Java client to interact with a data source. This changed with the release of JDBC 2.0, which included an optional package that supported server-side JDBC connections. Since then, every new JDBC release has included updates to both the client-side package (java.sql) and the server-side package (javax.sql). JDBC 4.3, the most recent version as of this writing, was released in September 2017 as part of Java SE 9.

How JDBC works?

Establishing a connection with a data source, sending queries and update statements, and processing the results are all possible with JDBC. To send SQL statements and retrieve data, the Java program uses JDBC classes and interfaces. The JDBC driver is used to implement the JDBC API. The JDBC Driver is a collection of classes that implement the JDBC interfaces for processing JDBC calls and returning result sets to Java programs. The data obtained by the application using the JDBC Driver is stored in the database (or data store).

JDBC drivers are client-side adapters (installed on the client machine rather than the server) that convert Java program requests to a protocol that the DBMS can understand.

JDBC is an abbreviation for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a variety of databases. The JDBC library includes APIs for all of the tasks associated with database usage that are listed below.

  • Making a database connection.
  • Developing SQL or MySQL statements
  • SQL or MySQL queries are run in the database.
  • Viewing and modifying the records that result.

JDBC drivers are classified into four types :

1. Type-1 driver or JDBC-ODBC bridge driver

To connect to the database, the JDBC-ODBC bridge driver employs the ODBC driver. JDBC method calls are converted into ODBC function calls by the JDBC-ODBC bridge driver. JDBC method calls are converted into ODBC function calls by the JDBC-ODBC bridge driver. Because it can connect to any database, the Type-1 driver is also known as the Universal driver. Also Because a common driver is used to interact with various databases, the data transferred via this driver is not secure. In Java 8, the JDBC-ODBC Bridge has been removed.

2. Type-2 driver or Native-API driver

The Native API driver makes use of the database’s client-side libraries. The driver converts JDBC method calls into database API native calls. It is not entirely written in Java. This driver converts JDBC method calls into database API native calls. This driver requires their local API to interact with different databases, which is why data transfer is much more secure than with a type-1 driver.

3. Type-3 driver or Network Protocol driver

The Network Protocol driver makes use of middleware (application servers) to convert JDBC calls directly or indirectly into vendor-specific database protocols. The Network Protocol driver makes use of middleware (application servers) to convert JDBC calls directly or indirectly into vendor-specific database protocols. Also Because all of the database connectivity drivers are present on a single server, no individual client-side installation is required.

4. Type-4 driver or Thin driver

Type-4 drivers are also known as native protocol drivers. The Network Protocol driver makes use of middleware (application servers) to convert JDBC calls directly or indirectly into vendor-specific database protocols.This driver communicates directly with the database. Because it does not require a native database library, it is also known as Thin Driver. There is no need for a native library or a middle-ware server, so no client-side or server-side installation is required. They are portable drivers because they are entirely written in Java.

JDBC MySQL Connection

Prerequisites:

  1. Java should be installed in your system.
  2. Your system should have MySQL installed.
  3. Your computer should have an IDE installed. (In this tutorial we will be using Eclipse IDE).

Let us create a program now,

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBC {
      public static void main(String[] args) throws ClassNotFoundException, SQLException {
           Class.forName("com.mysql.jdbc.Driver");
           String url = "jdbc:mysql://localhost:3306/sys";
           String username = "root";
           String password = "tiger";
           Connection connection = DriverManager.getConnection(url, username, password);
           System.out.println("Connection Successful");
      }
}
  • Change the schema name (‘sys’ as specified in the URL), username, and password in the above program to connect to the MySQL database.
  • You will also need to add the JDBC driver for the MySQL jar to your program. Click here to download the MySQL connector jar.
  • Follow the below steps to add this jar file to your program,

Right-click on the project > Build Path > Configure Build Path… > Under Libraries tab > Add External JARS… > select the mysql-connector-java-8.0.22.jar Open > Apply and Close

JDBC MySQL Connector jar
JDBC MySQL Connector jar
  • Run the program now, and the output should look like below
JDBC MySQL program
JDBC MySQL program

Done. We have successfully established a connection to our MySQL database from a Java program using JDBC driver for MySQL.

Share:

Leave a Reply