无法初始化JDBCUtils的工具类

无法初始化JDBCUtils的工具类

困扰很多天了,还没没找到原因。

错误信息

java.lang.NoClassDefFoundError: Could not initialize class com.imooc.utils.JDBCUtils
	com.imooc.dao.impl.UserDaoImpl.isRegisted(UserDaoImpl.java:50)
	com.imooc.service.impl.UserServiceImpl.regist(UserServiceImpl.java:13)
	com.imooc.web.action.RegistServlet.regist(RegistServlet.java:47)
	com.imooc.web.action.RegistServlet.doGet(RegistServlet.java:29)
	com.imooc.web.action.RegistServlet.doPost(RegistServlet.java:17)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	com.imooc.web.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:14)

JDBCUtils工具类

package com.imooc.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.*;

public class JDBCUtils {

    public static final ComboPooledDataSource dataSource = new ComboPooledDataSource();

    /**
     * 获得连接池
     * @return
     */
    public static DataSource getDataSource() {
        return dataSource;
    }

    /**
     * 获得连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /**
     * 释放资源
     */
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

    public static void release(Statement stmt,Connection conn){
        if(stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿满 2019-05-10 15:32:07

同学你好,只看JDBCUtils工具类无法得到具体的错误,建议同学将全部的报错信息贴出来。

祝:学习愉快~

  • 提问者 飛絮飄零_joker #1
    Type Exception Report Message Servlet execution threw an exception Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) com.imooc.web.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:14) Root Cause java.lang.NoClassDefFoundError: Could not initialize class com.imooc.utils.JDBCUtils com.imooc.dao.impl.UserDaoImpl.isRegisted(UserDaoImpl.java:50) com.imooc.service.impl.UserServiceImpl.regist(UserServiceImpl.java:13) com.imooc.web.action.RegistServlet.regist(RegistServlet.java:47) com.imooc.web.action.RegistServlet.doGet(RegistServlet.java:29) com.imooc.web.action.RegistServlet.doPost(RegistServlet.java:17) javax.servlet.http.HttpServlet.service(HttpServlet.java:660) javax.servlet.http.HttpServlet.service(HttpServlet.java:741) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) com.imooc.web.filter.CharsetEncodingFilter.doFilter(CharsetEncodingFilter.java:14) Note The full stack trace of the root cause is available in the server logs.
    2019-05-10 15:33:48
  • 同学贴出来的是网页中的错误吗?建议同学将控制台的报错贴出来,另外记得贴在回答中,贴在回复中会失去格式,不好查找错误。祝:学习愉快~
    2019-05-10 16:19:55
  • 问题找到了,少导了一个c3p0的jar包mchange-commons-java-0.2.11.jar
    2019-05-10 17:51:17
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师