javaWeb完成注册功能

记录一下本身写的注册功能:用的编译器 eclipse  数据库 Mysql  服务器  tomcatjavascript

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

一,建库建表

 

 2、建立动态web项目:

 

 

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

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

下面上代码:mysql

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

<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代码:sql

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代码就是链接数据库的tomcat

  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);
    }

代码结束

看起来其实代码量不少,可是是本身花了半天一点点写出来的,若是有不足之处但愿能够告知,谢谢!

若有不懂的地方能够留言询问,或者联系qq 2979100039  我会尽力帮忙    over

相关文章
相关标签/搜索