标签:NPU inpu 文件名 stat ack info rop manager str
//第一种方式
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?user=root&password=root") ;
//第二种方式
//读取properties文件
Properties pro = new Properties() ;
InputStream in = JdbcDemo3.class.getClassLoader().getResourceAsStream("dbcfg.properties") ;
pro.load(in) ;
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", pro) ;
//第三种方式
Connection conn = DriverManager.getConnection(url,user,password) ;
输入文件名和扩展名(.properties)
private static String driverClass = "" ;
private static String url = "" ;
private static String user = "" ;
private static String password = "";
static{
ResourceBundle rb = ResourceBundle.getBundle("dbcfg") ;
driverClass = rb.getString("driverClass") ;
url = rb.getString("url") ;
user = rb.getString("user") ;
password = rb.getString("password") ;
try {
Class.forName(driverClass) ;//自动注册驱动
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(url, user, password) ;
} catch (SQLException e) {
e.printStackTrace();
}
return null ;
}
原文链接:https://www.cnblogs.com/xiezie/p/5689410.html
JDBC 创建连接对象的三种方式 、 properties文件的建立、编辑和信息获取
标签:NPU inpu 文件名 stat ack info rop manager str
原文地址:https://www.cnblogs.com/biaogejiushibiao/p/9289230.html