Skip to content

Instantly share code, notes, and snippets.

@lakshyaraj2006
Last active January 16, 2026 13:59
Show Gist options
  • Select an option

  • Save lakshyaraj2006/7199e7239f7553b6fd0de598d4510ae0 to your computer and use it in GitHub Desktop.

Select an option

Save lakshyaraj2006/7199e7239f7553b6fd0de598d4510ae0 to your computer and use it in GitHub Desktop.
Connect jdbc to mysql

JDBC + MySQL

Installer files

Important

  • First, download & configure the server.
  • Install the workbench
  • Add the driver (steps below)

Steps for adding driver

  • Download the MySQL Connector/J from the link given above
  • Extract the files
  • You will get a .jar file
  • Copy and paste the file to C:\jdbc

Add to project configuration

  • Open IntellijIdea
  • Go to File > Project Structure > Libraries
  • Click on +, and then on Java
  • Locate the .jar file & click OK

Create the database before connecting in Java

  • Syntax: CREATE DATABASE <database_name>;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
class Main {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/<database_name>";
String user = "<username>"; // default: root
String password = "<password>";
try (Connection conn = DriverManager.getConnection(url, user, password)) {
System.out.println("Connected to MySQL");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment