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.
This means that the given user isn’t been granted access to the database which you’re attempting to connect. You’d need to issue the following SQL command with MySQL admin rights:
Note that the user name and password are case sensitive.
Also note that this has after all nothing to do with reading properties files. You’d have exactly the same problem when supplying username/password/database as hardcoded string variables.