老师 帮忙看下代码
package com.imooc.jdbc.model;
import java.util.Date;
public class Course {
private int id;
private String courseName;
private String direction;
private String desp;
private Date date;
//构造方法
public Course() {}
public Course(int id, String courseName, String direction, String desp, Date date) {
super();
this.id = id;
this.courseName = courseName;
this.direction = direction;
this.desp = desp;
this.date = date;
}
public Course(String courseName, String direction, String desp, Date date) {
super();
this.courseName = courseName;
this.direction = direction;
this.desp = desp;
this.date = date;
}
public Course(String courseName, String direction, String desp) {
super();
this.courseName = courseName;
this.direction = direction;
this.desp = desp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
public String getDesp() {
return desp;
}
public void setDesp(String desp) {
this.desp = desp;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package com.imooc.jdbc.servelet;
import java.io.IOException;
import java.util.ArrayList;
import java.sql.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 com.imooc.jdbc.model.Course;
import com.imooc.jdbc.service.CourseImpl;
/**
* 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
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// List<Course> list=new ArrayList<Course>();
String courseName=request.getParameter("courseName");
String direction=request.getParameter("direction");
String desp=request.getParameter("desp");
Course course=new Course(courseName,direction,desp);
CourseImpl courseImpl=new CourseImpl();
int i=courseImpl.addCourse(course);
if(i>0) {
request.getRequestDispatcher("/showCourse.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
package com.imooc.jdbc.service;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.imooc.jdbc.model.Course;
import com.imooc.jdbc.utils.CourseUtils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class CourseImpl implements CourseSevice {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ComboPooledDataSource dataSource = new ComboPooledDataSource();
List<Course> courseList=new ArrayList<Course>();
@Override
public int addCourse(Course course) {
int i = 0;
try {
conn = dataSource.getConnection();
String sql = "insert into course(id,name,category,desp,createTime) Values(null,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, course.getCourseName());
pstmt.setString(2, course.getDirection());
pstmt.setString(3, course.getDesp());
pstmt.setDate(4, new Date(System.currentTimeMillis()));
i = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
CourseUtils.release(conn, pstmt);
}
return i;
}
public List<Course> showCourse() {
Course course=new Course();
try {
conn = dataSource.getConnection();
String sql = "select * from course";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while(rs.next()!=false) {
//course设定值
course.setId(rs.getInt("id"));
course.setCourseName(rs.getString("name"));
course.setDirection(rs.getString("category"));
course.setDesp(rs.getString("desp"));
course.setDate(rs.getDate("createTime"));
//装入courseList
courseList.add(course);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
CourseUtils.release(rs, conn, pstmt);
}
return courseList;
}
}
package com.imooc.jdbc.service;
import java.sql.ResultSet;
import java.util.List;
import com.imooc.jdbc.model.Course;
public interface CourseSevice {
//添加数据
public int addCourse(Course course);
//查询显示数据
public List<Course> showCourse();
}
package com.imooc.jdbc.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
* 设置工具包
* @author Administrator
*
*/
public class CourseUtils {
private static final String driverClass;
private static final String url;
private static final String username;
private static final String password;
static {
Properties props=new Properties();
InputStream is=CourseUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
try {
props.load(is);
}catch(IOException e){
e.printStackTrace();
}
driverClass=props.getProperty("driverClass");
url=props.getProperty("url");
username=props.getProperty("username");
password=props.getProperty("password");
}
public static void loadDriver() throws ClassNotFoundException {
Class.forName(driverClass);
}
public static Connection getConnetion() throws Exception {
loadDriver();
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public static void release(Connection conn, Statement stmt) {
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,Connection conn, Statement stmt) {
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;
}
}
}
<%@ 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="${pageContext.request.contextPath }/AddCourseServlet" method="post">
<div class="courseName" id="courseName" >
<label>课程名</label> <input type="text" id="courseName" name="courseName"></input>
</div>
<div class="direction" id="direction" >
<label>所属方向 </label><input type="text" id="direction" name="direction"></input>
</div>
<div class="desp" id="desp" >
<label>课程描述</label><input type="text" id="desp" name="desp"></input>
</div>
<div><input type="submit" id="btn" name="btn" value="添加"></div>
</form>
</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>
保存成功!
</body>
</html>
跳转不了到 showCourse.jsp
正在回答
同学你好,老师这里测试你的代码是没有问题的,建议你将你的报错信息再贴详细一点。老师再看一下。
可以clear一下控制台
然后点击添加,然后将控制台的信息都贴一下。
注意:你这里使用的是转发,再跳转时地址栏的地址是不会改变的
祝学习愉快~
- 参与学习 人
- 提交作业 357 份
- 解答问题 8016 个
本阶段将带你学习MySQL数据库,JDBC接口,MyBatis框架等,带你掌握的数据的存放和管理。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星