I'm trying to connect to a MySql database from a test-app in Java. I have just followed a plain tutorial I found online. The code works (and compiles OK), but I cant log in to the db and fetch anything. Getting the error:
1:
Error connecting to DB: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user '******'@'%' to database '*****_javaapp' java.lang.NullPointerException
Why does Java add the '@', and the '%' symbols in the console? Is that why I cant log in?
I am not able to modify priveliges on the db user. It's just set to "default" I guess by the ISP provider...
I have discussed this '@' & '%' issue with my friend, and I understand that these symbols are MySql syntax for "accept anything", but I still dont know what do to fix it as my MySql knowlede is limited..
If I modify some details in the db connection I sometimes get:
2:
Error connecting to DB: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. java.lang.NullPointerException
My code My Main class:
package test;
import java.awt.EventQueue;
public class Main {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
// DB CONNECT
DBConnect connect = new DBConnect();
connect.getData();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
My connect class:
package test;
import java.sql.*;
public class DBConnect {
private Connection con;
private Statement st;
private ResultSet rs;
public DBConnect() {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://mywebhostadress:3306/some_db_name","some_db_user", "somepassword");
st = con.createStatement();
} catch (Exception ex) {
System.out.println("Error connecting to DB: " + ex);
}
}
public void getData() {
try {
String query = "select * from Client";
rs = st.executeQuery(query);
System.out.println("Records from db: ");
while (rs.next()) {
String org_number = rs.getString("org_number");
String org_name = rs.getString("org_name");
System.out.println("org_number: " + org_number + "org_name: " + org_name);
}
} catch (Exception exgetData) {
System.out.println(exgetData);
}
}
}
Thanks for any helpfull tips!
UPDATE:
Hmmm…
In MySQL Workbench I manage to set up the connection OK… But I get the same error here:
“The account you are currently using does not have sufficient privileges to make changes to MySQL users and privileges.”
Also, while trying to forward engineer in MySQL Workbench:
ERROR 1044: Access denied for user 'some_db_user'@'%' to database 'new_schema'
SQL Statement:
CREATE SCHEMA new_schema
I have also tried to do it directly in phpMyAdmin, but there I can see this error on the front page:
“Create new database - No Privileges”
I have tried to grant my default (and only) user all the rights I can give, but it doesn’t get accepted when I do a query in phpMyAdmin. I Guess this is why I can’t access it from my Java application. But I might type something wrong…
Shouldn’t it be:
GRANT ALL PRIVILEGES ON some_db_name.* TO 'some_db_user'@'%' WITH GRANT OPTION; ?
(taken from: this post)
I get: Error SQL query: GRANT ALL PRIVILEGES ON some_db_name . * TO 'some_db_user '@'%' WITH GRANT OPTION MySQL said: