获取数据库连接的五种方式
方式一
@Test
public void test01() throws Exception {
Driver driver = new Driver();
String url="jdbc:mysql://127.0.0.1:3306/jdbc";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","111111");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
方式二
@Test
public void test02() throws Exception {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
String url="jdbc:mysql://localhost:3306/jdbc";
Properties properties = new Properties();
properties.setProperty("user","root");
properties.setProperty("password","123456");
Connection connect = driver.connect(url, properties);
System.out.println(connect);
}
方式三
@Test
public void test03() throws Exception {
Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver)aClass.newInstance();
DriverManager.registerDriver(driver);
String url="jdbc:mysql://localhost:3306/jdbc";
String user="root";
String password="123456";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
方式四
@Test
public void test04() throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/jdbc";
String user="root";
String password="123456";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
方式五
@Test
public void test05() throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}