日期:2014-05-17  浏览次数:20913 次

java.lang.NoClassDefFoundError: Could not initialize class cn.elvis.utils.JdbcUt
写的jdbc工具类,前期写了测试正确,但发布到tomcat中就报这样的错误。
javax.servlet.ServletException: Servlet execution threw an exception


root cause 

java.lang.NoClassDefFoundError: Could not initialize class cn.elvis.utils.JdbcUtils
cn.elvis.dao.impl.UserDaoImpl.find(UserDaoImpl.java:180)
cn.elvis.service.impl.BusinessServiceImpl.register(BusinessServiceImpl.java:15)
cn.elvis.web.controller.RegisterServlet.doGet(RegisterServlet.java:40)
cn.elvis.web.controller.RegisterServlet.doPost(RegisterServlet.java:77)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

下面是我的工具类代码
Java code

public final class JdbcUtils {

    private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=JspWebDb";
    private static String user="sa";
    private static String psw="12345";
    
    private JdbcUtils(){}
    
    private static JdbcUtils instanse=new JdbcUtils();
    
    public static JdbcUtils getInstanse()
    {
        return instanse;
    }
    
    static{
        try{
            Class.forName(driver);
        }
        catch(ClassNotFoundException e)
        {
            throw new ExceptionInInitializerError(e);
        }
        
    }
    
    public static Connection getConnection() throws SQLException
    {
        return DriverManager.getConnection(url,user,psw);
     }
    
    
    public static void free(ResultSet rs,Statement st,Connection conn)
    {
        if(rs!=null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally
            {
                if(st!=null)
                    try {
                        st.close();
                    } catch (SQLException e) {
                        
                        e.printStackTrace();
                    }
                    finally
                    {
                        if(conn!=null)
                            try {
                                conn.close();
                            } catch (SQLException e) {
                                
                                e.printStackTrace();
                            }
                    }
            }
    }
    
    
    
    
}





------解决方案--------------------
探讨

驱动包要自己手动放入吗?