How to Connect JDBC to SQL (Step-by-Step Guide)
Connecting Java applications to a database is one of the most important skills for any backend or full stack developer. If you're working with Java, the standard way to interact with databases is...
Jagnyadatta Dalai is a content creator on QueueVerse, sharing insights and expertise on various topics. With 0 published articles, they contribute valuable knowledge to the community.
Advertisement
In this guide, you will learn how to connect JDBC to an SQL database step by step. We will use MySQL as an example, but the same concept applies to other databases like PostgreSQL and Oracle.
What is JDBC?
- Helps Java communicate with databases
- Allows CRUD operations (Create, Read, Update, Delete)
- Used in real-world applications like banking, e-commerce, etc.
Prerequisites Before Connecting JDBC
- Java JDK installed (Java 8 or above)
- Any SQL database (MySQL used in this example)
- Basic knowledge of Java & SQL
- MySQL Server running locally or remotely
- MySQL Connector (JDBC Driver)
Step-by-Step Guide to Connect JDBC to MySQL
Step 1 – Install MySQL and Create Database
Advertisement
CREATE DATABASE testdb;
USE testdb;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100)
);
Step 2 – Add JDBC Driver
If you are using Maven:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
Step 3 – Load and Register Driver
Class.forName("com.mysql.cj.jdbc.Driver");
//This step loads the JDBC driver into memory.
Step 4 – Establish Connection
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
- jdbc:mysql://localhost:3306/testdb → database URL
- root → username
- password → your MySQL password
Step 5 – Create Statement
Advertisement
Statement stmt = conn.createStatement();
//Used to execute SQL queries.
Step 6 – Execute Query
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getInt("id") + " " +
rs.getString("name") + " " +
rs.getString("email"));
}
Step 7 – Close Connection
conn.close();
//Always close the connection to avoid memory leaks.
Complete JDBC Example Code
import java.sql.*;
public class JdbcExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb",
"root",
"password"
);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(
rs.getInt("id") + " " +
rs.getString("name") + " " +
rs.getString("email")
);
}
conn.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Common Errors and Fixes
- Error: No suitable driver found
- Error: Access denied for user
- Error: Communications link failure
Best Practices for JDBC
- Always close connection using finally or try-with-resources
- Use PreparedStatement instead of Statement (for security)
- Never hardcode database credentials
- Use connection pooling for large applications
Conclusion
Advertisement
Start practicing by creating small projects like user management systems or blog backends.
Jagnyadatta Dalai is a content creator on QueueVerse, sharing insights and expertise on various topics. With 0 published articles, they contribute valuable knowledge to the community.
View ProfileComments (2)
Please login to join the conversation
Login to CommentGreat! It helped lot sir. Thanks
Your content is super amazing it is very useful for me i apply your principles in my daily life it help me grow in my life big fan sir.
Related Articles
How to Use ChatGPT for Coding (Best Prompts)
How to Use ChatGPT for Coding (Best Prompts)
Read MoreHow to Deploy Next.js App on Vercel (Step-by-Step Guide 2026)
How to Deploy Next.js App on Vercel (Step-by-Step Guide 2026)
Read MoreHow to Deploy MERN Stack App for Free (Frontend + Backend)
How to Deploy MERN Stack App for Free (Frontend + Backend)
Read MoreAdvertisement