⽅法
使⽤l连接JDBC数据库的时候碰到⼀些问题,记录⼀下。
下⾯是我最开始对着书敲的代码,需要改正和特别注意的地⽅⽤红框圈出来了:
改正后换成和MySQL⼀致的版本→
放⼀张最终运⾏成功的l代码图⽚及改正后的值:
value="sql.cj.jdbc.Driver"
value="jdbc:mysql://localhost/spring?useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true"
下⾯是运⾏时出现的具体错误及改正⽅法:
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数据传输安全利⽤数据加密)
解决⽅法:把<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 sq ......
mysql下载jar包//重点是这⾥,我的MySql版本是8.0的,但是导⼊jar包的时候是5.1版本的
//并且数据驱动写的是value="sql.jdbc.Driver",8.0版本的应该是value="sql.cj.jdbc.Driver"
Caused by: ptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
错误提⽰:Could not create connection to database server.
错误原因:我的MySql版本是8.0的,但是导⼊jar包的时候是5.1版本的,并且数据驱动写的是value="sql.jdbc.Driver",8.0版本的应该是value="sql.cj.jdbc.Driver"。
解决⽅法:jar包换成8.0版本的。并且value改成value="sql.cj.jdbc.Driver"。
3.第三次运⾏出现错误:
Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.S ......
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)获取到
明⽂密码,所以默认是关闭的,必须显式开启。
解决⽅法:在value="jdbc:mysql://localhost/spring?useSSL=false"后添加allowPublicKeyRetrieval=true
如:value="jdbc:mysql://localhost/spring?useSSL=false&allowPublicKeyRetrieval=true"
注意:这⾥有些⼈是⽤&连接的,如:useSSL=false&allowPublicKeyRetrieval=true",但是我在这⾥⽤&会报错,应该改成&
报错提⽰:对实体 "allowPublicKeyRetrieval" 的引⽤必须以 ';' 分隔符结尾。
Exception in thread "main" org.springframework.l.XmlBeanDefinitionStoreException: Line 13 in XML document from class path resource [ ......
Caused by: l.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.S ......
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.S ......
Caused by: java.sql.SQLException: The server time zone value 'ú±ê×??±??' is unrecognized or represents more than one time zone. You must configure eith ......
Caused by: ptions.InvalidConnectionAttributeException: The server time zone value 'ú±ê×??±??' is unrecognized or represents more than o
解决⽅法:在value="jdbc:mysql://localhost/spring?useSSL=false&&allowPublicKeyRetrieval=true"后加
上&serverTimezone=UTC
如:value="jdbc:mysql://localhost/spring?
useSSL=false&serverTimezone=UTC&&allowPublicKeyRetrieval=true"
6.第六次。。。终于运⾏成功了(欢呼)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论