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
老师把jar包都放到WEB-INF下的lib后:Column 'name' cannot be null
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星