配置文件
driver=com.mysql.cj.jdbc.Driver
jdbcUrl=jdbc:mysql://127.0.0.1:3306/java_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
user=root
password=123456
java代码中对文件进行调用
package peizhi;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
public class a1 {
public Connection getConnection() throws Exception{
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
InputStream in = getClass().getClassLoader().getResourceAsStream("db.properties");
//System.out.println(in.available());
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password");
Class.forName(driverClass);
System.out.println("driverClass:" + driverClass);
System.out.println("password:" + password);
System.out.println("user:" + user);
Connection conn=DriverManager.getConnection(jdbcUrl, user, password);
return conn;
}
public static void main(String[] args) throws Exception{
//TODO Auto-generated method stub
a1 t2 = new a1();
System.out.println(t2.getConnection());
}
}
注意
在运行的时候要选择自己对应的那个类[图片]
如下:
选择下面那个。