Reputation: 51
I installed MySQL database & JDBC. I created a database called feedback through MySQL command line client, and I want to access that database through a java program.
Here is the commands I used on MySQL command line client create the database.
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');
Then, the code in 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;
}
}
}
The Main Class:
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();
}
}
However, I get the following exception:
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)
I am following the tutorial in the following link
Any help?
Upvotes: 0
Views: 2757
Reputation: 2978
usually when having this exception:
Access denied for user 'sqluser'@'localhost' to database 'feedback'
means that this user don't have enough privileges to access the Database, what I would suggest you to do is to go to your SQL shell and write this command:
show grants;
it would show you which users has access to the Database and with their level of privilege
if this user don't seem to have the necessary privilege to access the DB you can grant it to him by writing this query in your SQL Shell
GRANT ALL PRIVILEGES ON *.* TO 'sqluser'@'localhost' WITH GRANT OPTION;
Good Luck;
Upvotes: 1
Reputation: 51423
The user sqluser
does not have the privileges to access database feedback
on the localhost
.
Use the mysql command line tool and grant the privileges to the user.
Do not forget to flush privileges
Upvotes: 1