jdbcutils初始化失败

jdbcutils初始化失败

二月 23, 2019 10:15:58 下午 org.apache.catalina.core.StandardWrapperValve invoke

严重: Servlet.service() for servlet [addCourse.servlet.addCourseServlet] in context with path [/addCourse] threw exception [Servlet execution threw an exception] with root cause

java.lang.NoClassDefFoundError: Could not initialize class addCourseJDBCUtils.jdbcUtils

at addCourseDaoImpl.courseDaoImpl.addCourse(courseDaoImpl.java:36)

at addCourse.servlet.addCourseServlet.doGet(addCourseServlet.java:35)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)

-----------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>


  <default-config>

    <property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="jdbcUrl">jdbc:mysql:///school</property>

<property name="user">root</property>

<property name="password">123456</property>

<property name="initialPoolSize">5</property>

<property name="maxPoolSize">20</property>

  </default-config>

  

</c3p0-config>

---------------------------------------------------------------------------

package addCourseJDBCUtils;


import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


import com.mchange.v2.c3p0.ComboPooledDataSource;


public class jdbcUtils {

private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();

public static Connection getConn() throws SQLException {

Connection conn =  dataSource.getConnection();

return conn;

}

public static void release(Connection conn,Statement st,ResultSet rs) {

closeConnection(conn);

closeStatement(st);

closeResultSet(rs);

}

public static void release(Connection conn,Statement st) {

closeConnection(conn);

closeStatement(st);

}

private static void closeConnection(Connection conn) {

try {

if(conn!=null) {

conn.close();

}

}catch(Exception ex) {ex.printStackTrace();}

}

private static void closeStatement(Statement st) {

try {

if(st!=null) {

st.close();

}

}catch(Exception ex) {ex.printStackTrace();}

}

private static void closeResultSet(ResultSet rs) {

try {

if(rs!=null) {

rs.close();

}

}catch(Exception ex) {ex.printStackTrace();}

}

}

------------------------------------------------------

package addCourseDaoImpl;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;


import com.mchange.v2.c3p0.ComboPooledDataSource;


import addCourse.model.course;

import addCourseDao.addCourseDao;

import addCourseJDBCUtils.jdbcUtils;


public class courseDaoImpl implements addCourseDao {


@Override

public boolean addCourse(course c) {

Connection conn = null;

PreparedStatement pstmt = null;

boolean flag = false;

try {

conn = jdbcUtils.getConn();

String sql = "insert course values(null,?,?,?,now())";

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, c.getName());

pstmt.setString(2, c.getCategory());

pstmt.setString(3,c.getDesp());

int x = pstmt.executeUpdate();

if(x>0) {

flag = true;

}

}catch(Exception e) {

e.printStackTrace();

}finally {

jdbcUtils.release(conn, pstmt);

}

return flag;

}


@Override

public List<course> FindAllCourse() {

Connection conn = null;

PreparedStatement pstmt = null;

List<course> list = new ArrayList();

ResultSet rs = null;

try {

conn = jdbcUtils.getConn();

String sql = "select * from course";

pstmt = conn.prepareStatement(sql);

rs = pstmt.executeQuery();

while(rs.next()) {

course c = new course();

c.setId(rs.getInt("id"));

c.setName(rs.getString("name"));

c.setCategory(rs.getString("category"));

c.setDesp(rs.getString("desp"));

c.setCreateTime(rs.getDate("createTime"));

list.add(c);

}

}catch(Exception e) {

e.printStackTrace();

}finally {

jdbcUtils.release(conn, pstmt,rs);

}

return list;

}

}


----------------------------------------------

package addCourse.servlet;


import java.io.IOException;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import addCourse.model.course;

import addCourseDaoImpl.courseDaoImpl;


@WebServlet("/addCourseServlet")

public class addCourseServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    public addCourseServlet() {

        super();

    }


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

course c = new course();

String courseName = request.getParameter("courseName");

String category =request.getParameter("category");

String desp =request.getParameter("desp");

if(courseName.isEmpty()||category.isEmpty()||desp.isEmpty()) {

request.getRequestDispatcher("/addCourse.jsp").forward(request, response);

}else {

c.setName(courseName);

c.setCategory(category);

c.setDesp(desp);

courseDaoImpl add= new courseDaoImpl();

add.addCourse(c);

List<course> list = add.FindAllCourse();

request.getSession().setAttribute("list",list);

request.getRequestDispatcher("/showCourse.jsp").forward(request, response);

}

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}


}

------------------------------------------

package addCourse.model;


import java.sql.Date;


public class course {

private int id;

private String name;

private String category;

private String desp;

private Date createTime;

public course(){}

public course(int id, String name, String category, String desp, Date createTime) {

super();

this.id = id;

this.name = name;

this.category = category;

this.desp = desp;

this.createTime = createTime;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getCategory() {

return category;

}

public void setCategory(String category) {

this.category = category;

}

public String getDesp() {

return desp;

}

public void setDesp(String desp) {

this.desp = desp;

}

public Date getCreateTime() {

return createTime;

}

public void setCreateTime(Date date) {

this.createTime = date;

}

@Override

public String toString() {

return "course [id=" + id + ", name=" + name + ", category=" + category + ", desp=" + desp + ", createTime="

+ createTime + "]";

}

}


正在回答

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

1回答

报错信息提示:

java.lang.NoClassDefFoundError: Could not initialize class addCourseJDBCUtils.jdbcUtils

不能初始化类addCourseJDBCUtils的jdbcUtils

同学的数据库是哪个版本的呢?如果是8.0的,请按照教辅:

http://img1.sycdn.imooc.com//climg/5c7250bf0001e9b703930385.jpg

设置一下。

如果是5.0版本的,建议同学看看驱动包是否是放在lib文件夹下了?版本是否正确呢?

祝学习愉快。

  • 慕粉4191198 提问者 #1
    版本是5.1.7,lib目录下面有了,用那个测试类可以添加和查看数据库,但是通过那个servlet进去c3p0没起作用conn是null
    2019-02-24 17:08:40
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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