java代码
public ArrayList getConn() {
Properties prop = new Properties();
String url = null;
String password = null;
String username = null;
String driver = null;
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream(“db-config.properties”));
url = prop.getProperty(“url”);
password = prop.getProperty(“password”);
username = prop.getProperty(“username”);
System.out.println(password);
System.out.println(username);
driver = prop.getProperty(“dirverClass”);
System.out.println(driver);
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
statement = con.createStatement();
sql = “select * from projectlisttable”;
// sql=“SELECT * FROM userinfo WHERE name LIKE ‘%发%’”;
rs = statement.executeQuery(sql);
while (rs.next()) {
name = rs.getString(“name”);
list.add(name);
onlinebug = rs.getString(“onlinebug”);
list.add(onlinebug);
}
;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
配置文件
url=jdbc:mysql://10.0.30.171:3306/ifengqa
username=root
password=root
dirverClass=com.mysql.jdbc.Driver
遇到的问题:
Access denied for user ‘root’@‘xxx.xxx.xxx.xxx’ (using password: YES)
j解决方法:grant all privileges on . to root@’%’ identified by ‘root’;
如何读取数据库配置文件
最新推荐文章于 2023-12-27 11:10:53 发布