0

I'm trying to create a connection to a database in my Java program. I'm not sure if i have the correct address for it though in my code.

Below is my connection code:

 public static Connection ConnectDB() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "n1nlhdb5501-02.shr.prod.ams1.secureserver.net";
            Connection conn = DriverManager.getConnection(url, "user1", "password");
            return conn;

        } catch (Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
   }

Unfortunately, Im really unsure of whether I have put the correct information into the url and the "Class.forname()" lines. I know the one is to load the actual driver and the other is the address of the database but i'm not sure if the information I have input is correct, because the connection is unsuccessful. Is there anyway I could find the exact address that i need to use in the url string through an SQL statement maybe? Also, I'm not sure if I'm loading the correct drivers for the jar file which i'm using, its called 'mysql-connector-java-5.1.35-bin.jar'

All help appreciated!

Thanks in advance Marko

Marko Vidalis
  • 331
  • 1
  • 7
  • 18
  • 1
    If you are using a MySql DB your Driver looks right. You can use a mysql client and try to connect to the DB. That way you can confirm your connection URL – Phoenix Jun 29 '15 at 09:59
  • 2
    The URL should start with `"jdbc:mysql:"` - with that it finds the MySQL driver. Also normally a database is not exposed to the outside net; you could test that with MySQL Workbench. – Joop Eggen Jun 29 '15 at 10:02

1 Answers1

1

This is how you can connect MySQL database using Java.

try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://x.x.x.x:3306/databaseName", "username", "password");
        Statement smt = con.createStatement(); //Create Statement to interact
        ResultSet r = smt.executeQuery("select * from users where(AccountID='" + q + "');");
        while (r.next()) {
          name = r.getString("name");
        }
        con.close
   } catch (Exception e) {
        e.printStackTrace();
   }

In your case URL will be like...

String URL = "jdbc:mysql://n1nlhdb5501-02.shr.prod.ams1.secureserver.net:3306/databaseName"

Edit: It seems 3306 port is closed.

Nmap scan report for n1nlhdb5501-02.shr.prod.ams1.secureserver.net (37.148.204.135)
Host is up (0.075s latency).
Not shown: 97 filtered ports

PORT     STATE  SERVICE
80/tcp   open   http
443/tcp  open   https
3306/tcp closed mysql
Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
  • Thank you very much Vicky! unfortunately, I get an error that says "Communications link failure". How could this be fixed? – Marko Vidalis Jun 29 '15 at 10:46
  • Is `MySQL` running on `3306`? – Vicky Thakor Jun 29 '15 at 10:55
  • http://stackoverflow.com/questions/6865538/solving-a-communications-link-failure-with-jdbc-and-mysql – Vicky Thakor Jun 29 '15 at 10:57
  • Yes it is, i ran the sql statement "SHOW VARIABLES WHERE Variable_name = 'port';" in phpmyadmin and it gave me the value 3306. – Marko Vidalis Jun 29 '15 at 10:59
  • 1
    @MarkoVidalis phpmyadmin probably runs locally on your server, or at least within the same network so it does have access. Contrary to the expectation expressed in your title, Java does not connect "through phpmyadmin" to your MySQL server. phpmyadmin is just an administration frontend; it is not your MySQL server. – Mark Rotteveel Jun 29 '15 at 11:03
  • Unfortunately, I'm really unsure of how to open that port 3306 to see if that will fix the problem :/ Is it something i have to change on my computer or through the phpmyadmin interface? I don't think i've understood how the ports work :/ – Marko Vidalis Jun 30 '15 at 07:23
  • Thank you @MarkRotteveel, it makes more sense now. – Marko Vidalis Jun 30 '15 at 07:24