记录一下自己写的注册功能:用的编译器 eclipse  数据库 Mysql  服务器  tomcat

服务器搭建配置这里就直接过了(可以参考):https://www.cnblogs.com/2979100039-qq-con/p/12493329.html

一,建库建表

 

 二、创建动态web项目:

 

 

jar包下载地址    https://mvnrepository.com/artifact/mysql/mysql-connector-java

下载完成后复制到 lib文件夹下

下面上代码:

jsp页面代码:<head><meta charset="UTF-8">

复制代码
<title>Insert title here</title>
<style type="text/css">
   form {
         margin:0px 460px 0px 387px;
    }
    .rest:hover{
           color:black;
           width: 230px;
           height: 26px;
           background:#0066ff;
           border: none;
           border-radius:8px; 
       }
      .rest {
           color:black;
           width: 230px;
           height: 26px;
           background: #cccccc;
           border: none;
           border-radius:8px; 
       }
 </style>
</head>
<body>
<form action="registerServlet" method="post" name="from" >
<br>
 用户名:<input type="text" name="username" id="username"><br>
 密 &nbsp;码  :<input type="password" name="password" id="password"><br>
 性别:<input type="radio" name="sex" value="男" >男<input type="radio" name="sex" value="女" >女<br>
 爱好:<input type="checkbox" name="habby" value="羽毛球">羽毛球
      <input type="checkbox" name="habby" value="篮球">篮球
      <input type="checkbox" name="habby" value="足球">足球<br>
      <input type="submit" value="注册" class="rest" onclick=" return register();" >
</form>
<script type="text/javascript">
        function register(){
                 var flag = true;
                var admin = document.getElementById("username").value;
                var password = document.getElementById("password").value;
                if (admin==""){
                    alert("账号不能为空,请输入账号!");
                    flag = false;
                    return false;
                }
                else if (password==""){
                    alert("密码不能为空 ,请输入密码!");
                    flag = false;
                    return false;
                } 
                for(i=0;i<from.sex.length;i++)
                  {
                    if(from.sex[0].checked||from.sex[1].checked)
{ return true;
} else
{ alert("性别未选,请选择"); return false; }
if(flag == true){ return true; }
} </script>
</body>
复制代码

servlet代码:

复制代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
             request.setCharacterEncoding("UTF-8");   
             response.setContentType("text/html;charset=UTF-8");
             String name = request.getParameter("username");
             String pwd = request.getParameter("password");
             String sex = request.getParameter("sex");
             String[] happy = request.getParameterValues("habby");
             StringBuffer buf = new StringBuffer();
             for (String string : happy) {
                    buf.append(string);
            }
             String string = buf.toString();
             boolean boo = UserDao.register(name,pwd,sex,string);
          if (boo) {
              response.getWriter().print ("<script>");
              response.getWriter().print ("alert('恭喜您 注册成功!')");
              response.getWriter().print ("</script>");
           }
        /*response.getWriter().print("<script> alert(\"请确认您的账号密码!\"); </script>");*/
            
    }
复制代码

User(实体类代码)

复制代码
 private int  id;
     private  String name;
     private  String pwd;
     private  String sex;
     private  String hobby;
     
     
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getHobby() {
        return hobby;
    }
    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
    
    public User(int id, String name, String pwd, String sex, String hobby) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.sex = sex;
        this.hobby = hobby;
    }
复制代码

JDBC代码就是连接数据库的

复制代码
  private static final String DRIVER="com.mysql.cj.jdbc.Driver";
         private static final String URL="jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
         private static final String NAME="root";
         private static final String PWD="root";
         
         static {
             try {
                Class.forName(DRIVER);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
             
         }
        private static Connection getconnection() {
            try {
            return    DriverManager.getConnection(URL,NAME,PWD);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return null;
            
        }
        private static PreparedStatement getpreparedStatement(String sql,Object...objects) {
              try {
                 PreparedStatement prepareStatement = getconnection().prepareStatement(sql);
                 for (int i = 0; i < objects.length; i++) {
                     prepareStatement.setObject(i+1, objects[i]);
                 }return prepareStatement;
             } catch (SQLException e) {
                 e.printStackTrace();
             }
             return null;
         }
        public static boolean executeUpade(String sql,Object...objects) {
               try {
                int updae = getpreparedStatement(sql, objects).executeUpdate();
                if(updae>0) return true;
            } catch (SQLException e) {
                e.printStackTrace();
            }
             return false;
         }
复制代码

UserDao代码

    public static boolean register(String name, String pwd, String sex, String string) {
        return JDBCUtil.executeUpade("INSERT INTO `tb_user` (`name`, `pwd`, `sex`, `hobby`) VALUES (?,?,?,?)"
                ,name,pwd,sex,string);
    }