开发者

How can i connect Mysql by using Java?

开发者 https://www.devze.com 2023-01-07 14:09 出处:网络
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

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!


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消