Connecting Java Applications to MySQL
Connecting a Java application to a MySQL database is typically done using JDBC (Java Database Connectivity). JDBC provides a standard API for Java programs to interact with relational databases such as MySQL.
Prerequisites
Java Development Kit (JDK) installed
MySQL Server installed and running
MySQL JDBC Driver (Connector/J)
Download from MySQL or add via a build tool (Maven/Gradle)
Maven Dependency
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
</dependency>
Steps to Connect Java to MySQL
1. Load the JDBC Driver
(For modern JDBC versions, this step is optional)
Class.forName("com.mysql.cj.jdbc.Driver");
2. Create Database URL
String url = "jdbc:mysql://localhost:3306/mydatabase?useSSL=false&serverTimezone=UTC";
localhost – MySQL server location
3306 – Default MySQL port
mydatabase – Database name
3. Establish Connection
import java.sql.Connection;
import java.sql.DriverManager;
String username = "root";
String password = "password";
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected to MySQL successfully!");
4. Execute SQL Queries
Using Statement
import java.sql.Statement;
import java.sql.ResultSet;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
Using PreparedStatement (Recommended)
import java.sql.PreparedStatement;
String sql = "INSERT INTO users (name, email) VALUES (?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "John");
pstmt.setString(2, "john@example.com");
pstmt.executeUpdate();
✔ Prevents SQL Injection
✔ Improves performance
5. Close Resources
rs.close();
stmt.close();
conn.close();
Or use try-with-resources (recommended):
try (Connection conn = DriverManager.getConnection(url, username, password);
PreparedStatement pstmt = conn.prepareStatement(sql)) {
// execute query
}
Complete Example
import java.sql.*;
public class MySQLConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/mydatabase";
String user = "root";
String pass = "password";
try (Connection conn = DriverManager.getConnection(url, user, pass)) {
System.out.println("Connected successfully");
String query = "SELECT * FROM users";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getInt("id") + " " + rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Common Issues & Solutions
Issue Solution
Driver not found Add MySQL Connector/J
Access denied Check username/password
Time zone error Add serverTimezone=UTC
SSL error Use useSSL=false
Best Practices
Use PreparedStatement instead of Statement
Close connections properly
Use connection pooling (HikariCP, Apache DBCP) for large applications
Store DB credentials securely (not hardcoded)
Conclusion
Connecting Java applications to MySQL using JDBC is straightforward and powerful. With proper driver configuration, secure coding practices, and resource management, Java–MySQL integration can support robust and scalable applications.
Learn Full Stack JAVA Course in Hyderabad
Read More
CRUD Operations in MySQL for Java Developers
Introduction to Relational Databases
Testing REST APIs with Postman
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments