applicationContext.xml连接JDBC数据库时碰到的错误及解决方法

使用applicationContext.xml连接JDBC数据库的时候碰到一些问题,记录一下。

下面是我最开始对着书敲的代码,需要改正特别注意的地方用红框圈出来了:

MySQL版本8.0

jar版本原来是5.0,需要和MySql匹配,要换成8.0的改正后换成和MySQL一致的版本→8.0版本jar包

applicationContext.xml

放一张最终运行成功的applicationContext.xml代码图片及改正后的值:

value="com.mysql.cj.jdbc.Driver"

value="jdbc:mysql://localhost/spring?useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true"
成功运行的applicationContext.xml

JdbcTemplateTest.java

下面是运行时出现的具体错误及改正方法:

1.首先运行出现如下错误:

Sun Jul 05 21:55:52 CST 2020 WARN: 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.

错误原因:MySQL在高版本需要指明是否进行SSL(保障Internet数据传输安全利用数据加密)

参考博客:https://blog.csdn.net/xhangs/article/details/88635358

解决方法:把<property name="url" value="jdbc:mysql://localhost/spring"/>中的value加上?useSSL=false

                  如:<property name="url" value="jdbc:mysql://localhost/spring?useSSL=false"/>

2.第二次运行出现如下错误:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
......

//重点是这里,我的MySql版本是8.0的,但是导入jar包的时候是5.1版本的
//并且数据驱动写的是value="com.mysql.jdbc.Driver",8.0版本的应该是value="com.mysql.cj.jdbc.Driver"
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

错误提示:Could not create connection to database server.

错误原因:我的MySql版本是8.0的,但是导入jar包的时候是5.1版本的,并且数据驱动写的是value="com.mysql.jdbc.Driver",8.0版本的应该是value="com.mysql.cj.jdbc.Driver"。

解决方法:jar包换成8.0版本的。并且value改成value="com.mysql.cj.jdbc.Driver"

8.0版本jar包

3.第三次运行出现错误:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
......
Caused by: java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed

发生错误:Public Key Retrieval is not allowed。

错误原因:如果用户使用了 sha256_password 认证,密码在传输过程中必须使用 TLS 协议保护,但是如果 RSA 公钥不可用,可以使用服务器提供的公钥;可以在连接中通过 ServerRSAPublicKeyFile 指定服务器的 RSA 公钥,或者AllowPublicKeyRetrieval=True参数以允许客户端从服务器获取公钥;但是需要注意的是 AllowPublicKeyRetrieval=True可能会导致恶意的代理通过中间人攻击(MITM)获取到明文密码,所以默认是关闭的,必须显式开启。

参考博客:https://blog.csdn.net/u013360850/article/details/80373604。

解决方法:在value="jdbc:mysql://localhost/spring?useSSL=false"后添加allowPublicKeyRetrieval=true

              如:value="jdbc:mysql://localhost/spring?useSSL=false&amp;allowPublicKeyRetrieval=true"

注意:这里有些人是用&连接的,如:useSSL=false&allowPublicKeyRetrieval=true",但是我在这里用&会报错,应该改成&amp;

报错提示:对实体 "allowPublicKeyRetrieval" 的引用必须以 ';' 分隔符结尾。

Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 103; 对实体 "allowPublicKeyRetrieval" 的引用必须以 ';' 分隔符结尾。
......
Caused by: org.xml.sax.SAXParseException; lineNumber: 13; columnNumber: 103; 对实体 "allowPublicKeyRetrieval" 的引用必须以 ';' 分隔符结尾。

4.第四次运行出现错误:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Access denied for user '022278'@'localhost' (using password: YES)
......
Caused by: java.sql.SQLException: Access denied for user '022278'@'localhost' (using password: YES)

错误原因:我的username是022278,要改成root。(自己创建MySql数据库的时候用户名是022278,我以为是要用这个)

解决方法:<property name="username" value="022278"/>改成<property name="username" value="root"/>

5.第五次运行错误:

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '???ú±ê×??±??' 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.
......
Caused by: java.sql.SQLException: The server time zone value '???ú±ê×??±??' 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.
......
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '???ú±ê×??±??' 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.

解决方法:在value="jdbc:mysql://localhost/spring?useSSL=false&amp;&amp;allowPublicKeyRetrieval=true"后加上&amp;serverTimezone=UTC

如:value="jdbc:mysql://localhost/spring?useSSL=false&amp;serverTimezone=UTC&amp;&amp;allowPublicKeyRetrieval=true"

6.第六次。。。终于运行成功了!!!(欢呼!!!)

运行成功

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Spring Boot + Atomikos环境下,我们可以使用Jasypt库来实现数据库信息的加密和解密。以下是一个示例: 1. 首先,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency> ``` 2. 在application.properties或application.yml文件中,我们需要设置以下属性: ```properties jasypt.encryptor.password=yourSecretPassword ``` 这里的“yourSecretPassword”是一个自定义的密码,用于加密和解密数据。 3. 在applicationContext.xml文件中,我们可以使用Jasypt提供的加密和解密Bean来加密和解密数据库信息。以下是一个示例: ```xml <bean id="propertyConfigurer" class="com.ulisesbocchio.jasyptspringboot.properties.JasyptEncryptablePropertyPlaceholderConfigurer"> <constructor-arg ref="encryptor" /> <property name="locations"> <list> <value>classpath:application.properties</value> </list> </property> </bean> <bean id="encryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> <property name="password"> <value>${jasypt.encryptor.password}</value> </property> </bean> <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close"> <property name="uniqueResourceName"> <value>dataSource</value> </property> <property name="xaDataSourceClassName"> <value>com.mysql.cj.jdbc.MysqlXADataSource</value> </property> <property name="xaProperties"> <props> <prop key="URL">${encryptor.decrypt('encrypted.database.url')}</prop> <prop key="user">${encryptor.decrypt('encrypted.database.username')}</prop> <prop key="password">${encryptor.decrypt('encrypted.database.password')}</prop> </props> </property> <property name="poolSize" value="5" /> </bean> ``` 在这个示例中,我们使用了JasyptEncryptablePropertyPlaceholderConfigurer作为属性配置器,并将其构造函数的参数设置为encryptor Bean。我们还使用了StandardPBEStringEncryptor来创建encryptor Bean,并将其密码设置为前面在application.properties文件中设置的密码。 在dataSource Bean中,我们使用了encryptor.decrypt()方法来解密数据库URL、用户名和密码。这样,我们就可以安全地存储数据库信息,而不必担心它们被第三方获取到。 需要注意的是,加密和解密的过程是在Spring应用程序的启动过程中完成的。因此,如果我们在应用程序运行更改了加密密码,那么我们需要重新启动应用程序才能使新密码生效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值