• javeweb_学生信息添加系统


    在text.jsp中画出界面,以及设置提交选项的限制

      1 <%@ page language="java" contentType="text/html; charset=UTF-8"
      2     pageEncoding="UTF-8"%>
      3 <!DOCTYPE html>
      4 <html>
      5 <head>
      6 <meta charset="UTF-8">
      7 <title>当前位置:添加学生信息</title>
      8 </head>
      9 <body>
     10 <%
     11         Object message = request.getAttribute("message");
     12         if (message != null && !"".equals(message)) {
     13     %>
     14     <script type="text/javascript">
     15               alert("<%=request.getAttribute("message")%>");
     16               var  asd=request.getAttribute("username");         
     17     </script>
     18     <%
     19         }
     20     %>
     21 <form action="Servlet?method=add" method="post" onsubmit="return check(this)">
     22 
     23 <table >
     24         <tr>
     25             <th>登录账号:</th>
     26             <td>
     27                 
     28                 <input name="username" type="text"  />
     29             </td>
     30         </tr>
     31         <tr>
     32             <th>登录密码:</th>
     33             <td>
     34                 <input name="password" type="password" />
     35             </td>
     36         </tr>
     37         <tr>
     38             <th>性别:</th>
     39             <td>
     40                 <select name="sex">
     41                     <option>男</option>
     42                     <option>女</option>
     43                 </select>
     44             </td>
     45         </tr>
     46         <tr>
     47             <th>姓名:</th>
     48             <td>
     49                 <input name="name" type="text">
     50             </td>
     51         </tr>
     52         <tr>
     53             <th>学号:</th>
     54             <td>
     55                 <input name="sno" type="text">
     56             </td>
     57         </tr>
     58         <tr>
     59             <th>电子邮件:</th>
     60             <td>
     61                 <input name="email" type="text">
     62             </td>
     63         </tr>
     64         <tr>
     65             <th>所在学校:</th>
     66             <td>
     67                 <input name="school" type="text">
     68             </td>
     69         </tr>
     70         <tr>
     71             <th>所在系:</th>
     72             <td>
     73                 <input name="college" type="text">
     74             </td>
     75         </tr>
     76         <tr>
     77             <th>所在班级:</th>
     78             <td>
     79                 <input name="class1" type="text">
     80             </td>
     81         </tr>
     82         <tr>
     83             <th>入学年份(届):</th>
     84             <td>
     85                 <select name="time">
     86                         <option>2015</option>
     87                         <option>2016</option>
     88                         <option>2017</option>
     89                         <option>2018</option>
     90                         <option>2019</option>
     91                 </select>
     92             </td>
     93         </tr>
     94         
     95         <tr>
     96             <th>生源地:</th>
     97             <td>
     98                 <input name="ofstudents" type="text">
     99             </td>
    100         </tr>
    101         
    102         <tr>
    103             <th>备注:</th>
    104             <td>
    105                 <input name="remarks" type="text">
    106             </td>
    107         </tr>
    108         <tr>
    109             <td >
    110                 <input type="submit" value="提交" />
    111                 
    112             </td>
    113         </tr>
    114     </table>
    115 </form>
    116 
    117 
    118 </body>
    119 <script type="text/javascript">
    120 function check(form){
    121     if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^a-zA-Z0-9_u4e00-u9fa5]").test(form.username.value)){
    122         alert("登陆账号必须由6-12位英文字符或数字组成!");
    123         form.username.focus();
    124         return false;
    125     }
    126     if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
    127         alert("登陆账号必须以英文字母开头!");
    128         form.username.focus();
    129         return false;
    130     }
    131     if(form.password.value.length < 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){
    132         alert("密码由八位以上数字或字母组成!");
    133         form.password.focus();
    134         return false;
    135     }
    136     var re = /^d{4}/;
    137     if(form.sno.value.length != 8 || form.sno.value.match(re)[0]!='2018'){
    138         alert("学号由八位组成,前四位是2018");
    139         form.password.focus();
    140         return false;
    141     }
    142     //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
    143     var regex = /^([a-zA-Z]|[0-9])(w|-)+@[a-zA-Z0-9]+.([a-zA-Z]{2,4})$/;
    144     if(!regex.test(form.email.value)){
    145         alert("邮箱格式错误!");
    146         form.email.focus();
    147         return false;
    148     }
    149     return true;
    150 }
    151 </script>
    152 </html>

    创建4个包,在每个包里面创建一个.Java文件,分别是Dao、DBUtil、User、Servlet,用来连接数据库,以及向数据库中添加信息

    Dao:

     1 package Dao;
     2 
     3 import java.sql.Connection;
     4 import java.sql.Statement;
     5 
     6 import DBUtil.DBUtil;
     7 
     8 import Entity.User;
     9 
    10 public class Dao {
    11 
    12     public boolean add(User user) {
    13         // TODO Auto-generated method stub
    14         String sql = "insert into kao(username,password,sex,name,sno,email,school,college,class1,time,ofstudents,remarks) values('"+ user.getUsername() + "','"+ user.getPassword() +"','"+ user.getSex() +"','" + user.getName() +"','"+ user.getSno() +"','"+ user.getEmail() +"','"+ user.getSchool() +"','"+ user.getCollege() +"','"+ user.getClass1() +"','"+ user.getTime() +"','"+ user.getOfstudents()+"','" +user.getRemarks()+"')";
    15         Connection conn = DBUtil.getConn();
    16         Statement state = null;
    17         boolean f = false;
    18         int a = 0;
    19 
    20         try {
    21             state = conn.createStatement();
    22             a=state.executeUpdate(sql);
    23         } catch (Exception e) {
    24             e.printStackTrace();
    25         } finally {
    26             
    27             DBUtil.close(state, conn);
    28         }
    29 
    30         if (a > 0) {
    31             f = true;
    32         }
    33         return f;
    34 
    35 }
    36 }

    DBUtil:

     1 package DBUtil;
     2 
     3 
     4 
     5 import java.sql.Connection;
     6 import java.sql.DriverManager;
     7 import java.sql.PreparedStatement;
     8 import java.sql.ResultSet;
     9 import java.sql.SQLException;
    10 import java.sql.Statement;
    11 
    12 
    13 public class DBUtil {
    14     
    15     public static String db_url = "jdbc:mysql://localhost:3306/xinxi";
    16     public static String db_user = "root";
    17     public static String db_pass = "wyp6666";
    18     
    19     public static Connection getConn () {
    20         Connection conn = null;
    21         
    22         try {
    23             Class.forName("com.mysql.jdbc.Driver");
    24             conn = DriverManager.getConnection(db_url, db_user, db_pass);
    25         } catch (Exception e) {
    26             e.printStackTrace();
    27         }
    28         
    29         return conn;
    30     }
    31     
    32     public static void close (Statement state, Connection conn) {
    33         if (state != null) {
    34             try {
    35                 state.close();
    36             } catch (SQLException e) {
    37                 e.printStackTrace();
    38             }
    39         }
    40         
    41         if (conn != null) {
    42             try {
    43                 conn.close();
    44             } catch (SQLException e) {
    45                 e.printStackTrace();
    46             }
    47         }
    48     }
    49     
    50     public static void close (ResultSet rs, Statement state, Connection conn) {
    51         if (rs != null) {
    52             try {
    53                 rs.close();
    54             } catch (SQLException e) {
    55                 e.printStackTrace();
    56             }
    57         }
    58         
    59         if (state != null) {
    60             try {
    61                 state.close();
    62             } catch (SQLException e) {
    63                 e.printStackTrace();
    64             }
    65         }
    66         
    67         if (conn != null) {
    68             try {
    69                 conn.close();
    70             } catch (SQLException e) {
    71                 e.printStackTrace();
    72             }
    73         }
    74     }
    75 
    76     public static void main(String[] args) throws SQLException {
    77         Connection conn = getConn();
    78         PreparedStatement pstmt = null;
    79         ResultSet rs = null;
    80         String sql ="select * from users";
    81         pstmt = conn.prepareStatement(sql);
    82         rs = pstmt.executeQuery();
    83         if(rs.next()){
    84             System.out.println("成攻");
    85         }else{
    86             System.out.println("失败");
    87         }
    88     }
    89 }

    Servlet:

     1 package Servlet;
     2 import java.io.IOException;
     3 
     4 import javax.servlet.ServletException;
     5 import javax.servlet.annotation.WebServlet;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 
    10 import Dao.Dao;
    11 
    12 import Entity.User;
    13 
    14 
    15 
    16 @WebServlet("/Servlet")
    17 public class Servlet extends HttpServlet {
    18     private static final long serialVersionUID = 1L;
    19        
    20     
    21     public Servlet() {
    22         super();
    23         
    24     }
    25     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    26         req.setCharacterEncoding("utf-8");
    27         String method = req.getParameter("method");
    28         if ("add".equals(method)) {
    29             add(req, resp);
    30         } 
    31     }
    32     
    33     
    34     private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
    35         req.setCharacterEncoding("utf-8");
    36         String username = req.getParameter("username");
    37         String password = req.getParameter("password");
    38         String name = req.getParameter("sex");
    39         String sex = req.getParameter("name");
    40         String sno = req.getParameter("sno");
    41         String email = req.getParameter("email");
    42         String school = req.getParameter("school");
    43         String college = req.getParameter("college");
    44         String class1 = req.getParameter("class1");
    45         String time= req.getParameter("time");
    46         String ofstudents = req.getParameter("ofstudents");
    47         String remarks = req.getParameter("remarks");
    48         User user = new User(username,password,sex,name,sno,email,school,college,class1,time,ofstudents,remarks);
    49         
    50         Dao dao =new Dao();
    51         boolean f=dao.add(user);
    52         
    53         
    54         if(f) {
    55             req.setAttribute("message", "注册成功!");
    56             req.getRequestDispatcher("text.jsp").forward(req,resp);
    57         } else {
    58             req.setAttribute("message", "注册失败!");
    59             req.getRequestDispatcher("text.jsp").forward(req,resp);
    60         }
    61     }
    62 }
  • 相关阅读:
    django模型的crud操作
    django模型中的关系对应
    django中模型详解-字段类型与约束条件
    django中的模型详解-1
    运维自动化轻量级工具pssh
    zabbix告警使用sendEmail
    nginx正向代理,反向代理,透明代理(总结)
    nginx.conf的events,http段一般固定配置
    nginx实战2---浏览器设置缓存
    nginx之location
  • 原文地址:https://www.cnblogs.com/wyppaa/p/11716880.html
Copyright © 2020-2023  润新知