简体   繁体   中英

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.

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 .

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?

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);

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");
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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM