简体   繁体   English

JDBC无法连接到mysql

[英]JDBC Cannot connect to mysql

I'm trying to connect to mysql db using jdbc,but an exception occurred.Can anyone help me? 我正在尝试使用jdbc连接到mysql db,但是发生了异常。有人可以帮我吗? thx! 谢谢! driver version is 6.0.6 and mysql db version is 5.7.18. 驱动程序版本为6.0.6,而mysql数据库版本为5.7.18。

it seems the error is caused by ArrayIndexOutOfBoundsException,but I really can't figure it out! 看来该错误是由ArrayIndexOutOfBoundsException引起的,但我真的无法弄清楚!

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

As Mark said you should downgrade to 5.1.42 driver first. 正如Mark所说,您应该先降级到5.1.42驱动程序。 It helped me to at least to see what real error is. 它至少帮助我了解了什么是真正的错误。 In my case it was connection to root account from another host. 以我为例,它是从另一台主机连接到root帐户。 In you case it might be something else. 在您的情况下,可能还有其他事情。 6.0.6 really still under development. 6.0.6确实还在开发中。

Use com.mysql.cj.jdbc.Driver (deprecated), instead of com.mysql.jdbc.Driver And make sure you are using latest version of mysql driver 使用com.mysql.cj.jdbc.Driver(不建议使用)而不是com.mysql.jdbc.Driver并确保您使用的是最新版本的mysql驱动程序

<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>
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. 因此,您需要检查您在mysql中提供的端口号以及表名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM