Method one ( not recommended ): directly by creating the implementation class of java.sql.Driver implemented by Mysql.
@Test public void testConnection () { try { // 1. Provide java.sql.Driver interface implementation class Driver driver = null ; driver = new com.mysql.jdbc.Driver (); // 2. Provide url, Specify the data for specific operations String url = "jdbc: mysql: // localhost: 3306 / test" ; // 3. Provide an object of Properties, specify the user name and password Properties info = new Properties (); info.setProperty ( "user" , "root" ); info.setProperty ( "password", "abc123" ); // 4. Call driver's connect () to get the connection Connection conn = driver.connect(url, info); System.out.println(conn); } catch (SQLException e) { e.printStackTrace(); } }
Method two ( not recommended ): Get the connection by reflecting the Driver class implemented by Mysql.
@Test public void testConnection () { try { // 1. Instantiate Driver String className = "com.mysql.jdbc.Driver" ; Class clazz = Class.forName (className); Driver driver = (Driver) clazz.newInstance ( ); // 2. Provide the url, indicating the specific operation data String url = "jdbc: mysql: // localhost: 3306 / test" ; // 3. Provide the Properties object, indicating the user name and password Properties info = new Properties ( ); info.setProperty ( "user", "root" ); info.setProperty ( "password", "abc123"); // 4. Call connect () of the driver to get the connection Connection conn = driver.connect (url, info); System.out.println (conn); } catch (Exception e) { e.printStackTrace (); } }
Method 3: To achieve database connection through java.sql.Manager, Driver needs to be registered in DriverManager.
@Test public void testConnection () { try { // 1. 4 basic elements of database connection: String url = "jdbc: mysql: // localhost: 3306 / test" ; String user = "root" ; String password = " abc123 " ; String driverName =" com.mysql.jdbc.Driver " ; // 2. Instantiate Driver Class clazz = Class.forName (driverName); Driver driver = (Driver) clazz.newInstance ();
// 3. Register Drive DriverManager.registerDriver (driver);
// 4. Get connection Connection conn = DriverManager.getConnection(url, user, password); System.out.println(conn); } catch (Exception e) { e.printStackTrace(); } }
Method 4: In the implementation class of Driver in Mysql, Driver is instantiated in the static code block and registered in DriverManager.
@Test public void testConnection () { try { // 1. 4 basic elements of database connection: String url = "jdbc: mysql: // localhost: 3306 / test" ; String user = "root" ; String password = " abc123 " ; String driverName =" com.mysql.jdbc.Driver " ;
// 2. Load driver (①Instantiate
Driver ②Register driver) Class.forName (driverName); // Driver driver = (Driver) clazz.newInstance ( ); // 3. Register Driver // DriverManager.registerDriver (driver); / * The reason why the above code can be commented out is because the driver class in mysql declares: static { try { DriverManager.registerDriver (new Driver ()); // Driver is instantiated by default, and the instantiated Driver is registered in DriverManager } catch (SQLException var1) { throw new RuntimeException ("Can't register driver!" ); } } * / // 3. Get connection Connection conn = DriverManager.getConnection (url, user, password); System.out.println (conn); } catch (Exception e) { e.printStackTrace (); } }
Method 5 ( recommended ): Use the configuration file to obtain the database connection, so that the code and the specific database are decoupled. (The configuration file jdbc.properties should be placed in the src directory)
@Test public void testConnection() throws Exception { //1.加载配置文件 InputStream is = ConnectionTest.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.Load driver Class.forName(driverClass); //4.获取连接 Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn);
}