代码之家  ›  专栏  ›  技术社区  ›  Al-geBra

JDBC无法连接到数据库[重复]

  •  1
  • Al-geBra  · 技术社区  · 6 年前

    这个问题已经有了答案:

    我编写了一些代码来在本地数据库中插入一个表,每次堆栈跟踪都会出现以下错误:

    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 回复  |  直到 6 年前
        1
  •  0
  •   NiVeR    6 年前

    尝试通过添加时区信息来修改连接URL:

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