I'm trying to use a javax.sql.DataSource injected into a servlet (glassfish)
@Resource (name="jdbc/MysqlDS" )
javax.sql.DataSource mysqlDS;
The following statements fail, throwing "java.sql.SQLException: Access denied for user 'userapp'@'localhost' (using password: YES)":
con=mysqlDS.getConnection();
PreparedStatement pstmt=con.prepareStatement("select now()");
or
con=mysqlDS.getConnection("userapp","userpassword");
PreparedStatement pstmt=con.prepareStatement("select now()");
But the following were successful:
Class.forName("com.mysql.jdbc.Driver");
con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/user_lindenb", "appuser", "userpassword");
w.println("OK");
con.close(); con=null;
or
con=mysqlDS.getConnection("root","root_password");
or
con=mysqlDS.getConnection("me","mypassword");
or
$ mysql -u appuser --password=userpassword -D user_lindenb -e 'select now()'
+---------------------+
| now() |
+---------------------+
| 2015-08-05 10:19:07 |
+---------------------+
The grant was declared as follow:
grant insert,select,update,delete on user_lindenb.* TO 'appuser'@'localhost' identified by 'userpassword';
and the resources were declared as follow
asadmin --port 8138 create-jdbc-connection-pool \
--datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlDataSource \
--restype javax.sql.DataSource \
--property "password=userpassword:user=userapp:serverName=localhost:databaseName=user_lindenb" \
MysqlDS
asadmin --port 8138 create-jdbc-resource \
--connectionpoolid MysqlDS \
"jdbc/MysqlDS"
what's wrong with this user 'userapp' ?
Thank you.
after I added
grant USAGE on *.* TO 'appuser'@'localhost' identified by 'adminadmin';
everything worked fine... I don't understand why.