如何使用Java连接Mysql?

发布于 2024-09-09 17:55:13 字数 1352 浏览 0 评论 0原文

我正在尝试使用“mysql-connector-java-5.1.13”连接Mysql。我已经下载了它并将其放入我正在使用 Netbeans 的项目的库中。我找到了一个免费的主机并创建了Mysql数据库。现在我正在尝试以下代码;

   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 */ }
               }
           }
       }
   }

但捕获了异常,并显示:无法连接到数据库服务器。

url 是否有问题,因为我不确定要放在那里的内容,所以我放了我所在的免费主机的 IP 地址。

知道如何连接 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花肆意 2024-09-16 17:55:13

检查您的虚拟主机是否允许您进行远程连接。如果您有权访问 ssh,请检查您的 my.cnf 配置是否允许您远程连接。

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.

述情 2024-09-16 17:55:13

我会尝试使用不同的工具(可能是 MySQL 命令行工具)来验证用户名/密码和主机。

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

回忆躺在深渊里 2024-09-16 17:55:13

打印消息而不打印堆栈跟踪是愚蠢的。像这样写:

   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 */ }
               }
           }
       }
   }

我猜测驱动程序类没有找到;您收到 ClassNotFoundException。

将 MySQL 连接器 JAR 放入 /lib 是不够的;您必须将其添加到 Netbeans 中的运行时类路径中。也许你应该调查一下。

我建议首先在本地实例上尝试您的代码以确保其有效。如果确实如此,并且连接到托管数据库失败,您就会知道问题完全是由于托管问题造成的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文