我的代码正在尝试连接到MySQL,但我不断收到以下错误

时间:2019-07-19 22:01:55

标签: java mysql

我收到以下运行时错误:

SQL Error: java.sql.SQLNonTransientConnectionException: Could not create connection to database server. 0 08001
java.sql.SQLNonTransientConnectionException: Could not create connection to database server.
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:1008)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
        at Hour/com.java24hours.SysTableReporter.main(SysTableReporter.java:9)
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
        at mysql.connector.java@8.0.16/com.mysql.cj.protocol.a.NativePacketPayload.readInteger(NativePacketPayload.java:369)
        at mysql.connector.java@8.0.16/com.mysql.cj.protocol.a.NativeCapabilities.setInitialHandshakePacket(NativeCapabilities.java:68)
        at mysql.connector.java@8.0.16/com.mysql.cj.protocol.a.NativeProtocol.readServerCapabilities(NativeProtocol.java:522)
        at mysql.connector.java@8.0.16/com.mysql.cj.protocol.a.NativeProtocol.beforeHandshake(NativeProtocol.java:404)
        at mysql.connector.java@8.0.16/com.mysql.cj.protocol.a.NativeProtocol.connect(NativeProtocol.java:1450)
        at mysql.connector.java@8.0.16/com.mysql.cj.NativeSession.connect(NativeSession.java:165)
        at mysql.connector.java@8.0.16/com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
        ... 7 more

Skip网络已关闭,jdbc和mysql服务器具有相同的8.0.16版本,并且防火墙没有阻止mysql端口。但是,我仍然不断遇到这个问题。

编辑:对不起,这里的代码忘记了(顺便说一句,我以root用户身份使用abcd密码abcd连接)

package com.java24hours;

import java.sql.*;

public class SysTableReporter {
    public static void main(String[] arguments) {
       String data = "jdbc:mysql://127.0.0.1:33060/example";
       try (
           Connection conn = DriverManager.getConnection(
               data, "root", "abcd");
           Statement st = conn.createStatement()) {

           Class.forName("com.mysql.cj.jdbc.Driver");

            ResultSet rec = st.executeQuery(
                "SELECT * " +
                "FROM books " +
                "ORDER BY id");
            while(rec.next()) {
                System.out.println("id:\t" + rec.getString(1));
                System.out.println("title:\t" + rec.getString(2));
                System.out.println("author:\t" + rec.getString(3));
                System.out.println("submission date:\t" + rec.getString(4));
                System.out.println();
            }
            st.close();
        } catch (SQLException s) {
            System.out.println("SQL Error: " + s.toString() + " "
                + s.getErrorCode() + " " + s.getSQLState());
s.printStackTrace(System.out);
        } catch (Exception e) {
            System.out.println("Error: " + e.toString()
                + e.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

似乎我犯了一个愚蠢的错误,我使用的端口是33060而不是3306。谢谢大家在评论中的帮助。