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

Error message:

"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 link to the mysql database, see the reference link 1 below. One thing to note, if the database does not set the global serverTimeZone, an error is reported here, and then go to which setting page Advanced where the item is written as Asia / Shanghai and it is ok. After idea is connected, it can be on the right see the results shown in Figure

and then try the connection does not solve the problem (this step is just awkward XD played a database connection tools, feel this function integrates some of the features of datagrip), find other ways
key is being given this sentence:

reference Link 2 also lists several possible errors. . . Nor is it
reference link 3 finally resolved, which is considered a very low-level error. . .
Wrong way of writing when writing SqlMapConfig.xml

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

Write it correctly and pay attention to the colon. . .

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

It is a lesson and a summary of no suitable driver found for jdbc

In addition, if the database does not set the global serverTimeZone, an error may be reported here. A simple solution is to add some parameters (including time zone) after the url?

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

There is also a place to note that the & in the xml must be written as & otherwise an error

will be reported here and then changed:

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

Here I have solved all the problems related to the idea database and mybatis database configuration. The summary is as follows:

  • Mybatis appeared to solve the jdbc configuration problem and let users focus more on the problem itself, but the configuration process needs to be careful (also the reason why I am not familiar with the configuration, use it or lose it, slowly become familiar with)
  • The url in the configuration file SqlMapConfig.xml needs to pay attention to "mysql:" of ":"
  • ";" in the xml file needs to be replaced with "&"

Reference:
Idea link mysql database
No Suitable Driver Found For Jdbc Solution
Mybatis encountered problems-no suitable driver (No suitable driver found for jdbc)
server.xml; lineNumber: 44; columnNumber: 95; for entities " The characterEncoding "reference must end with a ';' separator.
Exception: java.sql.SQLException: The server time zone value '? Й ???????' is unrecognized or represents more

Guess you like

Origin www.cnblogs.com/lonelyisland/p/12677074.html