1

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:

1044 - Access denied for user 'some_db_user '@'%' to database ' some_db_name '

Community
  • 1
  • 1
CustomCase
  • 247
  • 1
  • 4
  • 15
  • The second error is simply a connection error. Try accessing your MySQL DB through the MySQL command line client or Workbench to check if it's up. – ydaetskcoR Jul 21 '14 at 12:58
  • I didn't manage to connect via the MySql Workbench either. Sorry, forgot to mention that in the first post!... First attemt: Workbench crashed. Second attemt. "Failed to connect"... :/ I have also tried to set up as localhost.... – CustomCase Jul 21 '14 at 13:04
  • Make sure you're actually running an instance of MySQL Server. The MySQL daemon can be found under `/bin/mysqld`. Running this will start your server and then you should be able to connect to `jdbc:mysql://localhost:3306/` – ydaetskcoR Jul 21 '14 at 14:18

2 Answers2

1

The error you're seeing:

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

is simply a connection error. Normally I'd suggest that you should try accessing your MySQL DB through the MySQL command line client or Workbench to check if it's up but here it like you haven't actually specified a host for the MySQL server (as you have a Null Pointer Error there).

You'll need to properly specify your host name in the form you provide in the OP. So to connect to a database called "some_db_name" on a local instance of MySQL as root (with the default blank password) you'd use:

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/some_db_name","root", "");

The @'%' part means the user can access from anywhere. Otherwise you can lock a user down a specific IP address or host name. It's possible to create a user role @% with lower privileges than one with the same user name but at a specific IP so then if that user logs in at that location they then get the roles of the specific IP. It has no relation to the error you're seeing here and it's what the default behaviour is for any added user unless they're specifically added with a host mask.

ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
0

SOLVED! I finally found the sollution.

My hosting provider had changed the settings for the default user on their new platform. (I wasn't able to change it). I have done a lot of testing on several different platforms that the provider has to offer.

-In their new system, I wasn't able to do anything because the user didn't automatically have the right priviliges...

In their old system, I was able to accesss everything (since the user had "dba" (database admin) rights).

Thanks for all the input guys.

CustomCase
  • 247
  • 1
  • 4
  • 15