javaWeb完成注册功能

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

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

一,建库建表

javaWeb完成注册功能_第1张图片

 

 二、创建动态web项目:

javaWeb完成注册功能_第2张图片

 

 

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

javaWeb完成注册功能_第3张图片

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

下面上代码:

jsp页面代码:

Insert title here




用户名:
 码 :
性别:
爱好:羽毛球 篮球 足球
class="rest" onclick=" return register();" >

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 ("");
           }
        /*response.getWriter().print("");*/
            
    }

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

代码结束

看起来其实代码量很多,但是是自己花了半天一点点写出来的,如果有不足之处希望可以告知,谢谢!

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

你可能感兴趣的:(javaWeb完成注册功能)