Could not initialize class

Could not initialize class

package com.imooc.mybatis.entity;

public class Students {
   private Integer id;
   private  Integer regNo;
   private String NAME;
   private String sex;
   private Integer age;
   private String grade;
   private String major;

   public Integer getId() {
       return id;
   }

   public void setId(Integer id) {
       this.id = id;
   }

   public Integer getRegNo() {
       return regNo;
   }

   public void setRegNo(Integer regNo) {
       this.regNo = regNo;
   }

   public String getNAME() {
       return NAME;
   }

   public void setNAME(String NAME) {
       this.NAME = NAME;
   }

   public String getSex() {
       return sex;
   }

   public void setSex(String sex) {
       this.sex = sex;
   }

   public Integer getAge() {
       return age;
   }

   public void setAge(Integer age) {
       this.age = age;
   }

   public String getGrade() {
       return grade;
   }

   public void setGrade(String grade) {
       this.grade = grade;
   }

   public String getMajor() {
       return major;
   }

   public void setMajor(String major) {
       this.major = major;
   }
}


package com.imooc.mybatis.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class MyBatisUtils {
   public static SqlSessionFactory sqlSessionFactory = null;
   static {
       Reader reader = null;
       try{
           reader = Resources.getResourceAsReader("mybatis-config.xml");
           sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
       }catch (IOException e){
          e.printStackTrace();
          throw new ExceptionInInitializerError(e);
       }
   }
   public static SqlSession openSession(){
       return sqlSessionFactory.openSession();
   }
   public static void closeSession(SqlSession session){
       if(session != null){
           session.close();

       }
   }
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
       PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="students">
   <select id="selectAll" resultType="com.imooc.mybatis.entity.Students">
       select * from students.student order by id desc limit 10
   </select>
</mapper>


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
       PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
       "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
   <settings>
       <setting name="mapUnderscoreToCamelCase" value="true"/>
   </settings>
   <environments default="dev">
       <environment id="dev">
           <transactionManager type="JDBC"></transactionManager>
           <dataSource type="POOLED">
               <property name="driver" value="com.mysql.jdbc.Driver"/>
               <property name="url" value="jdbc:mysql://localhost:3306/student?useUnicode=true&amp;characterEncoding=UTF-8"/>
               <property name="usename" value="root"/>
               <property name="password" value="123"/>
           </dataSource>
       </environment>
   </environments>
   <mappers>
       <mapper resource="mappers/students.xml"></mapper>
   </mappers>
</configuration>


package com.imooc.mybatis;

import com.imooc.mybatis.entity.Students;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyBatisTestor {
   @Test
   public  void testSelectAll() throws Exception {
       SqlSession session = null;
       try{
           session = MyBatisUtils.openSession();
           List<Students> list = session.selectList("students.selectAll");
           for (Students s:list){
               System.out.println(s.getId()+" "+s.getRegNo()+" "+s.getNAME()+" "+s.getAge()+" " +
                       s.getSex()+" "+s.getGrade()+" "+s.getMajor());
           }

       }catch (Exception e){
           throw e;
       }finally {
           MyBatisUtils.closeSession(session);
       }
   }
}


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>

   <groupId>com.imooc.mybatis1</groupId>
   <artifactId>mybatis1</artifactId>
   <version>1.0-SNAPSHOT</version>
   <repositories>
       <repository>
           <id>aliyun</id>
           <name>aliyun</name>
           <url>http://maven.aliyun.com/repository/public</url>
       </repository>
   </repositories>
   <dependencies>
       <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis</artifactId>
           <version>3.5.1</version>
       </dependency>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.47</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
       </dependency>
   </dependencies>
</project>


CREATE DATABASE students

USE students

CREATE TABLE student(

id INT NOT NULL AUTO_INCREMENT KEY,

reg_no INT NOT NULL,

NAME VARCHAR(20) NOT NULL,

sex VARCHAR(20) NOT NULL,

age INT NOT NULL,

grade VARCHAR(20) NOT NULL,

major VARCHAR(50)NOT NULL

)

DESC student

INSERT INTO student VALUES(NULL,20171201,"梅长苏","男",31,"2008","哲学系"),

(NULL,20171201,"梅长苏","男",31,"2008","哲学系"),

(NULL,20171202,"萧景琰","男",30,"2008","政治系"),

(NULL,20171203,"飞流","男",18,"20016","人体系"),

(NULL,20171204,"萧景桓","男",35,"2006","政治系"),

(NULL,20171205,"宫羽","女",25,"2013","声乐系"),

(NULL,20171206,"霓凰","女",31,"2008","国际关系专业"),

(NULL,20171207,"秦般弱","女",30,"2009","通信专业");



"C:\Program Files\Java\jdk1.8.0_101\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\APP\JetBrains\IntelliJ IDEA 2018.3.2\lib\idea_rt.jar=7811:C:\APP\JetBrains\IntelliJ IDEA 2018.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\APP\JetBrains\IntelliJ IDEA 2018.3.2\lib\idea_rt.jar;C:\APP\JetBrains\IntelliJ IDEA 2018.3.2\plugins\junit\lib\junit-rt.jar;C:\APP\JetBrains\IntelliJ IDEA 2018.3.2\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_101\jre\lib\rt.jar;C:\Users\Admin\IdeaProjects\mybatis1\target\test-classes;C:\Users\Admin\IdeaProjects\mybatis1\target\classes;D:\maven-repo\org\mybatis\mybatis\3.5.1\mybatis-3.5.1.jar;D:\maven-repo\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar;D:\maven-repo\junit\junit\4.12\junit-4.12.jar;D:\maven-repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.imooc.mybatis.MyBatisTestor,testSelectAll


java.lang.NoClassDefFoundError: Could not initialize class com.imooc.mybatis.utils.MyBatisUtils


at com.imooc.mybatis.MyBatisTestor.testSelectAll(MyBatisTestor.java:25)

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)


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

