老师 帮忙看下代码

老师 帮忙看下代码

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

正在回答

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

2回答

同学你好,老师这里测试你的代码是没有问题的,建议你将你的报错信息再贴详细一点。老师再看一下。

可以clear一下控制台

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

然后点击添加,然后将控制台的信息都贴一下。

注意:你这里使用的是转发,再跳转时地址栏的地址是不会改变的

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

祝学习愉快~

好帮手慕小脸 2020-03-28 20:09:05

同学你好,这里说的“跳转不了到 showCourse.jsp?”是如何进行的跳转呢?

老师这边是进入的同学的jsp页面,然后添加表字段。是可以正常添加成功的。

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

如果我的回答解决了你的疑惑,请采纳,祝学习愉快~

  • 提问者 慕仰0394571 #1
    我写的AddCourseServlet 中是添加数据成功后,跳转到showCourse.jsp 显示数据的 if(i>0) { request.getRequestDispatcher("/showCourse.jsp").forward(request, response); } 程序没有跳转到showCourse.jsp 页面显示,控制台输出了 “保存成功”我的代码都没有这四个字,为何控制台会显示 “保存成功”奇怪了
    2020-03-28 20:50:38
  • 提问者 慕仰0394571 #2
    我另外采用另外新建工程的办法,运行程序后,网页提示 Internal server error; org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:609) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1623) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 一样的代码,出现不同的错误信息提示
    2020-03-28 22:16:13
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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