2017-04-13 63 views
-1

我使用spring4 +行家编写一个Web项目无法初始化类me.gacl.util.JdbcUtils + mysql.I写了jdbcUtil这样的:java.lang.NoClassDefFoundError:

public class JdbcUtils { 
    private static String driver = null; 
    private static String url = null; 
    private static String username = null; 
    private static String password = null; 

    static{ 
     try{ 
      InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); 
      Properties prop = new Properties(); 
      prop.load(in); 

      driver = prop.getProperty("driver"); 
      url = prop.getProperty("url"); 
      username = prop.getProperty("username"); 
      password = prop.getProperty("password");    
      Class.forName(driver); 

     }catch (Exception e) { 
      e.printStackTrace(); 
      throw new ExceptionInInitializerError(e); 
     } 
    } 
    public static Connection getConnection() throws SQLException{ 
     return DriverManager.getConnection(url, username,password); 
    } 
    public static void release(Connection conn,Statement st,ResultSet rs){ 
     if(rs!=null){ 
      try{ 
       rs.close(); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 
      rs = null; 
     } 
     if(st!=null){ 
      try{ 
       st.close(); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     if(conn!=null){ 
      try{ 
       conn.close(); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

和JDBC测试类是这样的:

public class JdbcCRUDByPreparedStatement { 
    @Test 
     public void insert(){ 
      Connection conn = null; 
      PreparedStatement st = null; 
      ResultSet rs = null; 
      try{ 
       conn = JdbcUtils.getConnection(); 
       String sql = "insert into users(id,name,password,email,school,authority,technical,telenumber) values(?,?,?,?,?,?,?,?)"; 
       st = conn.prepareStatement(sql); 
       st.setInt(1, 1); 
       st.setString(2, "xiaoming"); 
       st.setString(3, "root"); 
       st.setString(4, "[email protected]"); 
       st.setString(5, "nanjing"); 
       st.setInt(6, 0); 
       st.setString(7, "javaweb"); 
       st.setInt(8, 123456); 
       int num = st.executeUpdate(); 
       if(num>0){ 
        System.out.println("insert success!"); 
       } 

      }catch (Exception e) { 
       e.printStackTrace(); 
      }finally{ 
       //SQL执行完成之后释放相关资源 
       JdbcUtils.release(conn, st, rs); 
      } 
     } 

     @Test 
     public void delete(){ 
      Connection conn = null; 
      PreparedStatement st = null; 
      ResultSet rs = null; 
      try{ 
       conn = JdbcUtils.getConnection(); 
       String sql = "delete from users where id=?"; 
       st = conn.prepareStatement(sql); 
       st.setInt(1, 1); 
       int num = st.executeUpdate(); 
       if(num>0){ 
        System.out.println("delete success"); 
       } 
      }catch (Exception e) { 
       e.printStackTrace(); 
      }finally{ 
       JdbcUtils.release(conn, st, rs); 
      } 
     } 

     @Test 
     public void update(){ 
      Connection conn = null; 
      PreparedStatement st = null; 
      ResultSet rs = null; 
      try{ 
       conn = JdbcUtils.getConnection(); 
       String sql = "update users set name=?,email=? where id=?"; 
       st = conn.prepareStatement(sql); 
       st.setString(1, "gacl"); 
       st.setString(2, "[email protected]"); 
       st.setInt(3, 2); 
       int num = st.executeUpdate(); 
       if(num>0){ 
        System.out.println("update success!"); 
       } 
      }catch (Exception e) { 
       e.printStackTrace(); 

      }finally{ 
       JdbcUtils.release(conn, st, rs); 
      } 
     } 

     @Test 
     public void find(){ 
      Connection conn = null; 
      PreparedStatement st = null; 
      ResultSet rs = null; 
      try{ 
       conn = JdbcUtils.getConnection(); 
       String sql = "select * from users where id=?"; 
       st = conn.prepareStatement(sql); 
       st.setInt(1, 1); 
       rs = st.executeQuery(); 
       if(rs.next()){ 
        System.out.println(rs.getString("name")); 
       } 
      }catch (Exception e) { 

      }finally{ 
       JdbcUtils.release(conn, st, rs); 
      } 
     } 
} 

但是当我作为Junite试运行,就不能成功运行,failuer跟踪:

java.lang.NullPointerException 
    at java.util.Properties$LineReader.readLine(Properties.java:434) 
    at java.util.Properties.load0(Properties.java:353) 
    at java.util.Properties.load(Properties.java:341) 
    at me.gacl.util.JdbcUtils.<clinit>(JdbcUtils.java:22) 
    at me.gacl.jdbc.JdbcCRUDByPreparedStatement.delete(JdbcCRUDByPreparedStatement.java:54) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    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:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 

Anyo ne知道如何解决它?

+0

你把你的资源'db.properties'放在哪里? – Harmlezz

+0

@Harmlezz在src/main/resources/configs –

+0

然后尝试'JdbcUtils.class.getClassLoader()。getResourceAsStream(“/ configs/db.properties”);' – Harmlezz

回答

0

检查的结果:

InputStream in = JdbcUtils.class.getClassLoader() 
          .getResourceAsStream("db.properties"); 

如果资源db.properties找不到您将收到null后来导致你的例外在:

prop.load(in); 

Class.getResourceAsStream()状态的Javadoc:

Returns: A InputStream object or null if no resource with this name is found

I f您的资源db.properties与您的课程JdbcUtils位于相同的包装中"db.properties"是正确的参考。否则,如果资源db.properties位于其他位置,则必须使用类似"/db.properties"的前导/来定义路径,该路径指示类路径的根目录。

0

NoClassDefFoundError表示该类在编译时存在于类路径中,但它在运行系统的类路径中不存在。