Question

I was wondering if anyone can help me I am trying to connect to an online database. The database is placed on http://labs.cms.gre.ac.uk/db/mysqlnew.php but i can't seem to connect, heres what I have so far, can anyone help?

Plus do i need import anything? I have import.hava.sql.*; but that is it?

try {

  System.out.println("Attempting Database Connection");
  Class.forName("com.mysql.jdbc.Driver");
  String url = "jdbc:mysql://http://labs.cms.gre.ac.uk/database";
  connection = DriverManager.getConnection(url, "user", "pass");
  stmt = connection.createStatement();
  System.out.println("Connection made");
} catch (Exception e) {
  System.out.println("Database connection attempt failed");
  System.out.println(e);
  }
}

No correct solution

OTHER TIPS

You cannot connect to an external database like this. Database running on the server labs.cms.gre.ac.uk will be protected by the firewall and it is internal to the network in which the server is running. Therefore there is no way you will be able to connect to this database.

Even if you connect to a local database you have multiple problems in the code:

  1. You should add MySQL JDBC driver to your classpath.
  2. Connect URL you are using is wrong. Connect URL cannot contain a protocol like http:// in it. Please google for MySQL JDBC connect URL format.

Hope that helps!

It's almost that.

First, the "url" here is not the same url you use in your browser, but the jdbc string, which is usually something like

"jdbc:mysql://localhost/test";

(see What is the MySQL JDBC driver connection string?)

Second, you must have a mysql jdbc driver in your classpath (see http://dev.mysql.com/downloads/connector/j/) . The line

 Class.forName("com.mysql.jdbc.Driver");

tries to find the mysql driver.

Third, you must ensure that you have access to connect to the database. This means that the DB administrator provided your "user" and "pass" pair access to be acessed from outside the server it's running and if the mysql db connection port (default 3306) is open to receive your request. Please check with your DBA.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top