《Java程序设计》实验报告(六)之在JSP中操作数据库

实验内容及步骤:

  1. 对试题表通过DAO类来操作数据库代码:
  1. 代码:

package lyx.study.jsp.dao;

import lyx.study.jsp.entity.Course;

import java.sql.*;

import java.util.ArrayList;

import java.util.List;

public class CourseDao

{

    private Connection conn ;

    private PreparedStatement pstmt;

    private ResultSet rs;

    public List selectAll()

    {

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/mysql" +

                         "?serverTimezone=UTC&useUnicode" +

                          "=true&characterEncoding=utf8&useSSL=true",

                    "root",

                    "root");

            String sql = "select * from course";

            pstmt = conn.prepareStatement(sql);

            //执行查询用executeQuery()

            rs = pstmt.executeQuery();

            Course course = null;

            List courseList = new ArrayList<>();

            while(rs.next())

            {

实验内容及步骤:                

course = new Course(

                        rs.getInt(1),

                        rs.getString(2),

                        rs.getString(3),

                        rs.getDouble(4),

                        rs.getString(5),

                        rs.getString(6)

                );

                courseList.add(course);

            }

            return courseList;

        }catch (ClassNotFoundException cfe)

        {

            cfe.printStackTrace();

            System.out.println("数据库驱动加载失败!");

        }catch (SQLException sqle)

        {

            sqle.printStackTrace();

            System.out.println("获取数据库链接失败!");

        }

        return null;

    }

    public Integer insertCourse(Course course)

    {

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection(

                    "jdbc:mysql://localhost:3306/mysql",

                    "root",

                    "root");

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

            pstmt = conn.prepareStatement(sql);

            //执行查询用executeQuery()

            pstmt.setInt(1,course.getCid());

            pstmt.setString(2,course.getCno());

            pstmt.setString(3,course.getCname());

            pstmt.setDouble(4,course.getCscore());

            pstmt.setString(5, course.getCtno());

            pstmt.setString(6,course.getCtype());

实验内容及步骤: 

            int count =  pstmt.executeUpdate();

            return count;

        }catch (ClassNotFoundException cfe)

        {

            cfe.printStackTrace();

            System.out.println("数据库驱动加载失败!");

        }catch (SQLException sqle)

        {

            sqle.printStackTrace();

            System.out.println("获取数据库链接失败!");

        }

        return 0;

    }

}

package lyx.study.jsp.entity;

import java.util.Objects;

public class Course

{

    private Integer cid;

    private String cno;

    private String cname;

    private Double cscore;

    private String ctno;

    private String ctype;

    public Course()

    {

    }

    public Course(Integer cid, String cno, String cname, Double cscore, String ctno, String ctype)

    {

        this.cid = cid;

        this.cno = cno;

        this.cname = cname;

        this.cscore = cscore;

        this.ctno = ctno;

        this.ctype = ctype;

    }

    public Integer getCid()

    {

        return cid;

实验内容及步骤: 

    }

    public void setCid(Integer cid)

    {

        this.cid = cid;

    }

    public String getCno()

    {

        return cno;

    }

    public void setCno(String cno)

    {

        this.cno = cno;

    }

    public String getCname()

    {

        return cname;

    }

    public void setCname(String cname)

    {

        this.cname = cname;

    }

    public Double getCscore()

    {

        return cscore;

    }

    public void setCscore(Double cscore)

    {

        this.cscore = cscore;

    }

    public String getCtno()

    {

        return ctno;

    }

    public void setCtno(String ctno)

    {

        this.ctno = ctno;

    }

    public String getCtype()

    {

        return ctype;

    }

    public void setCtype(String ctype)

实验内容及步骤: 

    {

        this.ctype = ctype;

    }

    @Override

    public boolean equals(Object o) {

        if (this == o) return true;

        if (!(o instanceof Course)) return false;

        Course course = (Course) o;

        return Objects.equals(cid, course.cid) &&

                Objects.equals(cno, course.cno) &&

                Objects.equals(cname, course.cname) &&

                Objects.equals(cscore, course.cscore) &&

                Objects.equals(ctno, course.ctno) &&

                Objects.equals(ctype, course.ctype);

    }

