0% found this document useful (0 votes)
16 views1 page

Java MySQL Database Connection Example

The document is a Java program that establishes a connection to a MySQL database named 'school'. It uses the JDBC DriverManager to connect with specified credentials and handles potential SQL exceptions. If the connection is successful, it prints a success message; otherwise, it prints an error message.

Uploaded by

BH GAMER
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Java MySQL Database Connection Example

The document is a Java program that establishes a connection to a MySQL database named 'school'. It uses the JDBC DriverManager to connect with specified credentials and handles potential SQL exceptions. If the connection is successful, it prints a success message; otherwise, it prints an error message.

Uploaded by

BH GAMER
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd

import [Link].

Connection;
import [Link];
import [Link];

public class SchoolDBConnection {


public static void main(String[] args) {
// Database credentials and URL
String url = "jdbc:mysql://localhost:3306/school"; // 'school' is your DB name
String username = "root"; // replace with your MySQL username
String password = "1234"; // replace with your MySQL password

// Try-with-resources automatically closes connection


try (Connection con = [Link](url, username,
password)) {
if (con != null) {
[Link]("✅ Database connection successful!");
} else {
[Link]("❌ Connection failed!");
}
} catch (SQLException e) {
[Link]("❌ Error connecting to database!");
[Link]();
}
}
}

You might also like