-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestJDBC.java
More file actions
32 lines (27 loc) · 1.11 KB
/
Copy pathTestJDBC.java
File metadata and controls
32 lines (27 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestJDBC {
public static void main(String[] args) {
String jdbcUrl = "jdbc:mysql://localhost:3306/ebookshop";
String username = "root";
String password = "Admin123!";
try {
// Load the MySQL JDBC driver
// Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");
// Establish the connection to the database
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
// If the connection is successful, print a success message
System.out.println("JDBC connected to MySQL successfully!");
// Close the connection
connection.close();
} catch (ClassNotFoundException e) {
System.err.println("Error: MySQL JDBC Driver not found.");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("Error: Unable to connect to MySQL.");
e.printStackTrace();
}
}
}