201819101023 姚雪丽(作业四)

本文介绍了如何实现学生登录界面(login.jsp)的代码,以及后续对用户信息进行添加(add.jsp)、删除(delete.jsp)、修改(update.jsp)和查询(edit.jsp)的数据库操作。通过实例展示了前后端交互和基本的CRUD操作过程。
摘要由CSDN通过智能技术生成

1、学生登陆页面    login.jsp代码如下:

<html>
<head>
    <title>登陆页面</title>
    <link rel="stylesheet" href="css/bootstrap.css">

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/bootstrap.bundle.js"></script>

    <style>
        .container{
            width:30%;
            height:20%;
            margin-top:10%;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <p align="center">学生登录</p>
            <form role="form" method="post" action="index.jsp">
                <div class="form-group">
                    <label>用户名:</label><input class="form-control" name="username" type="text" />
                </div>
                <div class="form-group">
                    <label>密码:</label><input class="form-control" name="password" type="password" />
                </div>
                <button class="btn btn-default" type="submit">提交</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

运行结果截图:

2、对学生信息进行增删改查

add.jsp代码如下(增):

<jsp:useBean id="user" class="com.media.bean.UserBean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>

<%
    Class.forName("com.mysql.jdbc.Driver");

    String url ="jdbc:mysql://localhost:3306/yxl";

    Connection connection = DriverManager.getConnection(url,"root","root");

    connection.setAutoCommit(false);

    String sql = "insert into user (username,password.gender.age) values (?,?,?,?)";
    PreparedStatement ps = connection.prepareStatement(sql);

    ps.setString(1,user.getUsername());
    ps.setString(2,user.getPassword());
    ps.setInt(3,user.getGender());
    ps.setInt(4,user.getAge());
    ps.setInt(5,0);

    ps.addBatch();

    ps.setString(1,user.getUsername());
    ps.setString(2,user.getPassword());
    ps.setInt(3,user.getGender());
    ps.setInt(4,user.getAge());
    ps.setInt(5,0);

    ps.addBatch();

    int[] count = {0};

    try {
        count=ps.executeBatch();
        connection.commit();
    }catch (SQLException e){
        connection.rollback();
        e.printStackTrace();
    }

    if(count[0] > 0){
    out.print("添加成功");
    }else{
    out.print("添加失败");
    }

    response.setHeader("refresh","3;url=home.jsp");
%>

运行结果截图:

delete.jsp代码如下(删):

<html>
<%
    Class.forName("com.mysql.jdbc.Driver");

    String url="jdbc:mysql://localhost:3306/yxl";
    Connection connection= null;
    connection = DriverManager.getConnection(url,"root","root");

    String sql="delete from user where id=?";
    PreparedStatement ps=connection.prepareStatement(sql);

    int id=Integer.parseInt(request.getParameter("id"));

    ps.setInt(1,id);

    int count=ps.executeUpdate();

    if(count>0){
        out.print("删除成功");
    }else{
        out.print("删除失败");
    }
%>
</html>

运行结果截图:

update.jsp代码如下(改):

<jsp:useBean id="user" class="com.media.bean.UserBean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");

String url="jdbc:mysql://localhost:3306/yxl";

Connection connection= DriverManager.getConnection(url,"root","root");

String sql="update user set username=?,password=?,gender=?,age=? where id=? ";

PreparedStatement ps=connection.prepareStatement(sql);

String ids = (String) request.getParameter("id");

    ps.setString(1,request.getParameter("username"));
    ps.setString(2,request.getParameter("password"));
    ps.setInt(3,Integer.parseInt(request.getParameter("gender")));
    ps.setInt(4,Integer.parseInt(request.getParameter("age")));
    ps.setInt(5,(Integer) request.getSession().getAttribute("id"));

   int count=ps.executeUpdate();

    if(count>0){
        out.print("修改成功");
    }else{
        out.print("修改失败");
    }
    response.setHeader("refresh","3;url=home.jsp");
%>

运行结果截图:

edit.jsp代码如下(查):

<html>
<head>
    <title>201819101023</title>
    <link rel="stylesheet" href="css/bootstrap.css">

    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.js"></script>
    <script src="js/bootstrap.bundle.js"></script>

    <style>
        .container{
            width: 50%;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <form method="post" action="update.jsp">
                <h3 style="margin-top:20px;" align="center">学生编辑</h3>

                <%
                    String idStr = request.getParameter("id");
                    int id = Integer.parseInt(idStr);

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

                    String url ="jdbc:mysql://localhost:3306/yxl";

                    Connection connection = DriverManager.getConnection(url,"root","root");
                    String sql = "select * from user where id = ?";
                    PreparedStatement ps = connection.prepareStatement(sql);

                    ps.setInt(1, id);

                    ResultSet rs = ps.executeQuery();

                    while (rs.next()){
                        request.getSession().setAttribute("id",id);
                        out.print("<input type='hidden' name='id' values='"+id+"'>");
                        out.print("用户名:<input type='text' class='form-control' name='username' values="+rs.getString("username")+"><br>");
                        out.print("密码:<input type ='text' name='password' class=\"form-control\" value="+rs.getString("password")+"><br>");
                        out.print("性别:<input type ='text' name='gender' class=\"form-control\" value="+rs.getString("gender")+"><br>");
                        out.print("年龄:<input type ='text' name='age' class=\"form-control\" value="+rs.getString("age")+"><br>");
                    }
                %>
                <button type="submit" style="width: 100%" class="btn btn-info">提交</button>
            </form>
        </div>
    </div>
</div>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值