-1

I am writing a web project using spring4+maven+mysql.I wrote a jdbcUtil like this:

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();
            }
        }
    }
}

and a jdbc test class like this:

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, "939712464@qq.com");
                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, "gacl@sina.com");
                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);
            }
        }
}

but when i run as Junite test,it cannot run successfully,failuer trace:

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)

Anyone know how to fix it?

2 Answers2

0

Check the result of:

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

If the resource db.properties couldn't be found you will receive null which causes your exception later at:

prop.load(in);

The Javadoc of Class.getResourceAsStream() states:

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

If your resource db.properties is located in the same package as your class JdbcUtils the "db.properties" is the right reference. Otherwise, if the resource db.properties is located elsewhere, you must define the path using a leading / like "/db.properties" denoting the root of your classpath.

Harmlezz
  • 7,972
  • 27
  • 35
0

NoClassDefFoundError means that the class is present in the classpath at Compile time, but it doesn't exist in the classpath at Runtime.

Bishal
  • 136
  • 7