java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError

package com.andreas.jdbcdemo;

import java.sql.Connection;
import java.sql.Statement;

import org.junit.Test;

import com.andreas.jdbc_utils.JdbcUtils;

public class TestToolClass {
	@Test
	/*
	 * 插入数据库一条记录
	 */
	public void inserttest() {
		Connection conn = null;
		Statement sta = null;
		try {
			// 建立连接
			conn = JdbcUtils.getConnection();
			// 创建sql类
			sta = conn.createStatement();
			// 编写sql语句
			String sql = "insert goods(name,price,desp)value('洗碗机',5326.2,'银色');";
			// 执行sql语句
			int msg = sta.executeUpdate(sql);
			if(msg>0) {
				System.out.println("插入成功");
			}else {
				System.out.println("插入失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放资源
			JdbcUtils.release(sta, conn);
		}
	}
}
package com.andreas.jdbc_utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 设置便于修改的静态变量
 */
public class JdbcUtils {

	private static final String driverClass;
	private static final String url;
	private static final String username;
	private static final String password;

	/**
	 * 初始化静态数据的静态代码块
	 */
	static {
		// 加载属性文件并解析
		Properties prop = new Properties();
		// 获得属性文件的输入流
		    // 通常使用类的加载器的方式进行获取
		        // 一般不会采用FilleInputStream("src/jdbc.properties")
		            // 一旦有WEB项目发布到服务器中,将无法使用src这种路径
		InputStream ins = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			prop.load(ins);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driverClass = prop.getProperty("driverClass");
		url = prop.getProperty("url");
		username = prop.getProperty("username");
		password = prop.getProperty("password");
	}

	/**
	 * 注册驱动的方法
	 * 
	 * @throws ClassNotFoundException
	 */
	public static void loadriver() throws ClassNotFoundException {
		Class.forName(driverClass);
	}

	/**
	 * 获得连接的方法
	 * 
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws Exception {
		// 在获得连接之前最好在加载一次驱动
		loadriver();
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}

	/**
	 * 释放资源的方法
	 */
	public static void release(Statement sta, Connection conn) {
		if (sta != null) {
			try {
				sta.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sta = null;
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn = null;
		}
	}

	public static void release(Statement sta, Connection conn, ResultSet res) {
		if (sta != null) {
			try {
				sta.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sta = null;
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn = null;
		}
		if (res != null) {
			try {
				res.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			res = null;
		}
	}
}
		driverClass=com.mysql.jdbc.Driver
		url=jdbc:mysql://localhost:3306/jdbc?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8&userSSL=false
		username=root
		password=123456
CREATE DATABASE jdbc;
CREATE TABLE goods(
    id int unsigned auto_increment KEY,
    name varchar(20) NOT NULL comment'商品名',
    price float(5,1) NOT NULL comment'商品价格',
    desp varchar(30) NOT NULL comment'商品描述'
);


INSERT goods(name,price,desp)values('手机',2000.0,'黑色,6+128'),
('冰箱',1500.0,'银色,对开门'),
('洗衣机',3000.0,'滚筒洗衣机'),
('空调',4000.0,'变频空调');

报错:http://img1.sycdn.imooc.com//climg/5eda10180939491300000000.jpg

报错信息

java.lang.NoClassDefFoundError: Could not initialize class com.andreas.jdbc_utils.JdbcUtils
	at com.andreas.jdbcdemo.TestToolClass.inserttest(TestToolClass.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
java.lang.NoClassDefFoundError: Could not initialize class com.andreas.jdbc_utils.JdbcUtils
	at com.andreas.jdbcdemo.TestToolClass.inserttest(TestToolClass.java:36)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

这是什么原因啊

正在回答

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

1回答

同学你好,你的代码运行没有问题。同学可以看一下配置文件是否在src目录下。

如下:

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

祝学习愉快!

  • 孬帮手慕小菜 提问者 #1
    确实放错地方了啊哈哈哈,谢谢老师
    2020-06-05 18:55:28
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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