• 学生信息的添加 Java web简单项目初试(失败)


    题目要求:

     

    1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)

    2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)

    3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)

    4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)

    5姓名:输入自己的姓名;

    5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)

    6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)

    7可以演示连接上数据库。(2分)

    com.Bean  

      1 public class User {
      2     private String username;
      3     private String password;
      4     private String name;
      5     private String studentID;
      6     private String sex;
      7     private String phone;
      8     private String email;
      9     private String xueyuan;
     10     private String xi;
     11     private String classes;
     12     private String year;
     13     private String shengyuandi;
     14     private String beizhu;
     15     public String getUsername() {
     16         return username;
     17     }
     18     public void setUsername(String username) {
     19         this.username = username;
     20     }
     21     public String getPassword() {
     22         return password;
     23     }
     24     public void setPassword(String password) {
     25         this.password = password;
     26     }
     27     public String getName() {
     28         return name;
     29     }
     30     public void setName(String name) {
     31         this.name = name;
     32     }
     33     public String getStudentID() {
     34         return studentID;
     35     }
     36     public void setStudentID(String studentID) {
     37         this.studentID = studentID;
     38     }
     39     public String getSex() {
     40         return sex;
     41     }
     42     public void setSex(String sex) {
     43         this.sex = sex;
     44     }
     45     public String getPhone() {
     46         return phone;
     47     }
     48     public void setPhone(String phone) {
     49         this.phone = phone;
     50     }
     51     public String getEmail() {
     52         return email;
     53     }
     54     public void setEmail(String email) {
     55         this.email = email;
     56     }
     57     public String getXueyuan() {
     58         return xueyuan;
     59     }
     60     public void setXueyuan(String xueyuan) {
     61         this.xueyuan = xueyuan;
     62     }
     63     public String getXi() {
     64         return xi;
     65     }
     66     public void setXi(String xi) {
     67         this.xi = xi;
     68     }
     69     public String getClasses() {
     70         return classes;
     71     }
     72     public void setClasses(String classes) {
     73         this.classes = classes;
     74     }
     75     public String getYear() {
     76         return year;
     77     }
     78     public void setYear(String year) {
     79         this.year = year;
     80     }
     81     public String getShengyuandi() {
     82         return shengyuandi;
     83     }
     84     public void setShengyuandi(String shengyuandi) {
     85         this.shengyuandi = shengyuandi;
     86     }
     87     public String getBeizhu() {
     88         return beizhu;
     89     }
     90     public void setBeizhu(String beizhu) {
     91         this.beizhu = beizhu;
     92     }
     93     public User(String username, String password, String name, String studentID, String sex, String phone, String email,
     94             String xueyuan, String xi, String classes, String year, String shengyuandi, String beizhu) {
     95         super();
     96         this.username = username;
     97         this.password = password;
     98         this.name = name;
     99         this.studentID = studentID;
    100         this.sex = sex;
    101         this.phone = phone;
    102         this.email = email;
    103         this.xueyuan = xueyuan;
    104         this.xi = xi;
    105         this.classes = classes;
    106         this.year = year;
    107         this.shengyuandi = shengyuandi;
    108         this.beizhu = beizhu;
    109     }
    110     
    111     
    112 
    113 }

    数据库驱动加载与连接()

     1 package com.DBUtil;
     2 
     3 import java.sql.Connection;
     4 
     5 import java.sql.DriverManager;
     6 //import java.sql.ResultSet;
     7 import java.sql.SQLException;
     8 //import java.sql.Statement;
     9 
    10 public class UserDBUtil {
    11     static Connection con;
    12     public static Connection getConnection() {
    13         try {
    14             Class.forName("com.mysql.cj.jdbc.Driver");
    15             System.out.println("数据库驱动加载成功");
    16         } catch (ClassNotFoundException e) {
    17             // TODO Auto-generated catch block
    18             e.printStackTrace();
    19         }
    20         
    21         try {
    22             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B88&serverTimezone=GMT%2B8&useSSL=false","root","101032");
    23             System.out.println("数据库链接成功!");
    24         } catch (SQLException e) {
    25             // TODO Auto-generated catch block
    26             e.printStackTrace();
    27         }
    28         
    29         return con;
    30         }
    31     
    32      public static void main(String[] args) {
    33          UserDBUtil c=new UserDBUtil();
    34          c.getConnection();
    35         
    36     }

     出现如图所示输出,说明数据库的驱动加载成功,数据库连接成功。

    servlet是运行在web服务器或应用服务器的程序,用来处理客户端请求的动态资源.即后台的服务程序

     1 package com.Servlet;
     2 
     3 import java.io.IOException;
     4 import java.sql.Connection;
     5 import java.sql.SQLException;
     6 import java.sql.Statement;
     7 //import java.util.List;
     8 
     9 import javax.servlet.ServletException;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 
    14 //import com.Bean.*;
    15 import com.DBUtil.*;
    16 
    17 public class AddServlet extends HttpServlet {
    18 
    19     private static final long serialVersionUID = 1L;
    20 
    21     protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException {
    22         req.setCharacterEncoding("UTF-8");
    23         
    24         String username = req.getParameter("username");
    25         String password= req.getParameter("password");
    26         String name= req.getParameter("name");
    27         String studnetID = req.getParameter("studnetID");
    28         String sex= req.getParameter("sex");
    29         String phone= req.getParameter("phone");
    30         String email= req.getParameter("email");
    31         String xueyuan = req.getParameter("xueyuan");
    32         String xi = req.getParameter("xi");
    33         String classes = req.getParameter("clsaaes");
    34         String year = req.getParameter("year");
    35         String shengyuandi = req.getParameter("shengyuandi");
    36         String beizhu = req.getParameter("beizhu");
    37         
    38         Connection conn = UserDBUtil.getConnection();
    39         Statement state =null;
    40         String addsql = null;            
    41         try {
    42             state = conn.createStatement();
    43             addsql = "insert into use01 (username,password,name,studnetID,sex,phone,email,xueyuan,xi,clsaaes,year,shengyuandi,beizhu) values ('"+username+"','"+password+"','"+name+"','"+studnetID+"','"+sex+"','"+phone+"','"+email+"','"+xueyuan+"','"+xi+"','"+classes+"','"+year+"','"+shengyuandi+"','"+beizhu+"')";
    44             state.executeUpdate(addsql);
    45             conn.close();
    46         } catch (SQLException e) {
    47             e.printStackTrace();
    48         }            
    49         resp.sendRedirect(req.getContextPath()+"/index.jsp");
    50         
    51     }
    52 }

    网页的排版编码:

      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 <script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script>
     12 
     13 <div class="container">
     14     <section>
     15         <form action="AddServlet" method="post" onsubmit="return check(this)">        
     16             <span class="username">登陆账号</span>
     17                 <input type="text" name="username" placeholder="  username" id="username">
     18             <br/>
     19             
     20             <span class="password">登陆密码</span>
     21             <input type="password" name="password" placeholder="  password" id="password">
     22             <br/>
     23             
     24             <span class="name">姓名</span>
     25                 <input type="text" name="name" placeholder="  name" id="name">
     26             <br/>
     27             
     28             <span class="studentID">学号</span>
     29             <input type="text" name="studentID" placeholder="  studentID" id="studentID" value="2018">
     30             <br/>
     31             
     32             <span class="sex">性别</span>
     33                 <input type="radio" name="sex" id="sexnan" value="男"> 34                 &nbsp;
     35                 <input type="radio" name="sex" id="sexnv" value="女"> 36             <br/>
     37             
     38 <!--             <span class="diqu">地区</span> -->
     39 <!--             <select name="diqu"> -->
     40 <!--                   <option value ="北京市">北京市</option> -->
     41 <!--                   <option value ="天津市">天津市</option> -->
     42 <!--                   <option value="河北省">河北省</option> -->
     43 <!--             </select> -->
     44 <!--             <br/> -->
     45             
     46             <span class="phone">手机号</span>
     47                 <input type="text" name="phone" placeholder="  phone" id="phone">
     48             <br/>
     49             
     50             <span class="email">邮箱</span>
     51                 <input type="text" name="email" placeholder="  email" id="email">
     52             <br/>
     53             
     54             <span class="xueyuan">所在学院:</span>
     55             <input type="text" name="xueyuan" placeholder="  xueyuan" id="xueyuan">
     56             <br/>
     57             
     58             <span class="xi">所在系:</span>
     59             <input type="text" name="xi" placeholder="  suozaixi" id="xi">
     60             <br/>
     61             
     62             <span class="classes">所在班级:</span>
     63             <input type="text" name="classes" placeholder="  classes" id="classes">
     64             <br/>
     65             
     66             
     67             <span class="year">入学年份(届):</span>
     68             <select name="year">
     69             <option value ="2014">2014</option>
     70                   <option value ="2015">2015</option>
     71                   <option value ="2016">2016</option>
     72                   <option value="2017">2017</option>
     73                   <option value="2018">2018</option>
     74                   <option value ="2019">2019</option>
     75                   </select>
     76             <br/>
     77             
     78             <span class="shengyuandi">生源地:</span>
     79             <input type="text" name="shengyuandi" placeholder="  shengyuandi" id="shengyuandi">
     80             <br/>
     81             
     82             <span class="beizhu">备注:</span>
     83             <input type="text" name="beizhu" placeholder="  beizhu" id="beizhu">
     84             <br/>
     85             
     86             <input type="submit" value="保存" class="butn">
     87 <!--             <input type="reset" value="重置" class="butn"> -->
     88             <br/>
     89         </form>
     90     </section>
     91 </div>
     92 
     93 <script>
     94     //正则表达式
     95 function check(form){
     96 
     97     if(form.username.value.length<6 || form.username.value.length>12 || new RegExp("[^0-9a-zA-Z]").test(form.username.value)){
     98         alert("登陆账号必须由6-12位英文字符或数字组成!");
     99         form.user.focus();
    100         return false;
    101     }
    102     if(new RegExp("[^a-zA-Z]").test(form.username.value.substring(0,1))){
    103         alert("登陆账号必须以英文字母开头!");
    104         form.user.focus();
    105         return false;
    106     }
    107     if(form.password.value.length <= 8 || new RegExp("[^0-9a-zA-Z]").test(form.password.value)){
    108         alert("密码必须由八位以上字母、数字组成!");
    109         form.phone.focus();
    110         return false;
    111     }
    112     if(form.studentID.value.length != 8 || new RegExp("[^0-9]").test(form.studentID.value)){
    113         alert("请输入八位学号!");
    114         form.phone.focus();
    115         return false;
    116     }
    117 
    118     if(form.phone.value.length != 11 || new RegExp("[^0-9]").test(form.phone.value)){
    119         alert("手机号请输入11位数字!");
    120         form.phone.focus();
    121         return false;
    122     }
    123 
    124     //以数字字母开头,中间可以是多个数字字母或下划线;然后是“@”;然后是数字字母;然后是“.”;最后是2-4个字母结尾
    125     var regex = /^([a-zA-Z]|[0-9])(w|-)+@[a-zA-Z0-9]+.([a-zA-Z]{2,4})$/;
    126     if(!regex.test(form.email.value)){
    127         alert("邮箱格式错误!");
    128         form.email.focus();
    129         return false;
    130     }
    131     return true;
    132 }
    133 </script>
    134 
    135 </body>
    136 </html>

    网页面板如下:

    问题:

    当数据输入到界面时,界面跳转失败,无法录入数据到数据库。

    错误原因如下:

     

     目前仍旧没由修正。

  • 相关阅读:
    UIPasteboard 粘贴板
    UIViewController没有随着设备一起旋转的原因
    UIButton 应用选择状态(附:UIButton 常用状态)
    WebService 中参数为枚举时引发的血案
    设计模式(1)之面向对象设计原则 阿正
    2012年年终总结 阿正
    生活工作如登山 阿正
    感谢我的技术总监 阿正
    尽孝要尽早 阿正
    我老了吗?不 你依然年轻 阿正
  • 原文地址:https://www.cnblogs.com/cxy0210/p/11715924.html
Copyright © 2020-2023  润新知