public class DbUtil2 {
private static String url = null;
private static String user = null;
private static String password = null;
private static String driverClass = null;
static {
try {
Properties pro = new Properties();
//FileInputStream in = new FileInputStream("./src/db.properties");
InputStream in=DbUtil2.class.getResourceAsStream("/db.properties");
pro.load(in);
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driverClass = pro.getProperty("driverClass");
Class.forName(driverClass);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
DbUtil2 db = new DbUtil2();
System.out.println(db.getConn());
}
public Connection getConn() throws SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
// TODO Auto-generated method stub
}
}
db.properties:
url=jdbc:mysql://localhost:3306/test
user=root
password=
driverClass=com.mysql.jdbc.Driver