2

Hello i am currently building a programm that register users to mysql database. Everything works fine on localhost but when i try to connect to external database it gives me an error such as this below.

enter image description here

I have granted all privileges to the user in the database i am trying to acess and also i have the driver installed. Any ideas??

My code:

private void createEventListenerDBProperties() {
        dbSubmitBtn.addActionListener((ActionEvent e) -> {
                if (dbDriverChooser.getSelectedItem().equals("com.mysql.jdbc.Driver")) {
                  driver = (String) dbDriverChooser.getSelectedItem();
                  port = dbPortField.getText();
                  host = "jdbc:mysql://" + hostField.getText() + ":" + port + "/";
                  db = dbnameField.getText();
                  dbuser = dbUsernameField.getText();
                  dbpassword = new String(dbPasswordField.getPassword());
                }
            }
        });
    }

    private Connection instanciateDB() {
        Connection con = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(host + db, dbuser, dbpassword);
            System.out.println("Connection Established");
        } catch (ClassNotFoundException | SQLException e) {
            System.out.println("Connection not Established");
            JOptionPane.showMessageDialog(Project2.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return con;
    }
4
  • You should try to check the connectivity to the remote host on desired port. Try to telnet on the remote port Commented Dec 22, 2014 at 1:31
  • it gives me the same error in 2 ports i have checked right now and both are available for connectivity both 3306 and 3536. so i dont think thats the reason Commented Dec 22, 2014 at 1:33
  • 1
    Typical mysql configuration often sets up username@localhost meaning mysql will not allow the user to connect from remote addresses, so ensure you've setup username@*. That being said, the error you show is more indicative of tcp ip issues, so try telnet [your host] 3306 at command prompt (dos or *nix will work) and make sure you can connect. Note that the model you're trying to do (local app connects to remote mysql) is not generally advisable, those types of connections are not encrypted and should generally occur over a trusted network only (e.g. a LAN). Commented Dec 22, 2014 at 1:37
  • @KostasMatrix You've checked two ports (and they're both available for connectivity?) Awesome. Try the port mysql is configured to listen on. It will be singular and it will be on the server. Commented Dec 22, 2014 at 1:42

1 Answer 1

1

I think your way to connect is wrong. Instead of this:

con = DriverManager.getConnection(host + db, dbuser, dbpassword);

try this:

con = DriverManager.getConnection(host + db + "?user=" +  dbuser + "&password=" +dbpassword);

Check this out, it may be useful. http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.