Understanding JDBC with MySQL

JDBC (Java Database Connectivity) is a Java API used to connect a Java application with a MySQL Database and execute SQL statements. In the non-Maven approach, we manually download the MySQL JDBC driver JAR and include it in the classpath while compiling and running the Java program. For MySQL, the JDBC driver is provided by MySQL Connector/J. The Connector/J JAR must be included in the application’s classpath when using the manual classpath approach.

Step 1: Install and Start MySQL Database

Make sure MySQL Server is installed and running.

Example connection details:

Host     : localhost
Port     : 3306
Database : jdbc_demo
Username : root
Password : password

Replace password with the actual password configured for your MySQL user.

Step 2: Download the MySQL JDBC Driver

Go to the official MySQL Connector/J download page:

MySQL Connector/J – Official Download

MySQL Connector/J is the JDBC driver that allows Java applications to communicate with MySQL.

After downloading and extracting the package, locate the JAR file.

For example:

mysql-connector-j-9.x.x.jar

The exact version depends on the Connector/J release you download.

For this example, assume:

mysql-connector-j-9.x.x.jar

Step 3: Create the Project Folder Structure

Create the following folders:

MySQLJDBC/

├── mysql-connector-j-9.x.x.jar

├── StudentApp.java

Mysql-AI

 

Step 4: Create the Database Table

Before running the Java program, create a database in MySQL:

CREATE DATABASE jdbc_demo;

Select the database:
USE jdbc_demo;

Create the student table:
CREATE TABLE student (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);

Insert sample data:
INSERT INTO student VALUES (1, 'Raja');
INSERT INTO student VALUES (2, 'Ram');Code language: PHP (php)

Step 5: Write the JDBC Program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class StudentApp {

    public static void main(String[] args) {

        try {

            // Load MySQL JDBC Driver
            Class.forName("com.mysql.cj.jdbc.Driver");

            // Create Connection
            Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/jdbc_demo",
                "root",
                "password"
            );

            // Create Statement
            Statement st = con.createStatement();

            // Execute Query
            ResultSet rs = st.executeQuery(
                "SELECT * FROM student"
            );

            // Display Results
            while (rs.next()) {

                System.out.println(
                    rs.getInt("id") + " " +
                    rs.getString("name")
                );
            }

            // Close Resources
            rs.close();
            st.close();
            con.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 6: Open Command Prompt or PowerShell

Navigate to the project folder.

For example:

PowerShell

cd D:\MySQLJDBC

Step 7: Compile Using the Classpath

Compile the Java program:

javac -classpath “.;D:\MySQLJDBC\mysql-connector.jar” StudentApp.java

run the program:

java -classpath “.;D:\MySQLJDBC\mysql-connector.jar” StudentApp

Execution and Output:

PS D:\MySQLJDBC> javac -classpath ".;D:\MySQLJDBC\mysql-connector.jar" StudentApp.java
PS D:\MySQLJDBC> java -classpath ".;D:\MySQLJDBC\mysql-connector.jar" StudentApp
1 Raja
2 RamCode language: CSS (css)
Scroll to Top