学习jdbc踩过的坑

注册驱动时:

Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

  • 数据库驱动com.mysql.jdbc.Driver已经被弃用了,应当使用新的驱动com.mysql.cj.jdbc.Driver

连接jdbc时:

The server time zone value ‘�й���׼ʱ��’ is unrecogni

  • 使用了Mysql Connector/J 6.x以上的版本,然后就报了时区的错误。加入参数serverTimezone=UTC就好了。

执行sql语句时:

check the manual that corresponds to your MySQL server version for the right syntax to use near

  • 可能1:表名使用了Mysql的关键字
  • 可能2:“near”之后提示的地方有打错的地方,比如多个符号少个字母等等

java.sql.BatchUpdateException: No value specified for parameter 5

  • 使用了PreparedStatement预处理,但是参数的个数与占位符?的个数不匹配

查询日期字段时:

查询日期字段为2020-02-27的数据时返回2020-02-26的数据

  • 连接jdbc时出现时区错误加入参数serverTimezone=UTC,而UTC为协调世界时,中国的时间与UTC的时差为+8小时。所以会晚一天,把UTC改为Asia/Shanghai即可

更新数据库数据时:

Duplicate entry ‘xxx’ for key ‘yyy’

  • 原因:向唯一字段yyy插入相同数据xxx

连接数据池时:

Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory

  • 没有引入logging的jar包

获取数据内容要读取时:

Column Index out of range,

  • 可能1:jdbc取值是从1开始,可能是取值为0
  • 可能2:取值大于索引范围

通过读取属性文件中的配置信息创建连接池时:

java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)

  • 可能1:文件路径错了
  • 可能2:文件名错了
发布了25 篇原创文章 · 获赞 71 · 访问量 8659

猜你喜欢

转载自blog.csdn.net/weixin_44689154/article/details/104407843