2017-07-03 211 views
1

我想使用jdbc连接到mysql数据库,但发生异常。任何人都可以帮我吗?谢谢!驱动程序版本是6.0.6,而mysql db版本是5.7.18。JDBC无法连接到mysql

它似乎错误是由ArrayIndexOutOfBoundsException引起的,但我真的不知道它!

代码:

package test; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 

public class Test { 

    @org.junit.jupiter.api.Test 
    public void test(){ 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin"); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:526) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:505) 
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:479) 
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1779) 
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:1596) 
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:633) 
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:347) 
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:219) 
    at java.sql.DriverManager.getConnection(DriverManager.java:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:247) 
    at test.Test.test(Test.java:13) 

Caused by: java.lang.ArrayIndexOutOfBoundsException: 39 
    at com.mysql.cj.mysqla.io.Buffer.readInteger(Buffer.java:271) 
    at com.mysql.cj.mysqla.io.MysqlaCapabilities.setInitialHandshakePacket(MysqlaCapabilities.java:62) 
    at com.mysql.cj.mysqla.io.MysqlaProtocol.readServerCapabilities(MysqlaProtocol.java:482) 
    at com.mysql.cj.mysqla.io.MysqlaProtocol.beforeHandshake(MysqlaProtocol.java:367) 
    at com.mysql.cj.mysqla.io.MysqlaProtocol.connect(MysqlaProtocol.java:1412) 
    at com.mysql.cj.mysqla.MysqlaSession.connect(MysqlaSession.java:132) 
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1726) 
    ... 36 more 
+0

检查列表:您的连接字符串:服务器,用户名,密码,端口,JDBC驱动程序的IP。 –

+0

[jdbc:无法创建与数据库服务器的连接]的可能重复(https://stackoverflow.com/questions/41637604/jdbc-could-not-create-connection-to-database-server) – aurelius

+1

MySQL Connector/J 6.x仍在开发中,因此不稳定,也许你应该使用5.1.42。 –

回答

-1
Class.forName("com.mysql.jdbc.Driver"); 
      Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin"); 

,这样你们需要检查没有你在mysql中已经提供,也表名的端口。

0

正如马克说你应该先降级到5.1.42驱动程序。它至少帮助我了解真正的错误。在我的情况下,它是从另一台主机连接到根帐户。在你的情况下,它可能是别的东西。 6.0.6真的还在开发中。