java.lang.NoClassDefFoundError

java.lang.NoClassDefFoundError

package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
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 service.addservice;
import test.Test;
import utils.utils;

//@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //获得输入内容
        String name=request.getParameter("class");
        String path=request.getParameter("path");
        String desp=request.getParameter("desp");
        //获得时间
        Calendar ca = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date time1=ca.getTime();
        String time=formatter.format(time1);
        //调用方法
        
        int i=addservice.setJDBC(name, path, desp, time);
        System.out.println("aaa");
        if(i>0) {
            //跳转链接至显示servlet
            request.getRequestDispatcher("/showServlet").forward(request, response);
        }
    }

}
package servlet;

import java.io.IOException;
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 service.addservice;


public class showServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        request.setAttribute("list", addservice.getjdbc());
        request.getRequestDispatcher("/showCourse.jsp").forward(request, response);
    }

}

servlet

package service;
//添加到数据库的方法

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import test.Test;

import utils.utils;

public class addservice {
    //设置jdbc方法
    public static int setJDBC(String name,String path,String desp,String time) {
         
        //数据表中数据集合
        List<Test> classlist=new ArrayList<Test>();
        //将对象添加进数据库
        ResultSet rs=null;
        Connection conn=null;
        PreparedStatement pstmt=null;
        int i=0;
        try {
            //获得链接
            conn=utils.getConnection();
             String sql="insert class values(?,?,?,?)";
            // String sql1="select *from class";
             pstmt=conn.prepareStatement(sql);
             pstmt.setString(1, name);
             pstmt.setString(2, path);
             pstmt.setString(3, desp);
             pstmt.setString(4, time);
             i =pstmt.executeUpdate();
            // rs=pstmt.executeQuery(sql1);
             //如果添加成功返回i
             
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            utils.release(conn, pstmt);
        }
        
        return i;
    }
    //数据库封装进集合方法
    public static List getjdbc() {
        //新建list
        List<Test> testlist=new ArrayList();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs= null;
        Test t=new Test();
        try {
            conn=utils.getConnection();
            String sql="select*from class";
            pstmt=conn.prepareStatement(sql);
            rs=pstmt.executeQuery();    
            while(rs.next()) {
                t.setName(rs.getString("name"));
                t.setPath(rs.getString("path"));
                t.setDesp(rs.getString("desp"));
                t.setTime(rs.getString("time"));
                testlist.add(t);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            utils.release(conn, pstmt, rs);
        }
        return testlist;
    }
    
}

service

package test;
//课程类
public class Test {
    private String name;
    private String path;
    private String desp;
    private String time;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    public String getDesp() {
        return desp;
    }
    public void setDesp(String desp) {
        this.desp = desp;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public Test(String name, String path, String desp, String time) {
        super();
        this.name = name;
        this.path = path;
        this.desp = desp;
        this.time = time;
    }
    public Test() {
        
    }
}

课程类

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>添加成功</h1>
<table border=10>
<tr>
<td>名称</td>
<td>方向</td>
<td>介绍</td>
<td>时间</td>
</tr>
<c:forEach items="${list}" var="n">
<tr>
<td>${n.name}</td>
<td>${n.path}</td>
<td>${n.desp}</td>
<td>${n.time}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/jdbcwork1/addServlet" enctype="multipart/form-data" method="post">
    <h1>课程添加</h1>
    课程名 <input type="text" placehoder="课程名" name="class"></br>
    方向<input type="text" placehoder="方向" name="path"></br>
    简介<input type="text" placehoder="介绍" name="desp"></br>
    <input type="submit" value="添加" >
    </form>
</body>
</html>


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

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

1回答
好帮手慕阿满 2019-06-04 11:58:12

同学你好,java.lang.NoClassDefFoundError这个错误,是加载类时,一些资源没有加载到,会报这个错误,问一下同学具体是那个类呢?建议同学将具体的错误信息贴一下。另外同学贴出来的代码中,有些地方不规范。比如类在命名时,首字母应该大写的。

祝:学习愉快~

  • 提问者 中野二乃 #1
    utils没有加载
    2019-06-04 12:03:33
  • 好帮手慕阿满 回复 提问者 中野二乃 #2
    建议同学重启项目再试试,如果还是不可以,建议同学将项目结构图贴一下。祝:学习愉快~
    2019-06-04 15:10:37
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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