views:

43

answers:

3

Hey there,

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!

+1  A: 

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

Aaron Bush
+1  A: 

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
+3  A: 

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.

jpartogi