jdbc Database connection mode ( iteration )
Mode 5 is the final version
Mode one
@Test
public void testConnection() throws SQLException {
// 1. obtain Driver Implementation class object of
Driver driver =new com.mysql.jdbc.Driver();
//url:http://localhost:8080/gmail/key.jpg
// jdbc:mysql: agreement
// localhost:ip Address
// 3306 Default mysql Port number
// test:test database
String url="jdbc:mysql://localhost:3306/test";
// Encapsulate the user name and password in Properties
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password","root");
Connection con=driver.connect(url,info);
System.out.println(con);
}
Mode two
// Mode two Iteration of mode one
// In the following procedure, there is no third party API, Make the program more portable
@Test
public void testConnections() throws Exception {
// 1. obtain Driver Implementation class object , Using reflection
Class cla=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver)cla.newInstance();
// 2. The database that provides the connection
String url="jdbc:mysql://localhost:3306/test";
// 3. Provide the user name and password needed to connect
Properties info=new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");
// 4. Get the connection
Connection con=driver.connect(url, info);
System.out.println(con);
}
Mode three
// Mode three : Use DriverManager Replace Driver
@Test
public void testConnection3() throws Exception {
// 1. obtain Driver Implementation class object
Class clazz=Class.forName("com.mysql.jdbc.Driver");
Driver driver=(Driver)clazz.newInstance();
// 2. Provide three other connection information
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
// Registration drive
DriverManager.registerDriver(driver);
// Get the connection
Connection con=DriverManager.getConnection(url,user,password);
System.out.println(con);
}
Mode 4
// Mode 4 : You can just load the driver , No need to display the registration driver
@Test
public void testConnection4() throws Exception {
// 1. Provide basic information about three connections
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="root";
// 2. load Driver
Class.forName("com.mysql.jdbc.Driver");
// Compared with mode three , The following operations can be omitted
// Driver driver=(Driver)clazz.newInstance();
// Registration drive
// DriverManager.registerDriver(driver);
// Why you can :
/* stay MySQL Of Driver Static code blocks are declared in the implementation class to implement the registration driver
*
*/
// 3. Get the connection
Connection con=DriverManager.getConnection(url,user,password);
System.out.println(con);
}
Methods five ( Final version )
// Methods five : Connect the database to the required 4 Messages
/*
* benefits :
* 1. The separation of data and code is realized , Achieve decoupling
* 2. If you need to modify the profile information , You can avoid repackaging programs
*/
@Test
public void getConnection5() throws Exception {
// 1. Read... In the configuration file 4 A basic message
InputStream is= JdbcCreat1.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pro = new Properties();
pro.load(is);
String user=pro.getProperty("user");
String password=pro.getProperty("password");
String url=pro.getProperty("url");
String driverClass=pro.getProperty("driverClass");
// 2. The load driver
Class.forName(driverClass);
// 3. Get the connection
Connection con = DriverManager.getConnection(url,user,password);
System.out.println(con);
}
}
-
Attached configuration file
user=root password=root url=jdbc:mysql://localhost:3306/test driverClass=com.mysql.jdbc.Driver