JDBC无法连接到数据库

时间:2018-06-03 20:09:54

标签: java mysql jdbc

我写了一些代码在本地数据库中插入一个表,并且每个堆栈跟踪收到以下错误:

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:108)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:932)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:857)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at CreateTable.main(CreateTable.java:24)
Caused by: com.mysql.cj.exceptions.WrongArgumentException: No timezone mapping entry for 'PDT'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2215)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
    at com.mysql.cj.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:895)
    ... 7 more

显然,我可能错了,但它与我的时区或MySQL或JDBC连接器中的时区设置方式有关。

这是我的原始源代码:

import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTable {
    public static void main(String[] args) throws SQLException {
        Connection myConnection = null;
        Statement myStmt = null;
        //ResultSet rs = null;
        String sql = "create table student(student_id varchar(20) primary key, name varchar(30), class varchar(15), marks varchar(10);";

        try {
        /*
        Get Connection to jdbc
        */
        myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?autoReconnect=true&useSSL=false", "userOne", "password");

        /*Create an object for passing SQL queries to DB */
        myStmt = myConnection.createStatement();

        /*Execute a resulting statement to create a table in the DB */
        myStmt.executeQuery(sql);

        System.out.println("DB Table created in Database Edureka!");

        } catch(Exception e) {
            e.printStackTrace();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

尝试通过添加时区信息修改连接网址:

myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?autoReconnect=true&useSSL=false&serverTimezone=PDT", "userOne", "password");