数据库连接工具类
public class DBUtil {
//获得输入流private static InputStream in = DBUtil.class.getClassLoader().getResourceAsStream("config.properties");
private static Properties prop = new Properties();
public static Connection getConnection() throws ClassNotFoundException, SQLException{
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
Connection conn=null;
//加载驱动
String driver=prop.getProperty("jdbc.driverClassName");//"oracle.jdbc.OracleDriver";
Class.forName(driver);
//建立连接
String url=prop.getProperty("jdbc.url");//"jdbc:oracle:thin:@10.1.55.241:1521:test";
String username=prop.getProperty("jdbc.username");
String password=prop.getProperty("jdbc.password");
conn=DriverManager.getConnection(url,username,password);
return conn;
}
}
我的config.properties放在了web工程的src目录下。
properties配置文件内容
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.1.55.241:1521:test
jdbc.username=bomc
jdbc.password=bomc
jdbc.show_sql=true