• java注册界面及mysql连接


    题目要求

    完成注册界面及添加功能

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

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

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

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

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

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

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

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

    设计思路

    页面布局部分(.jsp):

       

    add.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>添加学生信息</title>
    </head>
    <body style="text-align:center;">
     <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
     
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <br>
     <%--换行--%>
      <h2>添加学生信息</h2>
      <hr size="1" noshade color="#000000">
      <%--横线--%>
      <form action="Servlet?method=add" method="post" id="form"
       onSubmit="return validate()">
     
       <table width="800" border="0" align="center">
        <%--表格的宽为800,居中对齐--%>
        <%--表格的宽为800,居中对齐--%>
       <tr>
        <td>账号: <input type="text" name="username"></td>
       </tr>
       <tr>
        <td>密码: <input type="password" name="password"></td>
       </tr>
       <tr>
        <td>姓名: <input type="text" name="name">
        </td>
       </tr>
       <tr>
        <td>性别:&nbsp;&nbsp;&nbsp;<input id="pingtaijb" name="sex" type="checkbox"
         value="男">男 </input> <input id="pingtaijb" name="sex"
         type="checkbox" value="女" />女 </input>
        </td>
       </tr>
       <tr>
        <td>学号: <input type="text" name="num"></td>
       </tr>
       <tr>
        <td>邮箱: <input type="text" name="e_mail">
        </td>
       </tr>
       <tr>
        <td>学校: <input type="text" name="school">
        </td>
       </tr>
       <tr>
        <td>专业: <input type="text" name="zhuan">
        </td>
       </tr>
       <tr>
        <td>班级: <input type="text" name="ban">
        </td>
       </tr>
       <tr>
        <td>年份: <input type="text" name="year">
        </td>
       </tr>
       <tr>
        <td>生源地: <select name="diqu" οnchange="showUser(this.value)">
         <option value="1">北京市</option>
         <option value="2">天津市</option>
         <option value="3">河北省</option>
         <option value="4">山西省</option>
        </select>
        </td>
       </tr>
       <tr>
        <td>备注: <input type="text" name="bei">
        </td>
       </tr>
       <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="submit" type="submit" value="添加" />
        <input type="reset" value="重置"></td>
       </tr>
      </table>
      <p>&nbsp;</p>
     </form>
    <script type="text/javascript" language="JavaScript">
      //JS
      function validate() {
       
           var num = document.forms[0].username.value;//账号
           var password = document.forms[0].password.value;//密码
           var sex = document.forms[0].sex.value; //姓名
           var name = document.forms[0].name.value; //性别
           var stunum = document.forms[0].num.value;//学号
           var e_mail = document.forms[0].e_mail.value; //邮箱
           var college = document.forms[0].school.value; //学校
           var major = document.forms[0].zhuan.value; //专业
           //var ban = document.forms[0].ban.value; //班级
           var year = document.forms[0].year.value; //年份
           var locate = document.forms[0].diqu.value; //生源地
           var beizhu = document.forms[0].bei.value; //备注
       
           /*var username = validate(document.form.user_name.value);
           var password = validate(document.form.password.value);
           var name = validate(document.form.name.value);
           var sex = validate(document.form.sex.value);
           var num = validate(document.form.num.value);
           var e_mail = validate(document.form.e_mail.value);
           var school = validate(document.form.school.value);
           var zhuan = validate(document.form.zhuan.value);
           var ban = validate(document.form.ban.value);
           var year = validate(document.form.year.value);
           var diqu = validate(document.form.diqu.value);
           var bei = validate(document.form.bei.value);*/
       
           if (username[0]<'A'||username[0]>'z') {
              alert("账号必须以字母开头!");
              return false;
           }
           else if (12<username.length||username.length <6) { //判断姓名位数,必填
              alert("账号必须6-12位!");
              return false;
           }
           else if (password.length <= 8) {
              alert("请输入八位以上的密码!");
              return false;
           }
           else if (name.length <= 0) {
              alert("姓名不能为空,请输入姓名!");
              return false;
           }
           else if (num.length<8) {
              alert("学号必须是8位!");
              return false;
           }
           else if (num[0]=='2'&&num[1]=='0'&&num[2]=='1'&&num[3]=='7') {
              alert("前四位必须是2018!");
              return false;
           }
           else if (e_mail.length <= 0) {
              alert("邮箱不能为空,请输入邮箱!");
              return false;
           }
           else if(!/(S)+[@]{1}(S)+[.]{1}(w)+/.test(e_mail))
           {
              alert("请输入格式正确的 e-mail 地址!");
              return false;
           }  
           else {
              return true;
           }
           //document.getElementById("form").submit();
     }
     </script>
    </body>
    </html>

    数据库连接部分(.java):

    DBUtil.java

    package util;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    /**
     * 数据库连接工具
     * @author Hu
     *
     */
    public class DBUtil {
     
       public static String dbURL="jdbc:mysql://localhost:3306/test";//数据源  !!!!注意若出现加载或者连接数据库失败一般是这里出现问题
       public static String Name="root";
       public static String Pwd="123456";
     
       public static Connection getConn () {
           Connection conn = null;
      
           try {
          Class.forName("com.mysql.jdbc.Driver");//加载驱动
          conn = DriverManager.getConnection(dbURL, Name, Pwd);
           } catch (Exception e) {
               e.printStackTrace();
           }
      
            return conn;
     }
     
     /**
      * 关闭连接
      * @param state
      * @param 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();
       }
      }
     }
    }

    dao.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 cls.admin;
    import util.DBUtil;
    public class dao {
     /**
      * 娣诲姞
      * @param course
      * @return
      */
     public boolean add(admin course) {
      String sql = "insert into clc(num,password,sex,name,stunum,e_mail,college,major,year,locate,beizhu) "
        + "values('" +course.getNum()+  "',"
          + "'" + course.getPassword() + "',"
            + "'" + course.getSex()+ "',"
              + "'"+ course.getName()+ "',"
                + "'"+ course.getStunum() + "'"
                  + ",'" + course.getE_mail() + "',"
                    + "'" + course.getCollege()+ "',"
                      + "'" +course.getMajor()+"',"
                        + "'"+course.getYear() +"',"
                          + "'"+course.getLocate()+"',"
                            + "'"+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;
     }
    }
     
    Lservice.java
    package service;
    import cls.admin;
    import dao.dao;
    /**
     * CourseService
     * 服务层
     * @author Hu
     *
     */
    public class Lservice {
     static dao cDao = new dao();
     
     /**
      * 添加
      * @param course
      * @return
      */
     public static boolean add(admin course) {
      boolean f = false;
      
       cDao.add(course);
       f = true;
      
      return f;
     }
     
    }
     
    admin.java
     
    package cls;
    public class admin {
    private String num;
    private String password;
    private String sex;
    private String name;
    private String stunum;
    private String e_mail;
    private String college;
    private String major;
    private String year;
    private String locate;
    private String beizhu;
    public String getNum() {
     return num;
    }
    public void setNum(String num) {
     this.num = num;
    }
    public String getPassword() {
     return password;
    }
    public admin(String num, String password, String sex, String name, String stunum, String e_mail, String college, String major,
      String year, String locate, String beizhu) {
     super();
     this.num = num;
     this.password = password;
     this.sex = sex;
     this.name = name;
     this.stunum = stunum;
     this.e_mail = e_mail;
     this.college = college;
     this.major = major;
     this.year = year;
     this.locate = locate;
     this.beizhu = beizhu;
    }
    public void setPassword(String password) {
     this.password = password;
    }
    public String getSex() {
     return sex;
    }
    public void setSex(String sex) {
     this.sex = sex;
    }
    public String getName() {
     return name;
    }
    public void setName(String name) {
     this.name = name;
    }
    public String getStunum() {
     return stunum;
    }
    public void setStunum(String stunum) {
     this.stunum = stunum;
    }
    public String getE_mail() {
     return e_mail;
    }
    public void setE_mail(String e_mail) {
     this.e_mail = e_mail;
    }
    public String getCollege() {
     return college;
    }
    public void setCollege(String college) {
     this.college = college;
    }
    public String getMajor() {
     return major;
    }
    public void setMajor(String major) {
     this.major = major;
    }
    public String getYear() {
     return year;
    }
    public void setYear(String year) {
     this.year = year;
    }
    public String getLocate() {
     return locate;
    }
    public void setLocate(String locate) {
     this.locate = locate;
    }
    public String getBeizhu() {
     return beizhu;
    }
    public void setBeizhu(String beizhu) {
     this.beizhu = beizhu;
    }
    public void admin()
    {
    }
    }
     

    Servlet部分:

    Servlet.java

    package servlet;
    import java.io.IOException;
    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 service.Lservice;
    import cls.admin;
    /**
     * Servlet implementation class Servlet
     */
    @WebServlet("/Servlet")
    public class Servlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
          
        /**
         * @see HttpServlet#HttpServlet()
         */
        public Servlet() {
            super();
            // TODO Auto-generated constructor stub
        }
     /**
      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */
    Lservice service = new Lservice();
     
     /**
      * 方法选择
      */
     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);
      }
     }
     /**
      * 添加
      * @param req
      * @param resp
      * @throws IOException
      * @throws ServletException
      */
     private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
      req.setCharacterEncoding("utf-8");
      String num = req.getParameter("num");
      String password = req.getParameter("password");
      String sex = req.getParameter("sex");
      String name = req.getParameter("name");
      String stunum = req.getParameter("stunum");
      String e_mail = req.getParameter("e_mail");
      String college = req.getParameter("college");
      String major = req.getParameter("major");
      String year = req.getParameter("year");
      String locate = req.getParameter("locate");
      String beizhu = req.getParameter("beizhu");
      admin course = new admin(num,password,sex,name,stunum,e_mail,college,major,year,locate,beizhu);
      
      //添加后消息显示
      if(Lservice.add(course)) {
       req.setAttribute("message", "添加成功");
       req.getRequestDispatcher("add.jsp").forward(req,resp);
      } else {
       req.setAttribute("message", "姓名重复,请重新录入");
       req.getRequestDispatcher("add.jsp").forward(req,resp);
      }
     }
    }
  • 相关阅读:
    在循环中正确找到对应DOM元素的索引
    JavaScript继承方式(1)
    JavaScript的写类方式(1)
    基于WF4的新平台地理模块的3D模块
    基于WF4的新平台流程模式(4)动态并发
    基于WF4的新平台流程模式(9)表单路由启动传入
    基于WF4的新平台地理模块的3D模块
    基于WF4的新平台流程模式(2)启动条件路由
    基于WF4的新平台表单设计器功能简介
    我的2010年
  • 原文地址:https://www.cnblogs.com/songxinai/p/11716825.html
Copyright © 2020-2023  润新知