使用ubuntu上的Connector/J连接MySQL和Java时出错



我正在尝试做项目解释,在这里输入链接描述(代码在页面末尾),但当我运行它时,出现了错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: SELECT command denied to user 'sqluser'@'localhost' for table 'COMMENTS'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2768)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1612)
at de.vogella.mysql.first.MySQLAccess.readDataBase(MySQLAccess.java:30)
at de.vogella.mysql.first.Main.main(Main.java:7)

我认为这个错误可能与CLASSPATH有关,但我一点也不确定。这个错误的原因是什么?我该如何解决?

这个错误的根本原因解释得很清楚:

SELECT command denied to user 'sqluser'@'localhost' for table 'COMMENTS'

只需确保创建一个名为sqluser的mysql用户,并授予它对'COMMENTS'表的适当权限。

快速且肮脏:GRANT ALL PRIVILEGES ON your_db_name.* TO 'sqluser'@localhost IDENTIFIED BY 'sqluserpw';

最新更新