老师帮忙看看怎么解决这个问题

来源:4-2 JDBC的工具类的抽取二

小太阳哦

2019-08-12 09:32:21

package com.imooc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import org.junit.Test;
public class GoodsTest {
	@Test
	public void doem() {
		Connection conn=null;
		Statement stat=null;
		ResultSet rslt=null;
		try {
			conn=JDBCUtils.getConnertion();
			String sql0="INSERT goods VALUES(NULL,'手机',2000,'黑色,存储容量32G'),(NULL,'冰箱',1500,'银色,对门开'),(NULL,'洗衣机',3000,'滚筒'), (NULL,'空调',4000,'变频空调')";
			String sql1="select * from goods";
			stat=conn.createStatement();
			int i=stat.executeUpdate(sql0);
			rslt=stat.executeQuery(sql1);
			if(i>0) {
				while(rslt.next()) {
					int id=rslt.getInt("id");
					float price=rslt.getFloat("price");
					String name=rslt.getString("name");
					String desp=rslt.getString("desp");
					System.out.println(id+"\t"+name+"\t"+price+"\t"+desp);
				}
			}
			else {
				System.out.println("数据插入失败");
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		finally {
			JDBCUtils.releasede(rslt, stat, conn);
		}
	}
}
package com.imooc;

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;

/**
 * JDBC的工具类
 * @author Administrator
 *
 */
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 props=new Properties();
		InputStream is=JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			props.load(is);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		driverClass=props.getProperty("driverClass");
		url=props.getProperty("url");
		username=props.getProperty("username");
		password=props.getProperty("password");
	}
	/**
	 * 注册驱动,获得连接的方法
	 * @throws Exception 
	 */
	public static Connection getConnertion() throws Exception {
		Class.forName(driverClass);
		 Connection conn=DriverManager.getConnection(url, username, password);
		 return conn;
	}
	public static void release(Statement stmt,Connection conn) {
		if(stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			conn=null;
		}
	}
	public static void releasede(ResultSet ste,Statement stmt,Connection conn) {
		if(ste!=null) {
			try {
				ste.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			ste=null;
		}
		if(stmt!=null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			stmt=null;
		}
		if(conn!=null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
			conn=null;
		}
	}
	
	}
	
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/javaJDBC+?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
username=root
password=1234

错误提示:

java.lang.NoClassDefFoundError: Could not initialize class com.imooc.JDBCUtils

at com.imooc.GoodsTest.doem(GoodsTest.java:37)

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.junit.runner.JUnitCore.run(JUnitCore.java:137)

at org.junit.runner.JUnitCore.run(JUnitCore.java:115)

at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)

at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(Unknown Source)

at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)

at java.util.Iterator.forEachRemaining(Unknown Source)

at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)

at java.util.stream.AbstractPipeline.copyInto(Unknown Source)

at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)

at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)

at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)

at java.util.stream.AbstractPipeline.evaluate(Unknown Source)

at java.util.stream.ReferencePipeline.forEach(Unknown Source)

at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)

at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)

at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)

at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)

at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)

at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.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)

http://img.mukewang.com/climg/5d50c1840001339902760210.jpg导入的mysql-java包

写回答

1回答

芝芝兰兰

2019-08-12

同学你好。错误信息是:

java.lang.NoClassDefFoundError: Could not initialize class com.imooc.JDBCUtils

含义是com.imooc.JDBCUtils这个类在初始化的时候出现了问题。在初始化的时候,我们使用了配置文件,那么很有可能是配置文件有问题:

看一下同学的目录结构:

http://img.mukewang.com/climg/5d50dd750001c15d02190123.jpg

看一下老师的目录结构:

http://img.mukewang.com/climg/5d50dd910001547502650128.jpg

可以看出,同学的jdbc.properties并没有直接放在src下,而是在com.imooc包中。需要将其直接放在src下哦~

如果解答了同学的疑问,望采纳~

祝学习愉快~


0

0 学习 · 8016 问题

查看课程