• Java Web学生信息保存


    Course.java
    package
    entity; public class Course { private int id; private String num; private String mima; private String sex; private String name; private String studentnum; private String youxiang; private String xueyuan; private String xi; private String banji; private String year; private String plase; private String beizhu; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getMima() { return mima; } public void setMima(String mima) { this.mima = mima; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getStudentnum() { return studentnum; } public void setStudentnum(String studentnum) { this.studentnum = studentnum; } public String getYouxiang() { return youxiang; } public void setYouxiang(String youxiang) { this.youxiang = youxiang; } public String getXueyuan() { return xueyuan; } public void setXueyuan(String xueyuan) { this.xueyuan = xueyuan; } public String getXi() { return xi; } public void setXi(String xi) { this.xi = xi; } public String getBanji() { return banji; } public void setBanji(String banji) { this.banji = banji; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getPlase() { return plase; } public void setPlase(String plase) { this.plase = plase; } public String getBeizhu() { return beizhu; } public void setBeizhu(String beizhu) { this.beizhu = beizhu; } public String getName() { return name; } public Course() {} public Course( String num, String mima, String sex, String name, String studentnum, String youxiang, String xueyuan, String xi, String banji, String year, String plase, String beizhu) { this.num = num; this.mima = mima; this.sex = sex; this.name = name; this.studentnum = studentnum; this.youxiang = youxiang; this.xueyuan = xueyuan; this.xi = xi; this.banji = banji; this.year = year; this.plase = plase; this.beizhu = beizhu; } }
    CourseServlet.java
    package servlet;
    import java.io.IOException;
    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 entity.Course;
    import dao.CourseDao;public class CourseServlet extends HttpServlet {  
        private static final long serialVersionUID = 1L;
         public CourseServlet() {
                super();
            }
        CourseDao dao = new CourseDao();
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method = req.getParameter("method");
            if ("add".equals(method)) {
                add(req, resp);
            } 
        }
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.setCharacterEncoding("utf-8");
            String num = req.getParameter("num");
            String mima = req.getParameter("mima");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String studentnum = req.getParameter("studentnum");       
            String xueyuan = req.getParameter("xueyuan");
            String xi = req.getParameter("xi");
            String banji = req.getParameter("banji");
            String year = req.getParameter("year");
            String plase = req.getParameter("plase");
            String beizhu = req.getParameter("beizhu");
            String youxiang = req.getParameter("youxiang");    
            Course course = new Course(num,mima,sex,name,studentnum,youxiang,xueyuan,xi,banji,year,plase,beizhu);        
            //添加后消息显示
            if(dao.add(course)){   
                req.setAttribute("message", "注册成功");
                req.getRequestDispatcher("index.jsp").forward(req,resp);}
            else {
                req.setAttribute("message", "注册失败");
                req.getRequestDispatcher("zhuce.jsp").forward(req,resp);}
            }
        }
    CourseDao.java
    package dao;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    import entity.Course;
    import util.DBUtil;
    public class CourseDao {
        public boolean add(Course course) {
            String sql = "insert into information( num , mima , sex, name , studentnum, youxiang,xueyuan,xi,banji,year,plase,beizhu) values('" + course.getNum() + "','" + course.getMima() + "','" + course.getSex() +  "','" + course.getName()+"','" + course.getStudentnum()+"','" + course.getYouxiang()+"','" + course.getXueyuan()+"','" + course.getXi()+"','" + course.getBanji()+"','" + course.getYear()+"','" + course.getPlase()+"','" + course.getBeizhu()+"')";
            Connection conn = DBUtil.getConn();
            Statement state = null;
            boolean f = false;
            int a = 0;        
            try {
                state = conn.createStatement();
                a=state.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                DBUtil.close(state, conn);
            }        
            if (a > 0) {
                f = true;
            }
            return f;
        }
    }

    DBUtil.java

    
    
    package util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    public class DBUtil {
        public static String db_url = "jdbc:mysql://localhost:3306/text1?useSSL=false";
        public static String db_user = "root";
        public static String db_pass = "123456";    
        public static Connection getConn () {
            Connection conn = null;    
            try {
                Class.forName("com.mysql.jdbc.Driver");//加载驱动
                conn = DriverManager.getConnection(db_url, db_user, db_pass);
            } catch (Exception e) {
                e.printStackTrace();
            }        
            return conn;
        }
        public static void close (Statement state, Connection conn) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }        
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }    
        public static void close (ResultSet rs, Statement state, Connection conn) {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }        
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }        
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    

    index.jsp

    
    
    <%@ 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>
    
    

    zhucu.jsp

    
    
    <%@ 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>
    <style>
        .a{
            margin-top: 20px;
        }
        .b{
            font-size: 20px;
             160px;
            color: white;
            background-color: greenyellow;
        }
    </style>
    </head>
    <body>
        <%
             Object message = request.getAttribute("message");
             if(message!=null && !"".equals(message)){ 
        %>
             <script type="text/javascript">
                  alert("<%=request.getAttribute("message")%>");
             </script>
        <%} %>
        <div align="center">
            <h1 style="color: red;">注册</h1>
                <form action="CourseServlet?method=add" method="post" onsubmit="return check()">                                                                           
                <div class="a">
                    登录账号<input type="text" id="num"name="num" value=""/>    
                          
                </div>
                <div class="a">
                    登录密码:<input type="password" id="mima"name="mima" value="" />
                </div>
                性别: <select name="sex" >
                    <option value="男">男</option>
                    <option value="女">女</option>
                 
                </select>
                <div class="a">
                    姓名:<input type="text" name="name" value="" />
                </div>
                <div class="a">
                    学号:<input type="text" id="studentnum"name="studentnum" value="" />
                </div>
                <div class="a">
                    电子邮件:<input type="text" id="youxiang" name="youxiang" value="" />
                </div>
              <div class="a">
                所在学院:<input type="text" name="xueyuan" value="" />
                </div>
                <div class="a">
                    所在系:<input type="text" name="xi" value="" />
                </div>
                <div class="a">
                    所在班级:<input type="text" name="banji" value="" />
                </div>
                入学年份(届)<select name="year">
                    <option value="2016">2016</option>
                    <option value="2017">2017</option>
                    <option value="2018">2018</option>
                    <option value="2019">2019</option>
                    </select><div class="a">
                    生源地:<input type="text" name="plase" value=""/>                         
                </div>
                <div class="a">
                    备注:<textarea rows="10" cols="50" name="beizhu">                    
                  </textarea>
                </div>         
                <div class="a">
                    <button type="submit" class="b">保&nbsp;&nbsp;&nbsp;存</button>
                </div>        
            </form>
        </div>
    <script type="text/javascript">
    function check() {
        var num = document.getElementById("num");
        var mima = document.getElementById("mima");
        var studentnum = document.getElementById("studentnum");
        var youxiang = document.getElementById("youxiang");
        if (!num.value.match(/^[a-zA-Z]w{5,11}$/)) {
            alert("用户名由六到十二英文字符和数字组成,以英文字母开头");
            num.focus();
            return false;
    }
        else if (!mima.value.match(/^[A-Za-z0-9]w{7,20}$/)) {
         alert(" 密码由八 位 以上英文 和数 字 组成");
         mima.focus();
         return false;
    }
        else if(studentnum.value<"20180000"|| studentnum.value>"20189999")
            {
             alert(" 学号由2018开头的八位组成");    
             studentnum.focus();
             return false;
            }
        else if (!youxiang.value.match(/^[a-z0-9]+([._\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {
         alert(" 邮箱格式错误");
         youxiang.focus();
         return false;
    }
    } 
        </script>
    </body>
    </html>
    
    
    


  • 相关阅读:
    斐波那契数列的递归和非递归实现
    二叉树遍历
    基础总结
    内部类
    I/O dempo
    读取文件注意事项
    eclipse 主题设置
    String 常用函数
    多态的理解
    ffmpeg处理RTMP流媒体的命令大全
  • 原文地址:https://www.cnblogs.com/fengjingfei/p/11716344.html
Copyright © 2020-2023  润新知