添加注册数据到数据库

UseServlet.java

@WebServlet("/user.do")
public class UseServlet extends HttpServlet{
    
    /*
     * 在Servlet中使用JDBC访问数据库,可以将数据库连接的代码单独写成一个类,
     * 这样其他的功能模块也可以实现代码的复用。
     * 建立数据库连接:
     * */
    private static Connection conn=null;
    public static Connection getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String user = "root";
            String pwd = "123456";
            String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
            conn=DriverManager.getConnection(url,user,pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    /*
     * 该方法同样可以单独设置成类。
     * 保存用户注册信息到数据库和一个获得本类实例:
     * */
    private static UseServlet instance=null;
    public static UseServlet getInstance(){
        if(instance==null)
            instance = new UseServlet();
        return instance;
    }
    //保存用户注册信息
    public boolean saveUser(InfoBean user){
        Connection conn=null;
        try {
            conn=UseServlet.getConn();//建立数据库连接
            String sql="insert into test(name,pwd,sex,age,email) values(?,?,?,?,?)";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());         //为SQL语句第一个参数赋值
            pstmt.setString(2, user.getPwd());
            pstmt.setString(3, user.getSex());
            pstmt.setInt(4, user.getAge());
            pstmt.setString(5, user.getEmail());
            pstmt.executeUpdate();      //执行INSERT语句
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    
    private static final long serialVersionUID = 1L;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //设置字符集格式为utf-8
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
    
        String name = new String(req.getParameter("name").getBytes("utf-8"),"utf-8");
        String pwd=new String(req.getParameter("pwd").getBytes("utf-8"),"utf-8");
        String sex=new String(req.getParameter("sex").getBytes("utf-8"),"utf-8");
        String age=new String(req.getParameter("age").getBytes("utf-8"),"utf-8");
        int userAge =0;
        if(age!=null&&!age.equals(""))
            userAge = Integer.parseInt(age);
        String email=new String(req.getParameter("email").getBytes("utf-8"),"utf-8");
        
        /*将获得表单值封装到用户信息对象中*/
        InfoBean user=new InfoBean();
        user.setName(name);
        user.setPwd(pwd);
        user.setSex(sex);
        user.setAge(userAge);
        user.setEmail(email);
        boolean res=UseServlet.getInstance().saveUser(user);//将用户注册信息保存到数据库
        if (res) {
            req.getRequestDispatcher("success.jsp").forward(req, resp);
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }
}

InfoBean.java

public class InfoBean {
    private int id;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    private String name;
    private String pwd;
    private String sex;
    private  int age;
    private String email;
    public InfoBean(){}
    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 int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
}

index.jsp

用户名:
密码:
性别:
年龄:
Email:

success.jsp

注册成功!
您的注册信息为:
用户名: <%=request.getParameter("name") %>
密码: <%=request.getParameter("pwd") %>
性别: <%=request.getParameter("sex") %>
年龄: <%=request.getParameter("age") %>
Email: <%=request.getParameter("email") %>
【回到主页】
捕获.PNG

你可能感兴趣的:(添加注册数据到数据库)