1

I'm trying to connect to mysql db using jdbc,but an exception occurred.Can anyone help me? thx! driver version is 6.0.6 and mysql db version is 5.7.18.

it seems the error is caused by ArrayIndexOutOfBoundsException,but I really can't figure it out!

code:

    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();
        }
    }
}

stack

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
alex.he
  • 13
  • 1
  • 4
  • Check list: your connection string: IP of server, username, password, port, JDBC driver. – Vy Do Jul 03 '17 at 08:57
  • Possible duplicate of [jdbc : Could not create connection to database server](https://stackoverflow.com/questions/41637604/jdbc-could-not-create-connection-to-database-server) – aurelius Jul 03 '17 at 08:58
  • 1
    The MySQL Connector/J 6.x is still in development and thus unstable, maybe you should use 5.1.42 instead. – Mark Rotteveel Jul 03 '17 at 13:10

3 Answers3

1

As Mark said you should downgrade to 5.1.42 driver first. It helped me to at least to see what real error is. In my case it was connection to root account from another host. In you case it might be something else. 6.0.6 really still under development.

ice
  • 44
  • 3
1

Use com.mysql.cj.jdbc.Driver (deprecated), instead of com.mysql.jdbc.Driver And make sure you are using latest version of mysql driver

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.15</version>
</dependency>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/testDB" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>
Hari Bisht
  • 197
  • 1
  • 2
  • 8
-1
Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");

so u need to check for the port no you have provided in mysql,and also the table name.