Reputation: 13
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
Upvotes: 1
Views: 9925
Reputation: 227
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>
Upvotes: 1
Reputation: 44
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.
Upvotes: 1
Reputation: 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.
Upvotes: -1