2017-02-23 68 views
-1

我下载并安装了mysql-5.7.17-macos10.12-x86_64,工作台和mysql-connector-java-5.1.40-bin.jar。 我使用下面的代码来联合mysql数据库,但它会引发com.mysql.jdbc.exceptions.jdbc4.CommunicationsException。jdbc连接失败,但MysqlWorkBench可以连接成功

Connecting to database: jdbc:mysql://127.0.0.1:3306/demo?user=root&password=root 
Communications link failure 

成功发送到服务器的最后一个数据包是0毫秒前。驱动程序尚未收到来自服务器的任何数据包。 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障

我可以使用的用户名和密码进行连接在mysqlworkbench数据库和MySQL服务器的状态是物质running.Does它,我有其他MySQL版本?使用的Mysql版本是5.7.17。

 try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      String conn = "jdbc:mysql://" + d_server_name + "/" + 
        d_db_name+"?user="+d_user_name+"&password="+d_password; 
      System.out.println("Connecting to database: " + conn); 
      d_connect = DriverManager.getConnection(conn); 
      System.out.println("Connected to database"); 
      return d_connect; 
     } catch(Exception e) { 
      throw e; 
     }  

回答

-1

请尝试使用以下格式。 connection = DriverManager.getConnection(“jdbc:mysql://127.0.0.1:3306/database”,“root”,“root”);

+0

我试过这个,但它似乎不工作。 – PuppeyD