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

相關文章
相關標籤/搜索