JSP存取数据库

实验目的

掌握服务器端技术Servlet,理解Servlet的生命周期;掌握Jsp的实质,认识out、request、response、session、application等JSP内置对象,灵活应用这些内置对象进行实际与数据库相连的项目开发。

实验内容

使用JSP+JDBC完成一个用户登录的程序,然后实现对一张表的添加、删除、查找和修

改操作。

实验步骤

建立多个jsp实现登录界面,数据增删改查的界面:

代码实现

增加操作:

private  void add(HttpServletRequest request,HttpServletResponse response){
    try {
        Connection con = DBHelper.getConnection();
        String sql ="insert into user values(null,?,?,?)";
        PreparedStatement pstm =con.prepareStatement(sql);
        pstm.setString(1,request.getParameter("account"));
        pstm.setString(2,request.getParameter("balance"));
        pstm.setString(3,request.getParameter("password"));
        int rs =pstm.executeUpdate();
        PrintWriter out = response.getWriter();
        String url =request.getContextPath()+"/main.jsp";

        if(rs>0)
        {
            out.print("<script language='javascript'>alert('insert success!!!');window.location.href='"
                    + url + "';</script>");
        }
        else {
            out.print("<script language='javascript'>alert('insert fall!!!');window.location.href='"
                    + url + "';</script>");
        }
        DBHelper.closeResources(null,pstm,con);

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

删除操作:

private void delete(HttpServletResponse response,HttpServletRequest request){
    Connection con = null;
    try {
        con = DBHelper.getConnection();
        String sql ="delete from user where id =?";
        PreparedStatement pstm =con.prepareStatement(sql);
        pstm.setString(1,request.getParameter("id"));
        int rs =pstm.executeUpdate();
        PrintWriter out = response.getWriter();
        String url =request.getContextPath()+"/main.jsp";
        if(rs>0)
        {
            out.print("<script language='javascript'>alert('delete success!!!');window.location.href='"
                    + url + "';</script>");
        }
        else {
            out.print("<script language='javascript'>alert('insert fall!!!');window.location.href='"
                    + url + "';</script>");
        }
        DBHelper.closeResources(null,pstm,con);



    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

修改操作:

private void alter(HttpServletRequest request,HttpServletResponse response){
    Connection con = null;
    try {
        con = DBHelper.getConnection();
        String sql ="update user set account=?,balance=?,password=? where id=?;" ;
        PreparedStatement pstm = con.prepareStatement(sql);
        pstm.setString(1,request.getParameter("account"));
        pstm.setString(2,request.getParameter("balance"));
        pstm.setString(3,request.getParameter("password"));
        pstm.setString(4,request.getParameter("id"));

        int rs =pstm.executeUpdate();
        PrintWriter out =response.getWriter();
        String url =request.getContextPath()+"/main.jsp";
        if(rs>0)
        {
            out.print("<script language='javascript'>alert('update success!!!');window.location.href='"
                    + url + "';</script>");
        }
        else {
            out.print("<script language='javascript'>alert('update fall!!!');window.location.href='"
                    + url + "';</script>");
        }
        DBHelper.closeResources(null,pstm,con);

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }


}

查询操作:

private void show(HttpServletRequest request,HttpServletResponse response){
    Connection con = null;
    try {
        con = DBHelper.getConnection();
        String sql ="select * from user where id=?";
        PreparedStatement pstm=con.prepareStatement(sql);
        pstm.setString(1,request.getParameter("id"));
        System.out.println("1");
        ResultSet rs =pstm.executeQuery();
        PrintWriter out =response.getWriter();
        String url =request.getContextPath()+"/main.jsp";
        if(rs.next())
        {
            System.out.println("2");
            User user = new User(rs.getInt(1),rs.getString(2),
                    rs.getDouble(3),rs.getString(4));
            HttpSession session =request.getSession();
            session.setAttribute("user",user);
            System.out.println("3");
            request.getRequestDispatcher("/result.jsp").forward(request,response);
        }
        else {
            out.print("<script language='javascript'>alert('select fall!!!');window.location.href='"
                    + url + "';</script>");
        }

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }


}

运行结果

         登录界面:

登录成功:

执行增加操作:

修改操作:

删除操作:

查看操作:

总结

        在本次实验中,我使用JSP和JDBC技术完成了一个用户登录程序,并实现了对一张表的添加、删除、查找和修改操作。通过这个实验,我深入了解了JSP和JDBC的工作原理和使用方法。

        首先,我创建了一个用户登录页面,包括用户名和密码的输入框以及登录按钮。当用户点击登录按钮时,JSP页面会将用户输入的用户名和密码传递给后台处理。在后台代码中,我首先通过JDBC连接数据库,并根据用户输入的用户名和密码查询数据库中是否存在匹配的用户。如果存在,则登录成功,跳转到另一个页面;如果不存在,则登录失败,返回登录页面并显示错误信息。接下来,我实现了对一张表的添加、删除、查找和修改操作。通过JDBC,我可们以连接到数据库,并执行SQL语句来操作数据库表。例如,要添加一条记录,我们可以通过JSP页面接收用户输入的数据,并将其传递给后台处理。后台代码中,我们使用JDBC执行INSERT语句将数据插入到数据库表中。类似地,我们可以使用DELETE语句删除表中的记录,使用SELECT语句查询表中的记录,使用UPDATE语句修改表中的记录。通过JDBC,我们可以方便地与数据库进行交互,并对表进行各种操作。

        在实验过程中,我遇到了一些问题,比如数据库连接失败、SQL语句执行错误等。通过查阅文档和调试代码,我们逐步解决了这些问题,并成功完成了实验。通过本次实验,我们深入理解了JSP和JDBC的使用方法,掌握了用户登录程序和对表的增删查改操作。这些知识和技能对于Web开发和数据库操作都非常重要,为我们今后的学习和工作奠定了基础。同时,我也意识到了数据安全性的重要性,需要注意对用户输入进行合法性验证和防范SQL注入等攻击。

        总之,本次实验让我学习到了很多有用的知识,提高了我的编程能力和问题解决能力。希望在以后的学习和实践中能够继续深入研究和应用这些知识,不断提升自己的技术水平。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值