No suitable driver found for jdbc
import org.junit.Test; import java.sql.*; public class jdbc { @Test public void demo(){ try { // 1、加载驱动程序 Class.forName("com.mysql.cj.jdbc.Driver"); //2、创建连接 Connection imooc = DriverManager.getConnection( "jdbc:mysql//192.168.3.7:3306/imooc"+ "?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8", "root","zhy632746542"); //3、创建进行SQL解析的对象 String select = "select * from news"; Statement sql = imooc.createStatement(); ResultSet set = sql.executeQuery(select); while (set.next()) { int id = set.getInt("id"); String title = set.getString("title"); String content = set.getString("content"); System.out.println("id"+" "+id+" "+"title"+title+""+"content"+content); } //4、释放资源 set.close(); sql.close(); imooc.close(); } catch (SQLException e) { e.printStackTrace(); }catch (ClassNotFoundException e) { e.printStackTrace(); } } }
下面是报错
java.sql.SQLException: No suitable driver found for jdbc:mysql//192.168.3.7:3306/imooc?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at jdbc.demo(jdbc.java:10)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
正在回答
从同学的代码中看出,同学使用的是mysql8的版本吧,那么同学用的数据库的驱动是 对应的驱动吗?
如果版本应,建议同学在jar包上右键,选择Build Path --Add to Bulid Path 试试。
祝学习愉快。
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星