我安装了MySQL数据库& JDBC。我通过MySQL命令行客户端创建了一个名为feedback的数据库,我想通过java程序访问该数据库。
这是我在MySQL命令行客户端上使用的命令创建数据库。
create database feedback;
use feedback;
CREATE USER sqluser IDENTIFIED BY 'sqluserpw';
grant usage on *.* to sqluser@localhost identified by 'sqluserpw';
grant all privileges on feedback.* to sqluser@localhost;
CREATE TABLE COMMENTS (id INT NOT NULL AUTO_INCREMENT,
MYUSER VARCHAR(30) NOT NULL);
INSERT INTO COMMENTS values (default, 'lars');
然后,Java中的代码:
package de.vogella.mysql.first;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import javax.swing.JOptionPane;
public class MYSQLACCESS {
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public void readDataBase() throws Exception {
JOptionPane.showMessageDialog(null, "Before try" );
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
JOptionPane.showMessageDialog(null, "1" );
connect = DriverManager.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement.executeQuery("select * from FEEDBACK.COMMENTS");
// writeResultSet(resultSet);
String my_user = resultSet.getString("MYUSER");
System.out.println(my_user);
JOptionPane.showMessageDialog(null, my_user );
} catch (Exception e) {
throw e;
}
}
}
主要类别:
package de.vogella.mysql.first;
import de.vogella.mysql.first.MYSQLACCESS;
public class Main {
public static void main(String[] args) throws Exception {
MYSQLACCESS first_access = new MYSQLACCESS();
first_access.readDataBase();
}
}
但是,我得到以下例外:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Access denied for user 'sqluser'@'localhost' to database 'feedback'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
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.checkErrorPacket(MysqlIO.java:928)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2493)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2526)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2311)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at de.vogella.mysql.first.MYSQLACCESS.readDataBase(MYSQLACCESS.java:30)
at de.vogella.mysql.first.Main.main(Main.java:7)
我正在遵循以下link
中的教程任何帮助?
答案 0 :(得分:1)
答案 1 :(得分:1)
通常在遇到此异常时:
拒绝用户'sqluser'@'localhost'访问数据库'feedback'
表示该用户没有足够的权限访问数据库,我建议你去做你的SQL shell并编写这个命令:
show grants;
它会显示哪些用户可以访问数据库及其权限级别
如果此用户似乎没有访问数据库的必要权限,您可以通过在SQL Shell中编写此查询来将其授予他
GRANT ALL PRIVILEGES ON *.* TO 'sqluser'@'localhost' WITH GRANT OPTION;
祝你好运;