8-2 自由编程
来源:8-2 自由编程
慕的地2082093
2020-03-14 11:26:04
package com.imooc.jdbc.domain;
import java.sql.Date;
public class Course {
private int id;
private String name;
private String category;
private String desp;
private Date createTime;
public Course() {
super();
// TODO Auto-generated constructor stub
}
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 createTime) {
this.createTime = createTime;
}
}
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");
System.out.println(name+" "+category+" "+desp+"hhhhhhhhhhhhhh");
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCUtils.getConnection();
String sql= "insert into course(id,NAME,category,desp,createTime) 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()));
System.out.println(sql);
int i = pstmt.executeUpdate();
if(i > 0) {
request.setAttribute("msg", "添加成功");
request.getRequestDispatcher("showCourseServlet").forward(request, response);
//response.sendRedirect(request.getContextPath()+"/showCourseServlet");
}else {
request.setAttribute("msg", "添加失败");
request.getRequestDispatcher("fail.jsp").forward(request, response);
//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);
}
}
package com.imooc.jdbc.utils;
import java.sql.Connection;
import java.sql.ResultSet;
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>
<%@ 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" >课程名</span>
<input type="text" name="name"><br>
<span style="width:70px;display:inline-block" >所属方向</span>
<input type="text" name="category"><br>
<span style="width:70px;display:inline-block" >课程描述</span>
<input type="text" name="desp"><br>
<div style="margin-left:50px">
<input type="submit" value=提交>
</div>
</div>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
p{
font-weight:bold;
font-size:30px;
}
</style>
</head>
<body>
<p>${msg}</p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {
margin: auto;
border: 1px solid gray;
}
p{
margin-left:-210px;
font-weight:bold;
font-size:30px;
}
td, th {
border: 1px solid gray;
}
</style>
</head>
<body style="text-align: center">
<form action="">
<div>
<p>${msg}</p>
<table>
<tr>
<th>课程名称</th>
<th>所属方向</th>
<th>课程描述</th>
<th>创建时间</th>
</tr>
<c:forEach items="${courses}" var="course" varStatus="idx">
<tr>
<th>${course.name}</th>
<th>${course.category}</th>
<th>${course.desp}</th>
<th>${course.createTime}</th>
</tr>
</c:forEach>
</table>
</div>
</form>
</body>
</html>
CREATE TABLE course(
id INT AUTO_INCREMENT KEY,
NAME VARCHAR(20) NOT NULL,
category VARCHAR(20) NOT NULL,
desp VARCHAR(30) NOT NULL,
createTime DATETIME NOT NULL
)CHARSET=UTF8;
SELECT * FROM course
1回答
好帮手慕阿莹
2020-03-14
1、同学你好,同学好像忘记贴这个类了,
showCourseServlet
2、类名首字母应大写,所以这里建议addCourseServlet可以改为AddCourseServlet
其他的同学写的不错哦。继续加油!!
如果我的回答解决了你的问题,请采纳,祝学习愉快.
相似问题