db.properties的配置文件:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:XE
username=u08
password=u08
工具类
private static String driver;
private static String url;
private static String username;
private static String password;
// 静态代码块(只在类加载时执行一次)
static {
try {
// 使用相对路径,JAVA工程的路径从工程名开始查找
//FileInputStream fis = new FileInputStream("config/db.properties");
String path =DBUtil.class.getResource("/db.properties").getPath();
System.out.println("path.substring(0)"+path.substring(0));
FileInputStream fis= new FileInputStream(path.substring(1));
Properties pro = new Properties();
// 加载数据
pro.load(fis);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
System.out.println("数据库配置信息加载完毕...")