IDEA SpringBoot SQL连接常见五大异常处理
IDEA SpringBoot SQL连接常见五大异常处理
文章共2,179字 · 阅读需要大约8分钟
一键AI生成摘要,助你高效阅读
问答
·
项目:Springboot+mybatis+MySQL
- 一、no data sources are configured to run this sql and provide advanced code assistance警告处理
- 二、Loading class `com.mysql.jdbc.Driver'. This is deprecated警告处理
- 三、Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually警告处理
- 四、Failed to configure a DataSource: 'url' attribute is not specified and no embedded警告处理
- 五、Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist警告处理
一、no data sources are configured to run this sql and provide advanced code assistance警告处理
1.异常错误
- SpringBoot项目中.xml或者.sql文件调用时报错
No data sources are configured to run this SQL and provide advanced code assistance.
2.原因
3.解决方法
1.连接Database
在IDEA左下角的小框中,选择Database
在右上角弹出的菜单中,点击+
号,添加Data Source
中的MySQL
2.配置Mysql
填写你需要连接数据库的相关信息,Host本机就为localhost Port默认为3306,确定URL中的库连接
点击Test Connection
,显示如下信息,则表示数据库链接成功
二、Loading class `com.mysql.jdbc.Driver’. This is deprecated警告处理
1.异常错误
- 数据源配置的驱动出错,启动后如果出现以下错误
Loading class com.mysql.jdbc.Driver'. This is deprecated.
The new driver class is com.mysql.cj.jdbc.Driver'.
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2.原因
- MySQL5用的驱动url是com.mysql.jdbc.Driver
- MySQL6以后用的是com.mysql.cj.jdbc.Driver。
- 版本不匹配便会报驱动类已过时的错误。
3.解决方法
当配置好数据库后,还需要在src下找到application.properties
的数据库配置
- 本机MySQL版本5.7,
driver-Class-name
选择com.mysql.jdbc.Driver
- MySQL时8.0以上,选择
com.mysql.cj.jdbc.Driver
三、Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually警告处理
1.异常错误
- 时区设置错误
Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' property manually
2.原因
- 时区不一致,MySQL驱动jar中的默认时区是UTC,代表的是全球标准时间
- 我们使用的时间北京时区是东八区,领先UTC八个小时
com.mysql.cj.jdbc.Driver
是mysql-connector-java 6中的特性- 相比mysql-connector-java 5 多了一个时区
serverTimezone
3.解决方法
在Advanced
中将serverTimezone
设置成Hongkong或者UTC
,然后点击 APPLY
再次尝试 Test Connection
成功连接
四、Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded警告处理
1.异常错误
- url配置数据源出错,未能确定合适的驱动程序类
Failed to configure a DataSource: 'url' attribute is not specified and no embedded
2.原因
pom.xml
文件中添加了mybatis依赖- 在
application.properties
中没有配置连接数据库的url、用户名user 、和密码 password - 在
pom.xml
文件中添加了有关数据库的依赖时,需要在属性文件中配置连接该库的路径,用户名和密码
3.解决方法
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=xxxxxxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
server.port=8004
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
mybatis.mapper-locations=classpath:mapper/*.xml
五、Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist警告处理
1.异常错误
- 未指定对应的数据库
Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist
2.原因
- 出现此问题原因跟四一样,url配置不正确,导致找不到数据库中相关表
- 或者是
mapper.xml
文件中查询条件出错导致
3.解决方法
- 如果是url配置不正确,则参考四的解决方法
- 如果是查询条件出错,查看语法错误并修改
异常索引
- no data sources are configured to run this sql and provide advanced code assistance警告处理
- Loading class `com.mysql.jdbc.Driver’. This is deprecated警告处理
- Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually警告处理
- Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded警告处理
- Cause: java.sql.SQLSyntaxErrorException: Table ‘mybatis.users‘ doesn‘t exist警告处理
更多推荐
已为社区贡献10条内容
所有评论(0)