分两个部分。第一部分是 config.properties文件
第二部分是 获取config.properties文件内容。连接oracle数据
# do not write in chinese
driverName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.0.202:1521:tarena
username=jsd1505
password=jsd1505
maxActive=1
maxWait=5000
public class ReadProperties {
private static BasicDataSource cp;
static{
try {
Properties p=new Properties();
p.load(new FileInputStream("config.properties"));
cp=new BasicDataSource();
cp.setDriverClassName(p.getProperty("driverName"));
cp.setUrl(p.getProperty("url"));
cp.setUsername(p.getProperty("username"));
cp.setPassword(p.getProperty("password"));
cp.setMaxActive(Integer.parseInt(p.getProperty("maxActive")));
cp.setMaxWait(Integer.parseInt(p.getProperty("maxWait")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
return cp.getConnection();
}
public static void closeConnection(Connection conn) {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}