• 6月12日学习日志


    今天完成了web实验三-数据库程序设计。

    index.jsp文件代码

    <%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
    <%@ page errorPage="error.jsp"%>
    <html>
    <head>
    <title>学生管理系统</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <h1>学生管理系统</h1>
        <a href="add.jsp">添加学生信息</a>
        <br />
        <br />
        <table style=" 50%;">
            <tr>
                <th>学号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>出生年月</th>
                <th>住址</th>
                <th>操作</th>
            </tr>
            <%
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "123456");
                    //使用Statement对象
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("select * from studentinfo");
    
                    /*
                    PreparedStatement stmt = con.prepareStatement("select * from student");
                    ResultSet rs = stmt.executeQuery();
                    */
                    while (rs.next()) {
                        int id = rs.getInt(1);
                        out.println("<tr><td>" + rs.getString(1) + "</td><td>" + rs.getString(2) + "</td><td>"
                                + rs.getString(3)+ "</td><td>" + rs.getString(4)+"</td><td>" + rs.getString(5) +"</td><td><a href='edit.jsp?id=" + id
                                + "'>修改</a>&nbsp;<a href='del.jsp?id=" + id + "'>删除</a></td></tr>");
                    }
                    rs.close();
                    stmt.close();
                    con.close();
                } catch (Exception e) {
                    out.println("Exception:" + e.getMessage());
                }
            %>
            
        </table>
        <br />
        <hr />
        <div style="text-align: center;  100%; font-size: 12px; color: #333;">
            &copy;版权所有:石家庄铁道大学信息科学与技术学院&nbsp;&nbsp;<a href="Lab03.png" target="_blank">网站地图</a>
        </div>
    </body>
    </html>

    error.jsp文件代码

    <%@ page language="java" isErrorPage="true" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>error page</title>
    </head>
    <body>
        错误信息为:<br/>
        <%=exception.getMessage()%><br>
        <%=exception.toString()%>
    </body>
    </html>
    <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>
    <html>
    <head>
    <title>添加学生信息</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <script type="text/javascript">
        function checkform(form)
          {
        if(form.id.value=="")
            {
                alert("学号不能为空");
                form.name.focus();
                return false;
            }
        if(form.name.value=="")
        {
            alert("姓名不能为空");
            form.name.focus();
            return false;
        }
        if(form.sex.value=="")
        {
            alert("性别不能为空");
            form.name.focus();
            return false;
        }
        if(form.birth.value=="")
        {
            alert("出生年月不能为空");
            form.name.focus();
            return false;
        }
        if(form.address.value=="")
        {
            alert("住址不能为空");
            form.name.focus();
            return false;
        }
        return true;
    } 
        </script> 
        <form action="addsave.jsp" method="post" onsubmit="return checkform(this);">
            <h2>添加学生信息</h2>
            <table style=" 50%">
                <tr>
                    <th width="30%">学号:</th>
                    <td width="70%"><input name="id" type="text"></td>
                </tr>
                <tr>
                    <th>姓名:</th>
                    <td><input name="name" type="text"></td>
                </tr>
                <tr>
                    <th>性别:</th>
                    <td><input name="sex" type="text"></td>
                </tr>
                <tr>
                    <th>出生年月:</th>
                    <td><input name="birth" type="text"></td>
                </tr>
                <tr>
                    <th>住址:</th>
                    <td><input name="address" type="text"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" name="submit" value="添加"> <input type="reset" value="重置"></td>
                </tr>
            </table>
        </form>
        
    </body>
    </html>
    <%@ page contentType="text/html; charset=utf-8" import="java.sql.*" errorPage="error.jsp"%>
    <html>
    <head>
    <title>添加学生信息</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <%
            request.setCharacterEncoding("utf-8");
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            String birth = request.getParameter("birth");
            String address = request.getParameter("address");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "123456");
    
            
            
            PreparedStatement stmt = con.prepareStatement("insert into studentinfo(id,name,sex,birth,address) values(?,?,?,?,?)");
            stmt.setString(1, id);
            stmt.setString(2, name);
            stmt.setString(3, sex);
            stmt.setString(4, birth);
            stmt.setString(5, address);
            int i = stmt.executeUpdate();
            
            
            if (i == 1) {
                out.println("<h2>添加成功!</h2><br/>");
                out.println("<a href='index.jsp'>返回首页</a>");
            } else {
                out.println("<h2>添加失败!</h2><br/>");
                out.println("<a href='add.jsp'>重新添加</a>");
            }
            stmt.close();
            con.close();
            
        %>
    </body>
    </html>
    <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" pageEncoding="utf-8"%>
    <html>
    <head>
    <title>删除学生信息</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <%
        request.setCharacterEncoding("utf-8");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "123456");
        Statement stmt=con.createStatement();
        String id=request.getParameter("id");
        int i=stmt.executeUpdate("delete from studentinfo where id="+id);
        if(i==1)
        {
            out.println("<h2>删除成功!</h2><br/>");
        }
            else
        {
            out.println("<h2>删除失败!</h2><br/>");
        }
        out.println("<a href='index.jsp'>返回首页</a>");
        stmt.close();
        con.close();
    
        %>
    </body>
    </html>
    <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
    <html>
    <head>
    <title>修改学生信息</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <%
            request.setCharacterEncoding("utf-8");
            String id = request.getParameter("id");    
            Class.forName("com.mysql.jdbc.Driver");
            
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "123456");
            //Statement stmt = con.createStatement();
            //ResultSet rs = stmt.executeQuery("select * from student where id=" + id);
            
            PreparedStatement stmt = con.prepareStatement("select * from studentinfo where id=?");
            stmt.setString(1, id);
            ResultSet rs = stmt.executeQuery();
            
            rs.next();
        %>
        <form action="editsave.jsp" method="post">
            <h2>修改学生信息</h2>
            <table style="50%">
                <tr>
                    <th width="30%">学号:</th>
                    <td width="70%"><input name="id" type="text"
                        value="<%=rs.getString(1)%>"></td>
                </tr>
                <tr>
                    <th>姓名:</th>
                    <td><input name="name" type="text"
                        value="<%=rs.getString(2)%>"></td>
                </tr>
                <tr>
                    <th>性别:</th>
                    <td><input name="sex" type="text"
                        value="<%=rs.getString(3)%>"></td>
                </tr>
                <tr>
                    <th>出生年月:</th>
                    <td><input name="birth" type="text"
                        value="<%=rs.getString(4)%>"></td>
                </tr>
                <tr>
                    <th>住址:</th>
                    <td><input name="address" type="text"
                        value="<%=rs.getString(5)%>"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="hidden" name="id" value="<%=id%>">
                        <input type="submit" value="修改"> <input type="reset"
                        value="重置"></td>
                </tr>
            </table>
        </form>
        <%
            rs.close();
            stmt.close();
            con.close();
        %>
    </body>
    </html>
    <%@ page import="java.sql.*" pageEncoding="utf-8" errorPage="error.jsp"%>
    <html>
    <head>
    <title>修改完成</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <%
            request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            String sex = request.getParameter("sex");
            String birth = request.getParameter("birth");
            String address = request.getParameter("address");
            String id = request.getParameter("id");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf-8", "root", "123456");
            Statement stmt = con.createStatement();
            String sql = "update studentinfo set name='" + name + "',sex='"+ sex + "',birth='" + birth + "',address='" + address
                    + "' where id=" + id;
            int i = stmt.executeUpdate(sql);
            if (i == 1) {
                out.println("<h2>修改成功!</h2><br/>");
                out.println("<a href='index.jsp'>返回首页</a>");
            } else {
                out.println("<h2>修改失败!</h2><br/>");
                out.println("<a href='edit.jsp?id='" + id + ">重新添加</a>");
            }
            stmt.close();
            con.close();
        %>
    </body>
    </html>
  • 相关阅读:
    收藏一些常用的methods
    判断DOM是否全屏及相关API
    原生post 协议提交文件流 至OSS服务器
    vue 嵌套路由,router-link-active的问题
    关于JAVA变量,数据类型,基本数据类型之间的转换和对象型的基本数据类型
    关于循环语法
    java环境变量的设置
    java的运行机制, JVM
    2-ser2003系统封装实验报告
    1-ser2008系统封装实验报告
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14910790.html
Copyright © 2020-2023  润新知