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

Bean和SQL异常[重复]

国胤
2023-03-14

我正在使用Spring Boot构建一个简单的CRUD操作,MySQL和Hibernate在这个操作中需要帮助。在Google上搜索了很多,StackOverflow没有找到合适的解决方案。

问题:服务器时区值“unknown”无法识别或表示多个时区。如果要利用时区支持,则必须配置服务器或JDBC驱动程序(通过ServerTimeZone配置属性)以使用更具体的时区值。

com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value 'unknown' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_91]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_91]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_91]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_91]
Unable to open JDBC Connection for DDL execution
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:69) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.jdbcStatement(GenerationTargetToDatabase.java:77) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:53) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlString(SchemaDropperImpl.java:375) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.applySqlStrings(SchemaDropperImpl.java:359) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.dropFromMetadata(SchemaDropperImpl.java:241) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.performDrop(SchemaDropperImpl.java:154) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:126) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.internal.SchemaDropperImpl.doDrop(SchemaDropperImpl.java:112) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:145) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:73) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:316) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1259) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]

[PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution


Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution


 Error creating bean with name 'digitalEmployeeRepo' defined in com.eg.demo.repository.DigitalEmployeeRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.GenericJDBCException: Unable to open JDBC Connection for DDL execution

....................................................我的密码。.............控制器...........................

package com.eg.demo.controller;

import com.eg.demo.model.DigitalEmployee;
import com.eg.demo.service.DigitalEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

public class DigitalController {
    @Autowired
    DigitalEmployeeService digitalEmployeeService;


    @RequestMapping(value = "/DigitalEmployee/", method = RequestMethod.GET)
    public ResponseEntity<List<DigitalEmployee>> listAllUsers() {
        List<DigitalEmployee> digitalEmployees = digitalEmployeeService.allEmployee();
        if (digitalEmployees.isEmpty()) {
            return new ResponseEntity(HttpStatus.NO_CONTENT);

        }
        return new ResponseEntity<List<DigitalEmployee>>(digitalEmployees, HttpStatus.OK);
    }

    // -------------------Retrieve Single User------------------------------------------

    @RequestMapping(value = "/digitalEmployee/{id}", method = RequestMethod.GET)
    public ResponseEntity<?> getEmployee(@PathVariable("id") long id) {

        DigitalEmployee digitalEmployee = digitalEmployeeService.findById(id);

        return new ResponseEntity<DigitalEmployee>(digitalEmployee, HttpStatus.OK);
    }

    // -------------------Create a User-------------------------------------------

    @RequestMapping(value = "/digitalEmployee/", method = RequestMethod.POST)
    public ResponseEntity<?> createEmployee(@RequestBody DigitalEmployee digitalEmployee) {
        digitalEmployeeService.save(digitalEmployee);
        return new ResponseEntity<DigitalEmployee>(HttpStatus.CREATED);
    }

    // ------------------- Update a User ------------------------------------------------

    @RequestMapping(value = "/digitalEmployee/{id}", method = RequestMethod.PUT)
    public ResponseEntity<?> updateEmployee(@PathVariable("id") long id, @RequestBody DigitalEmployee digitalEmployee) {
        DigitalEmployee currentEmployee = digitalEmployeeService.findById(id);

        currentEmployee.setName(digitalEmployee.getName());
        digitalEmployeeService.update(currentEmployee);
        return new ResponseEntity<DigitalEmployee>(currentEmployee, HttpStatus.OK);
    }

    // ------------------- Delete a User-----------------------------------------

    @RequestMapping(value = "/digitalEmployee/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteEmployee(@PathVariable("id") long id) {
        DigitalEmployee digitalEmployee = digitalEmployeeService.findById(id);
        digitalEmployeeService.deleteById(id);
        return new ResponseEntity<DigitalEmployee>(HttpStatus.NO_CONTENT);
    }

    // ------------------- Delete All Users-----------------------------

    @RequestMapping(value = "/DigitalEmployee/", method = RequestMethod.DELETE)
    public ResponseEntity<DigitalEmployee> deleteAll() {

        digitalEmployeeService.deleteAll();
        return new ResponseEntity<DigitalEmployee>(HttpStatus.NO_CONTENT);
    }

}

模型...........................

package com.eg.demo.model;

import javax.persistence.*;

@Entity
@Table(name = "DigitalEmployee")
public class DigitalEmployee {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private String name;

    public DigitalEmployee() {
    }

    public DigitalEmployee(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

...........回购.............

package com.eg.demo.repository;

import com.eg.demo.model.DigitalEmployee;
import org.springframework.data.repository.CrudRepository;

public interface DigitalEmployeeRepo extends CrudRepository<DigitalEmployee, Long> {
}
package com.eg.demo.service;

import com.eg.demo.model.DigitalEmployee;

import java.util.List;
import java.util.Optional;

public interface DigitalEmployeeService {
    void save(DigitalEmployee digitalEmployee);
    void update(DigitalEmployee digitalEmployee);
    void deleteAll();
    void deleteById(Long id);
    DigitalEmployee findById(Long id);
    List<DigitalEmployee> allEmployee();

}

.............服务Impl..............................

package com.eg.demo.service;

import com.eg.demo.model.DigitalEmployee;
import com.eg.demo.repository.DigitalEmployeeRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DigitalEmployeeServiceImpl implements DigitalEmployeeService {

    @Autowired
    DigitalEmployeeRepo digitalEmployeeRepo;

    public void save(DigitalEmployee digitalEmployee) {
        digitalEmployeeRepo.save(digitalEmployee);
    }

    public void update(DigitalEmployee digitalEmployee) {
        save(digitalEmployee);
    }

    public void deleteAll() {
         digitalEmployeeRepo.deleteAll();
    }

    public void deleteById(Long id) {
        digitalEmployeeRepo.deleteById(id);
    }

    public DigitalEmployee findById(Long id) {
        return digitalEmployeeRepo.findById(id).get();
    }

    public List<DigitalEmployee> allEmployee() {
        return (List<DigitalEmployee>) digitalEmployeeRepo.findAll();
    }
}

............主法...........

package com.eg.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


//@Configuration
@SpringBootApplication(scanBasePackages = "com.*")
@EnableSwagger2
//@EnableAutoConfiguration
//@ComponentScan("com.eg.demo")

public class DemoApplication {

    public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
    }
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

................应用程序属性............................

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.datasource.url=jdbc:mysql://localhost:3306/digital
spring.datasource.username=root
spring.datasource.password=
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.format_sql=true
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.eg</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>

        <!--<dependency>-->
            <!--<groupId>io.springfox</groupId>-->
            <!--<artifactId>springfox-swagger2</artifactId>-->
            <!--<version>2.9.2</version>-->
        <!--</dependency>-->



        <!--<dependency>-->
            <!--<groupId>io.springfox</groupId>-->
            <!--<artifactId>springfox-swagger2</artifactId>-->
            <!--<version>2.9.2</version>-->
        <!--</dependency>-->

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-oas</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>io.springfox</groupId>-->
            <!--<artifactId>springfox-swagger-ui</artifactId>-->
            <!--<version>2.9.2</version>-->
        <!--</dependency>-->
        <!---->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

共有1个答案

丌官炎彬
2023-03-14

使用datasource url属性配置时区,就像这个url:jdbc:mysql:/127.0.0.1:3306/test?&ServerTimeZone=Asia/Shanghai

 类似资料:
  • 问题内容: 我是.net的新手,因此我正在Windows窗体应用程序上工作。我试图将我的应用程序连接到基于Visual Studio Service的数据库。我只是在“提交”按钮后面编写代码。 在con.open();上 我遇到了一个异常,显示 “建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。找不到服务器或无法访问该服务器。请验证实例名称正确并且将SQL Server配置为

  • 问题内容: 我在有两个INSERT指令的存储过程中使用TRY CATCH块。 如果出现问题,则CATCH块负责回滚所做的所有更改,并且除一件事外,它都可以正常工作! 我的ASP.NET应用程序捕获的异常是一个SqlException,其编号为50000。这不是原始编号!(我期望的数字是2627) 在异常的Message属性中,我可以看到原始的异常编号和格式化的消息。 我如何获得原始的异常编号? 现

  • 我正在php中创建一个非常简单的注册表单,当前当用户尝试注册时,将弹出一个带有succes或fail消息的javascript警报。 现在,我想捕获sql异常,以显示用户名或电子邮件是否已经在数据库中执行,而不是标准的失败消息。 这是我目前拥有的代码: 我如何检查是否电子邮件或用户名已经在数据库中?这两个变量在数据库中都是唯一的。

  • 你怎么捕获一个异常,之后在另外一个线程上重新抛出?使用在标准文档18.8.5中描述的异常传递中的方法吧,那将显示标准库的魔力。 exception_ptr current_exception(); 返回一个exception_ptr 变量,它将指向现在正在处理的异常(15.3)或者现在正在处理的异常的副本(拷贝),或者有的时候在当前没有遇到异常的时候,返回值为一个空的exception_ptr变量

  • 以下错误在连接 sqlserver(java) 时抛出。我不知道这个问题的根本原因。 如果有人遇到这个问题,请让我们知道,如何解决这个问题?

  • 我正在使用JavaDSL来配置路由。我有一个类似于下面给出的路由。 在做了一些活动后,我该如何处理异常?在异常时,我尝试配置处理器和bean来重新引发异常。不管怎样,camel都在将异常设置为exchange,但没有破坏异常。 我在junit测试用例中这样做。我正在使用OneException处理器处理异常。在处理器内部,我正在进行断言。断言错误由camel自动处理,测试不会被标记为通过/失败。