    @Override

    public int hashCode()

    {

        return Objects.hash(cid, cno, cname, cscore, ctno, ctype);

    }

}

package lyx.study.jsp.service;

import lyx.study.jsp.dao.CourseDao;

import lyx.study.jsp.entity.Course;

import java.util.List;

public class CourseService

{

    private CourseDao courseDao;

    public CourseService()

    {

        courseDao = new CourseDao();

    }

    //处理查询所有课程的业务

    public List allCourse()

    {

        return courseDao.selectAll();

    }

    public Integer addCourse(Course course)

    {

实验内容及步骤: 

        return courseDao.insertCourse(course);

    }

}

package lyx.study.jsp.Servlet;

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 java.io.IOException;

@WebServlet("/addCourse")

public class AddCourseServlet extends HttpServlet

{

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

    {

        resp.sendRedirect(req.getContextPath() + "/addCourse.jsp");

    }

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

    {

        doGet(req,resp);

    }

}

package lyx.study.jsp.Servlet;

import lyx.study.jsp.entity.Course;

import lyx.study.jsp.service.CourseService;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.util.List;

public class AllCourseServlet extends HttpServlet

实验内容及步骤: 

{

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");

        CourseService courseService = new CourseService();

        List courseList = courseService.allCourse();

        //用的请求作用域:在一次请求内,数据可以用请求域(request)进行流转

        req.setAttribute("courseList",courseList);

     req.getRequestDispatcher("allCourse.jsp").forward(req,resp);

    }

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException

    {

        doGet(req,resp);

    }

}

package lyx.study.jsp.Servlet;

import lyx.study.jsp.entity.Course;

import lyx.study.jsp.service.CourseService;

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 java.io.IOException;

@WebServlet("/saveCourse")

public class SaveCourseServlet extends HttpServlet

{

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)

            throws ServletException, IOException

    {

        String cno = req.getParameter("cno");

实验内容及步骤: 

        String cname = req.getParameter("cname");

        String cscore = req.getParameter("cscore");

        String ctno = req.getParameter("ctno");

        String ctype = req.getParameter("ctype");

        Course course = new Course(

                0 ,

                cno ,

                cname,

                Double.parseDouble(cscore),

                ctno,

                ctype);

        CourseService courseService = new CourseService();

        Integer i = courseService.addCourse(course);

        if (i > 0){

            System.out.println("添加成功!");

        }

    }

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)

            throws ServletException, IOException

    {

        doGet(req,resp);

    }

}

         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

         version="4.0">

   

        allCourse

        lyx.study.jsp.Servlet.AllCourseServlet

   

实验内容及步骤: 

   

        allCourse

        /allCourse

   

  Title

添加Course信息!

method="get">

  课程编号:

  课程名称:

  课程学分:

  课程教师编号:

  课程类型:

 

 

    Title

添加课程

<%

    List courseList = (List)request.getAttribute("courseList");

    if (courseList != null){

%>

   

   

实验内容及步骤: 

       

       

       

a

       

       

       

   

   

   

    <%

        for(Course course:courseList){

    %>

   

       

       

       

       

       

   

    <%

            }

        }

    %>

   

课程id 课程编号 课程名称 课程学分 课程教师编号 课程类型
<%=course.getCid()%> <%=course.getCno()%> <%=course.getCname()%> <%=course.getCscore()%> <%=course.getCtno()%> <%=course.getCtype()%>

  1. 运行结果:《Java程序设计》实验报告(六)之在JSP中操作数据库_第1张图片

实验内容及步骤:《Java程序设计》实验报告(六)之在JSP中操作数据库_第2张图片

你的问题:

  1. 遇到SQL语句错误、连接池配置错误等问题。
  2. 定义 DAO 接口时,出现实现类与接口的方法定义不一致的问题

你可能感兴趣的:(实验报告,java,开发语言)