当前位置: 首页 > 知识库问答 >
问题:

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

郜谦
2023-03-14

我编写了一些代码来在本地数据库中插入一个表,但我在堆栈跟踪中得到以下错误:

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个答案

姜博
2023-03-14

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

myConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/edureka?autoReconnect=true&useSSL=false&serverTimezone=PDT", "userOne", "password");
 类似资料:
  • sry如果这个问题是noob(我是新来的)。。。 我一直试图设置我的应用程序连接到数据库,但我得到了这个错误,我似乎找不到问题。 这是密码 当我试着运行nodemon并打开http://localhost:3000/

  • 我使用 JTDS 版本: jtds-1.3.1 MS SQL JDBC version: mssql-jdbc-8.4.0.jre8 even mssql-jdbc-9.2.0.jre8 Java 8 更新 251 为简单起见,代码将相同: Android Studio 無法連接到 Azure sql server 中的資料庫 连接到SQL服务器或AzureSQL时遇到问题。 我想用这个语法连接:

  • 我正在尝试使用jdbc连接到我的MS SQL2008数据库,如下所示,但它的给出错误 我的数据库名为,实例名为。请建议我如何提供数据库实例名称和数据库名称在URL。 上面写着 但是当我尝试用相同的用户名和密码从DB GUI登录时,它被登录了。想知道在jdbc设置中在哪里提供数据库实例的详细信息 我也试过用下面的dut不管用

  • 我创建了一个简单的类来测试与我的localhost数据库的通信,这是我用Mysql Workbench创建的。Mysql服务器正在运行。JDBC驱动程序被添加到我的项目的类路径中。 当我运行程序时,我得到以下异常: 线程“main”com.mysql.cj.jdbc.Exceptions.CommunicationsException异常:通信链接失败 最后一个成功发送到服务器的数据包是在0毫秒前

  • //我试图创建一个注册表单(html),但在我单击按钮后,php文件似乎不能正确执行,web浏览器显示了上面的php代码,我想知道php有什么问题

  • 主要内容:1. 导入JDBC包,2. 注册JDBC驱动程序,数据库URL配置,创建连接对象,使用具有用户名和密码的数据库URL,关闭JDBC连接安装相应的驱动程序后,现在是时候来学习使用JDBC建立数据库连接了。 建立JDBC连接所涉及的编程相当简单。 以下是基本的四个步骤 - 导入JDBC包:使用Java语言的语句在Java代码开头位置导入所需的类。 注册JDBC驱动程序:使JVM将所需的驱动程序实现加载到内存中,从而可以满足JDBC请求。 数据库URL配置:创建一个正确格式化的地址,指向要连