5回答
好帮手慕阿满 2020-04-21 14:42:01

同学你好,这里需要删除本地仓库中存在的错误的依赖,例如:

http://img1.sycdn.imooc.com//climg/5e9e95a80941815105360135.jpg

祝:学习愉快~

好帮手慕阿满 2020-04-16 16:32:28

同学你好,关于同学的问题:

1、在pom文件中引入错误的依赖,本地仓库中会存在错误的文件夹。之后再次引入时,可能会提示错误的依赖。这里应该删除错误的本地仓库中错误的文件。

2、本地仓库中存在对应的错误文件夹,所以引入错误时,External Libraries下会出现错误的依赖。

祝:学习愉快~

  • 提问者 慕的地2082093 #1
    老师,我这边删除错误文件要怎么删?
    2020-04-21 11:07:10
好帮手慕阿满 2020-04-16 10:02:24

同学你好,students.student是没有错误的,这里是指查询students数据库中的student表。

例如:查询jdbctest数据库中的user表:

SELECT * FROM jdbctest.user  ORDER BY uid desc LIMIT 10;

http://img1.sycdn.imooc.com//climg/5e97bc8c0944456209020569.jpg

祝:学习愉快~

RekklesO 2020-04-15 22:56:03

你好,你的代码是有错误的,你看看你的数据库查询语句

select * from students.student order by id desc limit 10

其中students.student 是错误的。

以我的经验之谈,出现Could not initialize class(找不到数据库连接工具类)的错误,大概率都是你的数据库查询xml文件出现了错误,通俗点讲就是你的数据库查询语句出现错误,下次如果你又遇到了这个错误,我建议你先看一下你的查询语句


好帮手慕阿满 2020-04-15 15:37:45

同学你好,这边测试同学的代码,并没有什么问题。但是同学报错运行时找不到MyBatisUtils类,如:

http://img1.sycdn.imooc.com//climg/5e96b686093d9fe407960068.jpg

建议同学删除target文件,重启项目再试试。

如果还是报错,建议同学先注释掉报错的第25行,应该是调用closeSession()方法的代码。如:

http://img1.sycdn.imooc.com//climg/5e96b93b099ec61406420069.jpg

http://img1.sycdn.imooc.com//climg/5e96b97b09c78f5506990113.jpg

这里先不关闭session,查看具体报什么错误。

另外,建议同学命名规范一些,Students类中的NAME改为name。

祝:学习愉快~~

  • 老师,您看漏眼了吧,数据库查询语句错误了吧 select * from students.student order by id desc limit 10 其中的students.student这里错了,这个找不到工具类的错误,我遇到很多次,一直百度我也没找出为什么,后来我发现出现这个错误大概率是数据库查询xml文件的查询语句出现问题
    2020-04-15 22:52:59
  • 提问者 慕的地2082093 #2
    刚发现是数据库的名字students写成了表名student还有username写成了usename <property name="url" value="jdbc:mysql://localhost:3306/student?useUnicode=true&amp;characterEncoding=UTF-8"/> <property name="usename" value="root"/> 老师还有几个问题不太清楚(使用的环境为IDEA): 1.开始把mysql-connector-java写成mysql-connection-java(运行发现错误后更改成了mysql-connector-java),后面新建新项目使用dependency引入mysql时,artifactId提示的代码是mysql-connection-java,为什么不是mysql-connector-java,提示的代码也会有错误吗? 2.mysql的引用为什么写成mysql-connection-javaExternal Libraries也会自动生成对应的东西 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connection-java</artifactId> <version>5.1.47</version> </dependency>
    2020-04-16 10:21:45
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师