public class JdbcTest { public static void main(String[] args) { String dbURL = "jdbc:sqlserver://127.0.0.1:1433; DatabaseName=数据库名"; // 连接字符串 String userName = "sa"; String userPwd = "123456"; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //驱动 Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接数据库成功"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); System.out.print("连接失败"); } } }
SQLServer如上。
MySQL如下:mysql8以上的url为:jdbc:mysql://localhost:3306/thrcloud_db01?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
public class JdbcTest { public static void main(String[] args) { String dbURL = "jdbc:mysql://localhost:3306/数据库名"; // 连接字符串 String userName = "sa"; String userPwd = "123456"; try { Class.forName("com.mysql.jdbc.Driver"); //驱动 Connection dbConn = DriverManager.getConnection(dbURL, userName, userPwd); System.out.println("连接数据库成功"); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); System.out.print("连接失败"); } } }