在最近的实验中,我遇到了这样一个报错,鉴于没能在csdn上搜索到解决办法,将自己的解决方法放在这里。
错误状态形如下图:
解决的方法是在persistence.xml配置文件中的url的value中加入数据库名后的一段代码。
配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="firstJPA2.1" transaction-type="RESOURCE_LOCAL">
<class>entity.User</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3406/testDB?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="123"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
所需要添加的代码是:
?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
注意!!!这段代码一定要加在原来的jdbc.url的value中数据库名称的后面,还有错误可能是因为数据库未启动或者数据库的连接密码不正确,记得检查好persistence.xml配置文件中的信息。还有可能就是相应的jar包没有导入,注意前往项目的Build Path中配置。