第一步:在SCR下创建一个file,写好数据库的相关信息。

#oracle数据库
driver=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
user=scott
password=yue170305 # mysql数据库
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test
user=root
password=yue170305

第二步:连接数据库

public static Connection getConnection() throws Exception {
String driverClass = null;
String jdbcUrl = null;
String user = null;
String password = null;
// 读取文件 InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream(
"jdbc.properties");
Properties properties = new Properties();
properties.load(in);
driverClass = properties.getProperty("driver");
jdbcUrl = properties.getProperty("jdbcUrl");
user = properties.getProperty("user");
password = properties.getProperty("password"); Driver driver = (Driver) Class.forName(driverClass).newInstance();
Properties info = new Properties(); info.put("user", user);
info.put("password", password);
Connection connection = driver.connect(jdbcUrl, info);
return connection; }
05-11 13:11