'How to connect to online mysql database from Java desktop app

I pave paid hosting, and i added my database to it. I am trying to connect to this online database from my java desktop application but, i got exception: Communications link failure.

here is my code:

public Kviz_DAO(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename?"+
                "user=mydbuser&password=mydbpassword");
    }
    catch(Exception ex){
        ex.printStackTrace();
    }
}

Can anyone tell me what i'm doing wrong.



Solution 1:[1]

Check that, if permission is in the DB server for your IP. If not then GRANT the permission for the IP

GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'@'%' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

if not working, check the FIREWALL.

Solution 2:[2]

The default port for MySQL is 3306. Are you certain that the hostname and port you're using is correct?

Try this:

    konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename", mydbuser, mydbpassword);

Check this:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

Can you connect using the MySQL admin?

Solution 3:[3]

sample code:

String url1 = "jdbc:mysql://localhost:3306/";
String db1 = "userdb";
String driver1 = "com.mysql.jdbc.Driver";
String user1 = "root";
String pass1 = "sarakrish";
  try {
          Class.forName(driver1).newInstance();
          con = DriverManager.getConnection(url1 + db1, user1, pass1);

you should try this method:

String url = "jdbc:mysql://penal.ba:2082/";
String db = "mydatabasename";
String driver = "com.mysql.jdbc.Driver";
String user = "mydbuser";
String pass = "mydbpassword";
            Class.forName("com.mysql.jdbc.Driver");
            konekcija = DriverManager.getConnection(url+db,user,pass);

Solution 4:[4]

Download the connector J . Add it to your classpath and in the code:

// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
  .getConnection("jdbc:mysql://remoteUri/database-name?"
      + "user=user&password=userpw");

Solution 5:[5]

Connection konekcija = DriverManager.getConnection("jdbc:mysql://penal.ba:2082/mydatabasename?" + "user=mydbuser&password=mydbpassword");
//konekcija should be an object of Connection class.....I think :-p

Solution 6:[6]

If you can't connect to a remote MySql database, this is due to permissions by the hosting company/server. You can solve this by adding permission for your ip.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Deepu--Java
Solution 2 Community
Solution 3 jmail
Solution 4 Grigoris Loukidis
Solution 5 YMomb
Solution 6 DW_