老师帮忙看一下代码,运行不出来
driverClass=com.mysql.js.jdbc.Driver; url = mysql:jdbc:///jdbc?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8; user=root; password=marryme533;
package com.imooc.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; /* * JDBC的工具类 * * */ public class JDBCUtils { private static final String driverClass; private static final String url; private static final String user; 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 Auto-generated catch block e.printStackTrace(); } driverClass = props.getProperty("driverClass"); url = props.getProperty("url"); user = props.getProperty("user"); password = props.getProperty("password"); } /* * 注册驱动的方法 * * */ public static void loadDriver() throws ClassNotFoundException { Class.forName(driverClass); } /* * 获得连接的方法 */ public static Connection getConnection() throws Exception { loadDriver(); Connection conn = DriverManager.getConnection(url, user, password); return conn; } /* * 资源释放 */ public static void release(Statement stmt,Connection conn) { if(stmt != null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt=null; } if(conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn=null; } } public static void realease(ResultSet rs,Statement stmt,Connection conn) { if(stmt != null) { try { stmt.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } stmt=null; } if(conn != null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } conn=null; } if(rs != null) { try { rs.close(); }catch(SQLException e) { e.printStackTrace(); } rs=null; } } }
package com.imooc.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; import com.imooc.utils.JDBCUtils; /* * preparedStatemnet的使用 */ public class JDBCDeno3 { @Test public void test3() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql="select * from goods"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while(rs.next()) { System.out.println(rs.getString("name")); } }catch(Exception e) { e.printStackTrace(); }finally { JDBCUtils.realease(rs, pstmt, conn); } } @Test public void demo3() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { //建立连接 conn = JDBCUtils.getConnection(); String sql="insert into goods values (null,?,?,?)"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, "微波炉"); //pstmt.setString(2, "200.0"); pstmt.setString(3, "方便"); pstmt.executeUpdate(); rs = pstmt.executeQuery("select * from goods"); while(rs.next()) { System.out.println(rs.getString("id"+" "+rs.getString("name")+" "+rs.getFloat("price"))); } }catch(Exception e) { e.printStackTrace(); }finally { //释放资源 JDBCUtils.realease(rs, pstmt, conn); } } }
报错:
java.lang.NoClassDefFoundError: Could not initialize class com.imooc.utils.JDBCUtils
at com.imooc.jdbc.JDBCDeno3.demo3(JDBCDeno3.java:59)
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:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星