propreties信息:
#注释
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=(这里是用户名)
password=(这里是密码)
public class DBUtil {
static private DBUtil util;
private String driver;
private String url;
private String username;
private String password;
private DBUtil() {
Properties prop = new Properties();
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("db.properties"));
driver = prop.getProperty("driver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
public static synchronized DBUtil getInstance() {
if (null == util) {
util = new DBUtil();
}
return util;
}
public Connection getConnection() throws Exception {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,username,password);
return conn;
}
public static void main(String[] args) {
try {
System.out.println(DBUtil.getInstance().getConnection());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}