【问题标题】:How can i connect Mysql by using Java?如何使用 Java 连接 Mysql?
【发布时间】:2010-07-15 00:59:15
【问题描述】:

我正在尝试使用“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 */ }
               }
           }
       }
   }

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

网址有问题吗,因为我不确定我会放什么,所以我放了我所在的免费主机的IP地址。

知道如何连接 Mysql 吗?提前致谢!

【问题讨论】:

  • 您需要查看异常的详细信息。尝试将 catch 块更改为 e.printStackTrace();并将其添加到您的问题中。
  • 添加到之前的评论:将这一行 - System.err.println ("Cannot connect to database server") - 改为 e.printStackTrace()
  • Connecting to a MySQL database. 的可能重复项

标签: java mysql connection


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

         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 中的运行时类路径中。也许你应该调查一下。

      我建议先在本地实例上尝试您的代码,以确保它可以正常工作。如果是这样,并且连接到托管数据库失败,您就会知道问题完全是由托管问题引起的。

      【讨论】:

        猜你喜欢
        • 2011-02-25
        • 2015-09-05
        • 1970-01-01
        • 1970-01-01
        • 2017-03-04
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多