JDBC_ get the link database

Original video: https://www.bilibili.com/video/av67955358?p=11

 

One

We first need to create a new File configuration information in documents prepared src

Database configuration information needed

1. The user name user (usually the default is root)

2. Password password

3.url jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8

jdbc: mysql: // localhost: 3306 port number test is fixed database  

? ServerTimezone = GMT% 2B8 When using mysql-connector-java-8.0.18.jar, the problem involves setting time zones, it is necessary to finally add in url? ServerTimezone = GMT% 2B8

4.com.mysql.jdbc.Driver call methods

Of particular note password can not have spaces

 

two

Class.forName(driverClass);

Load the driver

 

three

调用 DriverManager.getConnection(url, user, password);

 

Connection conn = DriverManager.getConnection(url, user, password);

@Test
    public void connectiontest4() throws Exception {
        //读取配置文件的4个基本信息
        
        InputStream is = JUnittext.class.getClassLoader().getResourceAsStream("jdbc_properties");
        
        Properties pros = new Properties();
        pros.load(is);
        
        String user = pros.getProperty("user");
        String password = pros.getProperty("password");
        String url = pros.getProperty("url");
        String driverClass = pros.getProperty("driverClass");
        
        // 2. Load drive 
        the Class.forName (driverClass); 
        
        
        // . 3 Get Link 
        Connection Conn = the DriverManager.getConnection (URL, User, password); 
        
        System.out.println (Conn); 
        
    }

 

 

Guess you like

Origin www.cnblogs.com/jintianhekele/p/11874067.html