方式一:
@Test
public void testConnection1() {
try {
// 1、提供java.sql.Driver接口实现类的对象
Driver driver = null;
driver = new Driver();
// 2、提供url,指明要操作的数据库
String url = "jdbc:mysql://localhost:3307/test";
// 3、提供Properties对象,指明用户名和密码
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
// 4、调用driver的connect(),获取连接
Connection conn = driver.connect(url, info);
System.out.println(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
方式二:
@Test
public void testConnection2() {
try {
// 1、实例化Driver
String className = "com.mysql.jdbc.Driver";
Class clazz = Class.forName(className);
Driver driver = (Driver) clazz.newInstance();
// 2、提供url,指明要操作的数据库
String url = "jdbc:mysql://localhost:3307/test";
// 3、提供Properties对象,指明用户名
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "123456");
// 4、调用driver的connect(),获取连接
Connection conn = driver.connect(url, info);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
方式三:
@Test
public void testConnection3() {
try {
// 1、数据库连接的4个基本要素
String url = "jdbc:mysql://localhost:3307/test";
String user = "root";
String password = "123456";
String className = "com.mysql.jdbc.Driver";
// 2、实例化Driver
Class clazz = forName(className);
Driver driver = (Driver) clazz.newInstance();
// 3、注册驱动
DriverManager.registerDriver(driver);
// 4、获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
方式四:
@Test
public void testConnection4() {
try {
// 1、获取数据库连接的4个基本要素
String url = "jdbc:mysql://localhost:3307/test";
String user = "root";
String password = "123456";
String driverName = "com.mysql.jdbc.Driver";
// 2、加载驱动(实例化Driver、注册驱动)
Class.forName(driverName);
// 实例化Driver
// Class<?> clazz = forName(driverName);
// Driver driver = (Driver) clazz.newInstance();
// 注册驱动
// DriverManager.registerDriver(driver);
// Driver类中的静态代码块随类的加载而加载
// static {
// try {
// DriverManager.registerDriver(new Driver());
// } catch (SQLException var1) {
// throw new RuntimeException("Can't register driver!");
// }
// }
// 3、获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
方式五:(最推荐的方式)
jdbc.properties 配置文件声明在工程的src目录下
# =号两边不能有空格,否则会报错
user=root
password=123456
url=jdbc:mysql://localhost:3307/test
driverClass=com.mysql.jdbc.Driver
@Test
public void testConnection5() throws Exception{
// 1、加载配置文件
InputStream is = MyTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
// 2、读取配置文件
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 3、加载驱动
Class.forName(driverClass);
// 4、获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}