1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| String url = "jdbc:mysql://localhost:3306/wineCulturalJewel"; String user = "root"; String password = "1234";
try (Connection connection = DriverManager.getConnection(url, user, password)) {
if (connection != null && connection.isValid(5)) { System.out.println("数据库连接成功!"); } else { System.out.println("数据库连接失败!"); } } catch (SQLException e) { System.out.println("连接数据库时发生错误:"); e.printStackTrace(); }
|