Could not initialize class com

Could not initialize class com

package com.imooc.jdbc.utils;


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 {

public static final ComboPooledDataSource datasource = new ComboPooledDataSource();

/**

* 建立连接

* @return

* @throws Exception

*/

public static Connection  getConnection() throws Exception {

Connection conn=datasource.getConnection();

return conn;

}

/**

* 释放资源

*/

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

if(stmt != null) {

try {

stmt.close();

}catch(Exception e){

e.printStackTrace();

}

stmt = null;

}

if(conn != null) {

try {

conn.close();

}catch(Exception e){

e.printStackTrace();

}

conn = null;

}

}

/**

* 释放资源

*/

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

if(rs != null) {

try {

rs.close();

}catch(Exception e){

e.printStackTrace();

}

rs = null;

}

if(stmt != null) {

try {

stmt.close();

}catch(Exception e){

e.printStackTrace();

}

stmt = null;

}

if(conn != null) {

try {

conn.close();

}catch(Exception e){

e.printStackTrace();

}

conn = null;

}

}

}

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

<c3p0-config>


  <default-config>

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

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

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

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

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

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

  </default-config>

  

</c3p0-config>

package com.imooc.jdbc.servlet;


import java.io.IOException;

import java.sql.Connection;

import java.sql.Date;

import java.sql.PreparedStatement;


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 com.imooc.jdbc.domain.Course;

import com.imooc.jdbc.utils.JDBCUtils;


/**

 * Servlet implementation class addCourseServlet

 */

@WebServlet("/addCourseServlet")

public class addCourseServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public addCourseServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

//response.getWriter().append("Served at: ").append(request.getContextPath());

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

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

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

Connection conn = null;

PreparedStatement pstmt = null;

try {

conn = JDBCUtils.getConnection();

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

pstmt = conn.prepareStatement(sql);

pstmt.setString(1, name);

pstmt.setString(2, category);

pstmt.setString(3, desp);

pstmt.setDate(4, new Date(System.currentTimeMillis()));

int i = pstmt.executeUpdate();

if(i > 0) {

response.sendRedirect(request.getContextPath()+"/showCourseServlet");

}else {

response.sendRedirect(request.getContextPath()+"/showCourseServlet");

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

JDBCUtils.release(pstmt, conn);

}

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

doGet(request, response);

}


}

<%@ 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>

<style type="text/css">

.input{

margin-left:70px;

}

</style>


</head>

<body>

<form action="${pageContext.request.contextPath}/addCourseServlet">

<div style="text-align:center;line-height:30px">

<span style="font-weight:bold">课程添加</span><br>

<span style="width:70px;display:inline-block" name="name">课程名</span>

<input type="text" ><br>

<span style="width:70px;display:inline-block" name="category">所属方向</span>

<input type="text" ><br>

<span style="width:70px;display:inline-block" name="desp">课程描述</span>

<input type="text" ><br>

<div style="margin-left:50px">

<input type="submit" value=提交>

</div>

</div>

</form>

</body>

</html>


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

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

5回答
提问者 慕的地2082093 2020-03-13 19:17:10
  • 同学你好,这个报错是说你在使用rs之前没有调用next()方法,老师从代码看,同学是进行调用了的,同学可以把代码复制出来,然后再粘回去保存后,重新运行试试,可以选择project-clean,重新编译一下试试。如果还是报错,请同学检查一下,是否其他地方在没有调用结果集的next()方法就去get了呢? 请同学检查一下。祝学习愉快。
    2020-03-13 19:41:14
  • 提问者 慕的地2082093 回复 好帮手慕阿莹 #2
    老师,使用project-clean,清除后,原来移动到jar包都放到WEB-INF下的lib的文件全丢失了,重新add to build path后,运行就提示这些文件找不到,这个需要怎么弄?
    2020-03-13 20:30:15
  • 好帮手慕阿莹 回复 提问者 慕的地2082093 #3
    同学你好,同学遇到的这个问题比较奇怪,重新编译应该不会清除掉jar包,同学还有那个jar包么?把所有的jar包都拿出来,并全部重新放一下试试呢?如果还是不行,新建一下项目把代码贴进去试试呢?
    2020-03-14 09:36:54
好帮手慕阿莹 2020-03-13 19:07:06

同学你好

报错是提示你,插入的name字段不能为null,也就是说,没有获取到name这个参数

同学前段代码写错了,这个name属性要写到要提交的input表单中,作为参数名称哦

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

如果我的回答解决了你的问题,请采纳,祝学习愉快.

提问者 慕的地2082093 2020-03-13 18:46:50

老师把jar包都放到WEB-INF下的lib后:Column 'name' cannot be null

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

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


好帮手慕阿莹 2020-03-13 18:01:33

同学你好

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

同学把这些jar包都放到WEB-INF下的lib中试试呢?

如果我的回答解决了你的问题,请采纳,祝学习愉快.

提问者 慕的地2082093 2020-03-13 16:35:06
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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