Get your system ready to write and run JDBC code with this step-by-step guide
Table of Contents
What We’ll Set Up
- Install a database MySQL.
- Download and configure the JDBC driver.
- Create a practice database table.
- Verify everything works with a test connection.
1. Install a Database
We’ll use MySQL for this tutorial, but steps are similar for any other SQL.
MySQL Installation
- Download MySQL Installer:
- Visit MySQL Downloads.
- Choose the Community Server version for your OS.
- Run the Installer:
- Follow prompts, and set a root password (e.g.,
root123
). - Keep defaults for most settings.
- Follow prompts, and set a root password (e.g.,
- Verify Installation:
- Open MySQL Command Line or MySQL Workbench.
- Log in with:
mysql -u root -p # Enter your password when prompted
2. Create a Practice Database and Table
Let’s create a students
table for testing.
Step 1: Create a Database
Run in MySQL Command Line:
CREATE DATABASE jdbc_practice;
USE jdbc_practice;
Step 2: Create the students
Table
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50)
);
Step 3: Insert Sample Data
INSERT INTO students (name, email) VALUES
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com');
3. Download and Add the JDBC Driver
The JDBC driver is a JAR file that lets Java talk to your database.
MySQL Driver Setup
- Download the Driver:
- Get the latest MySQL Connector/J from here.
- Choose the Platform Independent ZIP file.
- Add the JAR to Your Project:
- Manual Setup:
- Extract the ZIP and copy the
.jar
file (e.g.,mysql-connector-j-8.0.33.jar
). - In your IDE (e.g., IntelliJ/Eclipse):
- Right-click project > Open Module Settings > Libraries > Add the JAR.
- Maven Users: Add this to
pom.xml
:
- Extract the ZIP and copy the
- Manual Setup:
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
For detailed steps on how to add an external JAR in Eclipse, visit how to add external JARs in Eclipse.
4. Verify Your Setup
Let’s write a tiny Java program to confirm everything works.
TestConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_practice";
String user = "root";
String password = "root123"; // Use your root password
try (Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students")) {
System.out.println("Connection successful! Students:");
while (rs.next()) {
String name = rs.getString("name");
String email = rs.getString("email");
System.out.println(name + " - " + email);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Expected Output:
Connection successful! Students:
Alice - alice@example.com
Bob - bob@example.com
Troubleshooting Common Issues
ClassNotFoundException: com.mysql.cj.jdbc.Driver
- Fix: Ensure the JDBC driver JAR is added to your project.
Access denied for user 'root'@'localhost'
- Fix: Verify your MySQL username/password.
Unknown database 'jdbc_practice'
- Fix: Run
CREATE DATABASE jdbc_practice;
in MySQL first.
- Fix: Run
Summary
- Installed MySQL and created a practice database.
- Added the JDBC driver to your project.
- Tested the setup with a simple Java program.
Next Up: Connecting to a Database with JDBC – Learn how to establish secure connections and handle exceptions!