学mysql中遇到的一系列问题,整合如下

在最近做一些java web整合时,因为我在maven官网查找的资源,使用的最新版,6.0.3,发现Mysql连接中的几个问题,总结如下:


1、Loading class `com.mysql.jdbc.Driver'.This isdeprecated. The new driver class is `com.mysql.cj.jdbc.Driver。

按照书上的教程,写了如下的数据库连接配置:

[html] view plain copy
  1. user=root  
  2. password=  
  3. driverClass=com.mysql.jdbc.Driver  
  4. jdbcUrl=jdbc:mysql://localhost:3306/spring  
以上配置中,密码我是默认没有设置的。在连接的时候出现以上提示,根据提示,很显然,这种driver配置方式在此版本中已经被废弃,因此需要将driverClass配置为:com.mysql.cj.jdbc.Driver。


2、如下提示:

警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1a0e2e48 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
七月 05, 2016 8:58:29 下午 com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
警告: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1a0e2e48 -- APPARENT DEADLOCK!!! Complete Status: 
...
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.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:695)

搜索了一下 需要在Url后面添加一个参数:

[java] view plain copy
  1. ?serverTimezone=UTC  

即完整的配置应修改为:

[html] view plain copy
  1. user=root  
  2. password=  
  3. #old driver   
  4. #driverClass=com.mysql.jdbc.Driver  
  5. #new driver is as follow:  
  6. driverClass=com.mysql.cj.jdbc.Driver  
  7. jdbcUrl=jdbc:mysql://localhost:3306/spring?serverTimezone=UTC  

然后才能正常连接,加上后测试,连接成功。下图是测试代码以及测试结果:



3、Url中localhost:3306 可以省略,看到有的地方直接写的三个\\\,省略ip和端口,然后直接接某个数据库。因此测试了一下,将配置改为:

[html] view plain copy
  1. user=root  
  2. password=  
  3. driverClass=com.mysql.cj.jdbc.Driver  
  4. jdbcUrl=jdbc:mysql:///spring?serverTimezone=UTC  

发现也是可以连上的。


4、使用Hibernate整合连接mysql时,出现以下错误:

Disabling contextual LOB creation as createClob() method threw error :java. lang. reflect. InvocationTargetException。

原因: at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:244) ,就是在配置的时候没取到值。那么为什么没取到值呢,是因为Hibernate默认是使用jdbc方式来取的,如果使用连接池的方式的话,必须告诉Hibernate一声,让它不使用单纯的JDBC连接。因此在Hibernate的hibernate.cfg.xml中加入一条属性:

[html] view plain copy
  1. <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>  
但是在加入此属性时,有一点比较疑惑的是,在引号中输入hibernate后使用自动补全,发现temp属性没有,不知道这是怎么回事。截图如下:


但直接复制以上代码进去,也不会出错。。只能怀疑是eclipse补全功能的锅了!

猜你喜欢

转载自blog.csdn.net/hgh3223273424/article/details/80587475