连接数据库的三种方式
- 方式一:
@Test
public void test1(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String s="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
Connection conn = DriverManager.getConnection(s, "root", "123456");
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
- 方式二:
@Test
public void test2(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String s="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
Properties p = new Properties();
p.setProperty("user","root");
p.setProperty("password","123456");
Connection conn = DriverManager.getConnection(s, p);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
- 方式三:
@Test
public void test3(){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String s="jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC&characterEncoding=utf8";
InputStream ss = Test12.class.getClassLoader().getResourceAsStream("my.properties");
Properties p = new Properties();
p.load(ss);
Connection conn = DriverManager.getConnection(s, p);
System.out.println(conn);
} catch (Exception e) {
e.printStackTrace();
}
}