gpt4 book ai didi

java - 使用属性文件获取错误数据库

转载 作者:行者123 更新时间:2023-12-02 07:28:51 25 4
gpt4 key购买 nike

我正在使用数据库的属性文件,这是我的代码:

我已将我的database.prperties 文件设置在直接src 文件夹中。

这是我的代码(我在 jsp 页面中应用此代码):

Properties prop=new Properties();
InputStream inputStream=null;
try{
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("database.properties");
prop.load(inputStream);
}
finally{
if (inputStream != null) try { inputStream.close(); } catch (IOException ignore) {}
}

String driver=prop.getProperty("driver");
if (driver != null)
{
System.setProperty("driver", driver);
}
String url = prop.getProperty("url");
String username= prop.getProperty("username");
String password = prop.getProperty("password");

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,username,password); // Getting error at this line.
Statement stmt = con.createStatement();
String sql = "select * from info;";
ResultSet rs = stmt.executeQuery(sql);
System.out.println(sql);

这是我的属性文件:

driver=com.mysql.jdbc.Driver  
url=jdbc:mysql://localhost/abc
username=crips
password=drift

但是我在 Connection con = DriverManager. getConnection(url,用户名,密码);

任何有关此上下文的意见都将受到赞赏。

最佳答案

java.sql.SQLException: Access denied for user 'crips'@'localhost'

这意味着给定用户未被授予访问您尝试连接的数据库的权限。您需要使用 MySQL 管理员权限发出以下 SQL 命令:

GRANT ALL ON abc.* TO 'crips'@'localhost' IDENTIFIED BY 'drift';

请注意,用户名和密码区分大小写。

另请注意,这毕竟与读取属性文件无关。当提供用户名/密码/数据库作为硬编码字符串变量时,您会遇到完全相同的问题。

关于java - 使用属性文件获取错误数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13178051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com