mybatis配置,idea报错:java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_t

Posted lonelyisland

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis配置,idea报错:java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_t相关的知识,希望对你有一定的参考价值。

报错信息:

"C:\\Program Files\\Java\\jdk1.8.0_211\\bin\\java.exe" "-javaagent:C:\\Program Files\\JetBrains\\IntelliJ IDEA 2019.3.4\\lib\\idea_rt.jar=54826:C:\\Program Files\\JetBrains\\IntelliJ IDEA 2019.3.4\\bin" -Dfile.encoding=UTF-8 -classpath "C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\access-bridge-64.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\cldrdata.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\jaccess.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\jfxrt.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\nashorn.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\sunec.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\sunpkcs11.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\ext\\zipfs.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\jfr.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\jfxswt.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.8.0_211\\jre\\lib\\rt.jar;D:\\file\\java_web_dev\\IdeaProject\\mybatis_test\\target\\test-classes;D:\\file\\java_web_dev\\IdeaProject\\mybatis_test\\target\\classes;E:\\software\\mysql\\mysql-8.0.18-winx64\\mysql-connector-java-8.0.18.jar;C:\\Users\\lonelyisland\\.m2\\repository\\org\\mybatis\\mybatis\\3.5.4\\mybatis-3.5.4.jar;C:\\Users\\lonelyisland\\.m2\\repository\\mysql\\mysql-connector-java\\8.0.18\\mysql-connector-java-8.0.18.jar;C:\\Users\\lonelyisland\\.m2\\repository\\com\\google\\protobuf\\protobuf-java\\3.6.1\\protobuf-java-3.6.1.jar;C:\\Users\\lonelyisland\\.m2\\repository\\log4j\\log4j\\1.2.17\\log4j-1.2.17.jar;C:\\Users\\lonelyisland\\.m2\\repository\\junit\\junit\\4.11\\junit-4.11.jar;C:\\Users\\lonelyisland\\.m2\\repository\\org\\hamcrest\\hamcrest-core\\1.3\\hamcrest-core-1.3.jar" com.syz.test.MybatisTest
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
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.
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_test
### The error may exist in com/syz/dao/IUserDao.xml
### The error may involve com.syz.dao.IUserDao.findAll
### The error occurred while executing a query
### Cause: java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_test
	at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
	at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:144)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
	at com.sun.proxy.$Proxy0.findAll(Unknown Source)
	at com.syz.test.MybatisTest.main(MybatisTest.java:30)
Caused by: java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_test
	at java.sql.DriverManager.getConnection(DriverManager.java:689)
	at java.sql.DriverManager.getConnection(DriverManager.java:208)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:221)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:216)
	at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:95)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:422)
	at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:138)
	at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:60)
	at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:336)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	... 7 more

Process finished with exit code 1

idea链接mysql数据库见下面参考链接1,需要注意的一点,如果数据库没有设置全局的serverTimeZone这里报错,然后去哪个设置页面Advanced那里把该项写为Asia/Shanghai就ok,idea连接后可以在右侧看到如图所示效果

然后尝试连接后并未解决问题(尴尬XD 这步只是起到了数据库连接工具,感觉这个功能集成了datagrip的一些特性),再找其他方法
关键报错是这句话:

参考链接2也列了几种可能得错误。。。也不是
参考链接3终于解决,算是一个非常低级的错误。。。写SqlMapConfig.xml的时候
错误写法

<property name="url" value="jdbc:mysql//localhost:3306/mybatis_test"/>

正确写法,注意有冒号。。。

<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>

算是一个教训,也是一个no suitable driver found for jdbc的一个解决汇总

另外,如果数据库没有设置全局serverTimeZone,这里还可能报个错,一个简单的解决是在url后?加上一些参数(包括时区)

<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT"/>

还有一个地方注意,xml中的&必须写为&否则这里报错

再改:

<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT"/>

到这里我出现的关于idea数据库,以及mybatis数据库配置出现的问题全部解决,小结如下:

  • mybatis出现就是为了解决jdbc配置问题,让使用者更加专注于问题本身,但是配置过程需小心(也是我不熟悉配置的原因,use it or lose it,慢慢熟悉)
  • 配置文件SqlMapConfig.xml中url需要注意"mysql:"的":"
  • xml文件中的";"需要用"&"代替

参考:
idea链接mysql数据库
No Suitable Driver Found For Jdbc 解决方法
mybatis遇到的问题----没有合适的驱动(No suitable driver found for jdbc)
server.xml; lineNumber: 44; columnNumber: 95; 对实体 "characterEncoding" 的引用必须以 \';\' 分隔符结尾。
异常:java.sql.SQLException: The server time zone value \'?й???????\' is unrecognized or represents more

以上是关于mybatis配置,idea报错:java.sql.SQLException: No suitable driver found for jdbc://localhost:3306/mybatis_t的主要内容,如果未能解决你的问题,请参考以下文章

spring boot+mybatis注解使用方式(无xml配置)设置自动驼峰明明转换(),IDEA中xxDao报错could not autowire的解决方法

mybatis的报错____3Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an error

spring boot配置mybatis出现Invalid bound statement (not found)报错的解决办法

IntelliJ IDEA Spring+Mybatis dao bean对象注入失败

mybatis的报错总结_____2Error updating database. Cause: java.sql.SQLSyntaxErrorException: You have an err

在idea中搭建MyBatis可能遇到的问题