The connection property 'useUnicode' only accepts values of the form: 'true', 'false', 'yes' or 'no

本文介绍了在Eclipse中使用Spring+hibernate访问MySQL数据库时出现的错误及解决方案。涉及eclipse、spring、hibernate、MySQL及其驱动的具体版本,详细阐述了三个错误,包括SSL连接、实体引用和连接属性值问题,并给出了对应的jdbcUrl修改方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 The connection property 'useUnicode' only accepts values of the form: 'true', 'false', 'yes' or 'no'. The value 'true;characterEncoding=utf-8' is not in this set.

 

https://blog.youkuaiyun.com/sddxqlrjxr/article/details/51868776

 

 

以下错误均出现在eclipse使用spring+hibernate访问MySQL数据库的时候。 
eclipse版本:Version: Neon Release (4.6.0) 
spring版本:4.3.0 
hibernate版本:5.2.0 
MySQL版本:5.7.13 
MySQL驱动版本:5.1.39 
spring配置文件名:applicationContext.xml。

<!--定义数据源Bean,使用C3P0数据源实现,并注入数据源必要信息-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="com.mysql.jdbc.Driver"
    p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"
    p:user="root"
    p:password="1234"
    p:maxPoolSize="40"
    p:minPoolSize="2"
    p:initialPoolSize="2"
    p:maxIdleTime="30" />
1
2
3
4
5
6
7
8
9
10
11
错误一:
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
1
解决方案
p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin"
1
将上面这句换成下面的

p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin?useUnicode=true&characterEncoding=utf-8&useSSL=false"
1
此时错误一将不出意外的消失,但是此时错误二应运而生。

错误二
对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾。
1
解决方案
p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin"
1
将这句再改成下面这样的

p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin?useUnicode=true;characterEncoding=utf-8;useSSL=false"
1
与错误一的解决方案比,就是讲‘&’符号改成了‘;’。 
错误二销声匿迹,错误三粉墨登场。

错误三
The connection property 'useUnicode' only accepts values of the form: 'true', 'false', 'yes' or 'no'....
1
解决方案
p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin"
1
把这句话再改一下

p:jdbcUrl="jdbc:mysql://localhost:3306/ShuziYingxin?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false"
1
就是既不能用‘&’也不能用‘;’而是要使用‘&amp;’。虽然&amp; 本身表示‘&’符号,但是必须这么写才可以,否则解析xml文件时就无法解析。

<think>嗯,用户遇到了Java中MySQL连接字符串配置导致的SQLException,具体提到allowPublicKeyRetrieval的问题。我需要先理解这个错误的背景和常见原因。 首先,用户可能在连接MySQL时使用了较新的JDBC驱动版本,比如8.x以上,而MySQL服务器版本可能较旧,或者在SSL配置上有问题。错误信息通常会说“Public Key Retrieval is not allowed”,这时候需要允许客户端获取公钥。这时候解决方案通常是在连接字符串中添加allowPublicKeyRetrieval=true参数。 接下来,我需要确认这个参数的作用和使用场景。这个参数允许客户端从服务器获取公钥,用于身份验证,尤其是在SSL未启用的情况下。但用户需要注意安全风险,因为可能中间人攻击,所以应该只在测试环境使用,生产环境应正确配置SSL。 然后,检查用户可能的连接字符串配置,例如URL格式是否正确,时区设置是否正确,以及是否启用了SSL。常见的连接字符串参数包括useSSL、serverTimezone等,这些都是可能引发问题的点。 另外,用户可能没有正确加载驱动类,或者驱动版本不兼容。例如,从MySQL 5.x升级到8.x,驱动类名从com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver,这也是需要检查的地方。 还需要考虑其他可能的错误,比如用户名或密码错误,网络问题,或者数据库服务未启动。但根据用户提供的错误信息,重点应放在连接参数的配置上,尤其是allowPublicKeyRetrieval和SSL设置。 最后,应该建议用户分步骤排查问题,从添加参数开始,逐步检查其他配置项,并提醒安全注意事项。同时,提供示例代码和配置,帮助用户快速应用解决方案。</think>针对Java中MySQL连接字符串配置导致的`SQLException: Public Key Retrieval is not allowed`错误,以下是系统性解决方案: ### 1. 错误原因分析 此错误常见于MySQL 8.x版本与JDBC驱动交互时,当客户端无法通过SSL安全获取公钥时触发。主要原因包括: - 未启用SSL连接但需要公钥验证身份[^2] - 客户端驱动版本与服务端不兼容 - 用户权限或密码加密方式不匹配 ### 2. 解决方案 #### (1) 基础配置 在JDBC连接字符串中添加参数: ```java jdbc:mysql://localhost:3306/dbname?allowPublicKeyRetrieval=true&useSSL=false ``` - `allowPublicKeyRetrieval=true`:允许公钥检索(注意安全风险) - `useSSL=false`:禁用SSL(仅限测试环境) #### (2) 安全配置方案 生产环境推荐: ```java jdbc:mysql://localhost:3306/dbname?useSSL=true&requireSSL=true&verifyServerCertificate=true ``` 需配合Java keystore配置SSL证书: ```bash -Djavax.net.ssl.keyStore=/path/keystore -Djavax.net.ssl.keyStorePassword=yourpassword ``` ### 3. 完整配置示例 ```java Class.forName("com.mysql.cj.jdbc.Driver"); Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mydb?" + "user=root&password=123456" + "&allowPublicKeyRetrieval=true" + "&useSSL=false" + "&serverTimezone=UTC"); ``` ### 4. 其他注意事项 | 参数 | 推荐值 | 说明 | |------|--------|------| | serverTimezone | UTC | 避免时区不一致问题 | | characterEncoding | UTF-8 | 字符集统一 | | autoReconnect | true | 自动重连 | ### 5. 驱动版本对照表 | MySQL版本 | 推荐驱动 | |-----------|----------| | 5.7 | mysql-connector-java-5.1.47 | | 8.0+ | mysql-connector-java-8.0.28 | 若使用连接池(如DBCP),需同步更新pom.xml依赖: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> ```
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值