|
| 1 | +import java.sql.*; |
| 2 | +import java.util.HashMap; |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +public class TestJdbc { |
| 6 | + public static void main(String[] args) { |
| 7 | + Map<String, String> params = parseArgs(args); |
| 8 | + |
| 9 | + String host = params.get("host"); |
| 10 | + String port = params.get("port"); |
| 11 | + String database = params.get("database"); |
| 12 | + String user = params.get("user"); |
| 13 | + String password = params.get("password"); |
| 14 | + |
| 15 | + if (host == null || port == null || database == null || user == null || password == null) { |
| 16 | + System.err.println("Usage: java TestJdbc --host <host> --port <port> --database <database> --user <user> --password <password>"); |
| 17 | + System.exit(1); |
| 18 | + } |
| 19 | + |
| 20 | + String url = String.format("jdbc:mysql://%s:%s/%s?useSSL=false&serverTimezone=UTC", host, port, database); |
| 21 | + |
| 22 | + try { |
| 23 | + Class.forName("com.mysql.cj.jdbc.Driver"); |
| 24 | + System.out.println("Attempting to connect to: " + url); |
| 25 | + System.out.println("User: " + user); |
| 26 | + |
| 27 | + // 简化连接参数 |
| 28 | + String testUrl = url + "?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useServerPrepStmts=false"; |
| 29 | + |
| 30 | + try (Connection conn = DriverManager.getConnection(testUrl, user, password)) { |
| 31 | + System.out.println("Connection established successfully!"); |
| 32 | + System.out.println("Connection info: " + conn.getMetaData().getDatabaseProductName() + " " + conn.getMetaData().getDatabaseProductVersion()); |
| 33 | + |
| 34 | + // 首先执行一个简单的查询测试代理是否工作 |
| 35 | + try (Statement stmt = conn.createStatement()) { |
| 36 | + System.out.println("Testing with a simple SELECT query..."); |
| 37 | + try (ResultSet rs = stmt.executeQuery("SELECT 1 as test_column")) { |
| 38 | + System.out.println("Simple query executed successfully!"); |
| 39 | + while (rs.next()) { |
| 40 | + System.out.println("Result: " + rs.getString(1)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + System.out.println("Now executing SHOW TABLES..."); |
| 45 | + try (ResultSet rs = stmt.executeQuery("SHOW TABLES")) { |
| 46 | + while (rs.next()) { |
| 47 | + System.out.println(rs.getString(1)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } catch (ClassNotFoundException e) { |
| 53 | + System.err.println("MySQL JDBC driver not found. Make sure mysql-connector-java.jar is in classpath."); |
| 54 | + System.exit(1); |
| 55 | + } catch (SQLException e) { |
| 56 | + System.err.println("Database connection failed: " + e.getMessage()); |
| 57 | + System.err.println("SQL State: " + e.getSQLState()); |
| 58 | + System.err.println("Error Code: " + e.getErrorCode()); |
| 59 | + e.printStackTrace(); |
| 60 | + System.exit(1); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static Map<String, String> parseArgs(String[] args) { |
| 65 | + Map<String, String> params = new HashMap<>(); |
| 66 | + for (int i = 0; i < args.length; i += 2) { |
| 67 | + if (i + 1 < args.length && args[i].startsWith("--")) { |
| 68 | + String key = args[i].substring(2); |
| 69 | + String value = args[i + 1]; |
| 70 | + params.put(key, value); |
| 71 | + } |
| 72 | + } |
| 73 | + return params; |
| 74 | + } |
| 75 | +} |
0 commit comments