📘 Lesson · Lesson 56
JDBC MySQL Connection
Connect Java with MySQL using JDBC
JDBC (Java Database Connectivity) lets a Java program connect to MySQL and run queries.
Java Program
import java.sql.*;
public class DBTest {
public static void main(String[] a) throws Exception {
String url = "jdbc:mysql://localhost:3306/school";
Connection con = DriverManager.getConnection(url, "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT name FROM students");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
con.close();
}
}
Output
Aman
Riya
How it Works
- DriverManager.getConnection() opens the database link.
- Statement runs the query; ResultSet reads rows; always close the connection.
Summary
- JDBC (Java Database Connectivity) lets a Java program connect to MySQL and run queries.
Connect Java with MySQL using JDBC
JDBC (Java Database Connectivity) Java program को MySQL से connect करके queries चलाने देता है।
Java Program
import java.sql.*;
public class DBTest {
public static void main(String[] a) throws Exception {
String url = "jdbc:mysql://localhost:3306/school";
Connection con = DriverManager.getConnection(url, "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT name FROM students");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
con.close();
}
}
Output
Aman
Riya
कैसे काम करता है
- DriverManager.getConnection() database link खोलता है।
- Statement query चलाता है; ResultSet rows पढ़ता है; connection हमेशा close करें।
सारांश
- JDBC (Java Database Connectivity) Java program को MySQL से connect करके queries चलाने देता है।
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.