我正在尝试使用注入 servlet (glassfish) 的 javax.sql.DataSource
@Resource (name="jdbc/MysqlDS" )
javax.sql.DataSource mysqlDS;
以下语句失败,抛出“java.sql.SQLException: Access denied for user 'userapp'@'localhost' (using password: YES)”:
con=mysqlDS.getConnection();
PreparedStatement pstmt=con.prepareStatement("select now()");
或者
con=mysqlDS.getConnection("userapp","userpassword");
PreparedStatement pstmt=con.prepareStatement("select now()");
但以下是成功的:
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;
或者
con=mysqlDS.getConnection("root","root_password");
或者
con=mysqlDS.getConnection("me","mypassword");
或者
$ mysql -u appuser --password=userpassword -D user_lindenb -e 'select now()'
+---------------------+
| now() |
+---------------------+
| 2015-08-05 10:19:07 |
+---------------------+
赠款声明如下:
grant insert,select,update,delete on user_lindenb.* TO 'appuser'@'localhost' identified by 'userpassword';
资源声明如下
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"
这个用户'userapp'有什么问题?
谢谢你。