0

I am trying to connect Mysql using "mysql-connector-java-5.1.13". I have downloaded it and put it to the lib of a project that i am working on Netbeans. And i found a free host and create and Mysql database. And now i am trying the following code;

   import java.sql.*;

   public class MysqlConnect
   {
       public static void main (String[] args)
       {
           Connection conn = null;

           try
           {
               String userName = "a6990612_jdict";
               String password = "0jdict";
               String url = "jdbc:mysql://216.108.239.44:3306/a6990612_jdict";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               System.err.println ("Cannot connect to database server");
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
       }
   }

But exception catched and it says: Cannot connect to database server.

Is there be a problem with the url, since i am not sure about what will i put there, i put the ip adress of the free host that i am on.

Any idea how can i connect Mysql? Thanks in advance!

makyol
  • 213
  • 8
  • 20
  • 2
    You need to look at the details of the Exception. Try changing the catch block to e.printStackTrace(); and add that to you question. – Dunderklumpen Jul 15 '10 at 01:03
  • 1
    Adding to previous comment: change this line - System.err.println ("Cannot connect to database server") - to e.printStackTrace() – Cambium Jul 15 '10 at 01:07
  • possible duplicate of [Connecting to a MySQL database.](http://stackoverflow.com/questions/1640910/connecting-to-a-mysql-database) – OMG Ponies Jul 15 '10 at 01:10

3 Answers3

3

Check with your webhosting whether they allow you to do remote connection. If you have access to ssh, check your my.cnf configuration whether you are allowed to connect remotely.

Joshua Partogi
  • 16,167
  • 14
  • 53
  • 75
1

I would try to validate the username/password and host with a different tool (perhaps the MySQL command line tool).

Aaron Bush
  • 111
  • 1
1

It's foolish to print out a message instead of the stack trace. Write it like this:

   import java.sql.*;

   public class MysqlConnect
   {
       public static void main (String[] args)
       {
           Connection conn = null;

           try
           {
               String userName = "a6990612_jdict";
               String password = "0jdict";
               String url = "jdbc:mysql://216.108.239.44:3306/a6990612_jdict";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               conn = DriverManager.getConnection (url, userName, password);
               System.out.println ("Database connection established");
           }
           catch (Exception e)
           {
               e.printStackTrace();
           }
           finally
           {
               if (conn != null)
               {
                   try
                   {
                       conn.close ();
                       System.out.println ("Database connection terminated");
                   }
                   catch (Exception e) { /* ignore close errors */ }
               }
           }
       }
   }

I'm guessing that the driver class isn't found; you're getting a ClassNotFoundException.

Putting the MySQL connector JAR into the /lib isn't sufficient; you have to add it to your runtime classpath in Netbeans. Maybe you should look into that.

I'd recommend trying your code on a local instance first to make sure it works. If it does, and connecting to the hosted database fails, you'll know that the problem is solely due to hosting issues.

duffymo
  • 305,152
  • 44
  • 369
  • 561