我想使用 Java
连接到数据库。我使用这段代码:
public static Connection getConnection() throws Exception {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:sqlserver://localhost;integratedSecurity=true;";
String username = "root";
String password = "mysql";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
System.out.println("Connected");
return conn;
} catch(Exception e) {
System.out.println(e);
}
return null;
}
但我不知道正确的默认 url
和 driver
属性值。我使用 MySQLWorkBench。
最佳答案
您使用的驱动程序是正确的,但您需要下载包含 mysql 连接器 jar 到您的构建路径中。
对于 url,mysql 默认在端口 3306 上运行,因此您的 url 将如下所示,将 [yourdatabasename] 替换为您的数据库名称,由于连接不是 https,所以设置了 useSSL
jdbc:mysql://localhost:3306/[yourDatabasename]?useSSL=false
关于java - 使用默认 URL 和驱动器连接到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003056/