📘 Lesson  ·  Lesson 56

JDBC MySQL Connection

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 चलाने देता है।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n