Development tool: eclipse
Solve the problem: the storage path of the database properties file
Background introduction: When using Ali's Druid database connection pool tool, the code needs to load and read the properties file. After reading the blog for a long time, I finally found a reliable solution
Specific steps are as follows:
Right-click the project name to create a source code package: new -> Source Folder
The name is set to config, and then the configuration file is placed in this folder
The mysql configuration file code is as follows:
url=jdbc:mysql://105.105.139.202:3306/test?rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com.mysql.cj.jdbc.Driver
initialSize=10
maxActive=20
maxWait=1000
filters=wall
The java code is as follows:
package stu0316;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
public class TestDruid {
public static void main(String[] args) throws Exception {
Properties pro = new Properties();
pro.load(TestDruid.class.getClassLoader().getResourceAsStream("druid.properties"));
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
As above, the content in the configuration file can be read normally, and the database connection can be realized