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,'变频空调');报错:
报错信息
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)
这是什么原因啊
17
收起
正在回答
1回答
同学你好,你的代码运行没有问题。同学可以看一下配置文件是否在src目录下。
如下:

祝学习愉快!
3. Java 数据库开发与实战应用
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星