Recently, this bug has bothered me for two days. I can’t find the answer on the Internet, and it keeps telling me that there is no suitable driver to load. At first I thought that my database version is too high, or whether to consider changing to a lower version. The error report is as follows:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8
### The error may exist in com/Dao/UserDao.xml
### The error may involve com.Dao.UserDao.findAll
### The error occurred while executing a query
### Cause: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8
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.test.MyBatisTest.main(MyBatisTest.java:32)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql//localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
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
The test to connect to the database under Database showed that it was normal. When there was no problem,
I checked the code one by one. Finally, I found that I was really crying by myself, and the error I found was because of a ":" Problem:
Wrong wording:
<property name="url" value="jdbc:mysql//localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8"/>
Correct writing: ( 注意冒号
)
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&characterEncoding=utf-8"/>
It is a lesson and a solution summary of a no suitable driver found for jdbc. It also makes me pay more attention to these details in the future.