Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java连接到外部mysql数据库_Java_Mysql - Fatal编程技术网

使用Java连接到外部mysql数据库

使用Java连接到外部mysql数据库,java,mysql,Java,Mysql,我正在尝试连接到外部数据库,但在连接DriverManager的过程中似乎出现了错误,这是我第一次使用此驱动程序进行连接,您能为我指出正确的方向吗?谢谢(getConnection电话可能有问题) 错误 SQL Exception: State : 08S01 Message: Communications link failure Last packet sent to the server was 0 ms ago. Error : 0 不要只传递一个字符串参数,而是尝试像

我正在尝试连接到外部数据库,但在连接DriverManager的过程中似乎出现了错误,这是我第一次使用此驱动程序进行连接,您能为我指出正确的方向吗?谢谢(getConnection电话可能有问题)

错误

SQL Exception:
State  : 08S01
Message: Communications link failure

Last packet sent to the server was 0 ms ago.
Error  : 0

不要只传递一个字符串参数,而是尝试像

    getConnection(String url, String user, String password)
以下是DriverManager类的指南

您可以使用单独的用户名、密码和驱动程序,而不是在一个实例中传递参数

Connection conn = null;

           try
           {
               String userName = "testuser";
               String password = "testpass";
               String url = "jdbc:mysql://localhost/test";
               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 */ }
               }
           }

您看到了什么错误或异常?我发布了异常,但您能否判断我的代码是否正确(连接字符串)?检查此链接,它将帮助您创建正确的连接字符串。
Connection conn = null;

           try
           {
               String userName = "testuser";
               String password = "testpass";
               String url = "jdbc:mysql://localhost/test";
               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 */ }
               }
           }