我试图使用注入servlet(glassfish)的javax.sql.DataSource
@Resource (name="jdbc/MysqlDS" )
javax.sql.DataSource mysqlDS;
以下语句失败,抛出" java.sql.SQLException:访问被拒绝用户' userapp' @' localhost' (使用密码:是)" :
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' ?
谢谢。
答案 0 :(得分:0)
我添加
后grant USAGE on *.* TO 'appuser'@'localhost' identified by 'adminadmin';
一切正常......我不明白为什么。