0

I'm using properties file for Database,and here is mycode:

And I have set my database.prperties file in straight src folder.

here is my code(I'm applying this code in a jsp page):

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);

Here is my properties file :

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

But I'm getting this error java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES) at line Connection con = DriverManager.getConnection(url,username,password);

Any inputs on this context will appreciated.

4

1 に答える 1

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

これは、指定されたユーザーが、接続しようとしているデータベースへのアクセスを許可されていないことを意味します。MySQL 管理者権限で次の SQL コマンドを発行する必要があります。

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

ユーザー名とパスワードは大文字と小文字が区別されることに注意してください。

また、これはプロパティ ファイルの読み取りとはまったく関係がないことにも注意してください。ユーザー名/パスワード/データベースをハードコードされた文字列変数として指定すると、まったく同じ問題が発生します。

于 2012-11-01T13:41:41.407 に答える