Repository navigation
Expand file tree
/
Copy pathTestDatabaseConnection.java
More file actions
93 lines (80 loc) · 4.26 KB
/
Copy pathTestDatabaseConnection.java
File metadata and controls
93 lines (80 loc) · 4.26 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Test Database Connection
// Run this to test if you can connect to MySQL database
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestDatabaseConnection {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/esecurewallet?useSSL=false&serverTimezone=UTC";
String username = "root";
String password = ""; // Change if you have a password
System.out.println("========================================");
System.out.println("Testing MySQL Database Connection");
System.out.println("========================================");
System.out.println("URL: " + url);
System.out.println("Username: " + username);
System.out.println("Password: " + (password.isEmpty() ? "(empty)" : "***"));
System.out.println();
try {
// Load MySQL driver
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("✓ MySQL Driver loaded successfully");
// Try to connect
System.out.println("Attempting to connect...");
Connection conn = DriverManager.getConnection(url, username, password);
if (conn != null) {
System.out.println("✓✓✓ CONNECTION SUCCESSFUL! ✓✓✓");
System.out.println("Database: " + conn.getCatalog());
System.out.println("Auto-commit: " + conn.getAutoCommit());
// Test query
var stmt = conn.createStatement();
var rs = stmt.executeQuery("SELECT DATABASE()");
if (rs.next()) {
System.out.println("Current database: " + rs.getString(1));
}
// Check if tables exist
rs = stmt.executeQuery("SHOW TABLES");
System.out.println("\nTables in database:");
boolean hasTables = false;
while (rs.next()) {
System.out.println(" - " + rs.getString(1));
hasTables = true;
}
if (!hasTables) {
System.out.println(" (No tables found - Hibernate will create them)");
}
conn.close();
System.out.println("\n✓ Connection closed successfully");
}
} catch (ClassNotFoundException e) {
System.err.println("❌ ERROR: MySQL Driver not found!");
System.err.println("Make sure mysql-connector-j-9.4.0.jar is in your classpath");
e.printStackTrace();
} catch (SQLException e) {
System.err.println("❌❌❌ CONNECTION FAILED! ❌❌❌");
System.err.println("Error Code: " + e.getErrorCode());
System.err.println("SQL State: " + e.getSQLState());
System.err.println("Error Message: " + e.getMessage());
System.err.println("\nCommon Issues:");
if (e.getMessage().contains("Communications link failure") ||
e.getMessage().contains("Connection refused")) {
System.err.println(" → MySQL server is not running!");
System.err.println(" → Start MySQL via XAMPP Control Panel");
} else if (e.getMessage().contains("Unknown database")) {
System.err.println(" → Database 'esecurewallet' does not exist!");
System.err.println(" → Run: CREATE DATABASE esecurewallet;");
} else if (e.getMessage().contains("Access denied")) {
System.err.println(" → Wrong username or password!");
System.err.println(" → Update password in hibernate.cfg.xml");
} else if (e.getMessage().contains("timeout")) {
System.err.println(" → Connection timeout!");
System.err.println(" → Check if MySQL is running and accessible");
}
e.printStackTrace();
} catch (Exception e) {
System.err.println("❌ Unexpected error: " + e.getMessage());
e.printStackTrace();
}
System.out.println("\n========================================");
}
}