1

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.

Nedimo
  • 307
  • 3
  • 9
  • 21

6 Answers6

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.

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
0

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?

Community
  • 1
  • 1
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I have changed host name into cpanels shared IP address, I fon't know if that is I supposed to do? – Nedimo Mar 24 '14 at 17:15
  • I have no idea what you're talking about. Please clarify. And maybe try connecting via MySQL admin and let us know if that worked. – duffymo Mar 24 '14 at 17:31
  • I have created mysql database in my cPanel. I can see database and tables from cPanel phpMyAdmin, but I don't know how to connect to it from java desktop app, I really don't know what is the name of my Host, and how to check it? – Nedimo Mar 24 '14 at 17:40
  • You've got bigger problems, then. Where did you get these? penal.ba:2082. What does PHP have to do with a Java solution? I'd forget about PHP admin and all that other stuff. Just write a simple Java class that connects to the database. – duffymo Mar 24 '14 at 17:56
  • penal.ba is main domain of my cPanel. I am not sure what you mean by simple Java class that connects to database, I am trying to do that in the portion of code that I posted here. My code workes fine when i use mysql localhost database, but I won't to use online mysql database. If you have some example or tutorial i would appreciated. – Nedimo Mar 24 '14 at 18:10
  • OK, now I get it. You've tested this with an instance of MySQL running locally on your machine, using "localhost" as the hostname, and everything was "fine". Now you're trying to connect to another MySQL instance on the network somewhere. Who owns that database? Do they know you're trying to connect? Unless you make arrangements with them to allow your app to do so, you will not be able to make a connection. – duffymo Mar 24 '14 at 18:14
  • I have paid web hosting, but I didn't contact to the person who is incharge for it. I didn't know that I have to do that. – Nedimo Mar 25 '14 at 20:41
  • You need to get some instruction from your web hosting service on how your app will connect to the database. I'm sure they tell you how. – duffymo Mar 25 '14 at 22:10
0

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);
jmail
  • 5,944
  • 3
  • 21
  • 35
0

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");
Grigoris Loukidis
  • 383
  • 2
  • 7
  • 23
0
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
YMomb
  • 2,366
  • 1
  • 27
  • 36
  • 1
    Please format your answer properly and explain it a bit, to make it meet the standards of this website. – AgataB Sep 01 '16 at 13:46
0

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.

DW_
  • 104
  • 1
  • 11