我正在尝试使用注入到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”有什么问题?

谢谢。

最佳答案

在我添加之后

grant USAGE on *.* TO 'appuser'@'localhost' identified by 'adminadmin';


一切正常...我不明白为什么。

08-07 05:26