添加注冊數(shù)據(jù)到數(shù)據(jù)庫

UseServlet.java

@WebServlet("/user.do")
public class UseServlet extends HttpServlet{
    
    /*
     * 在Servlet中使用JDBC訪問數(shù)據(jù)庫,可以將數(shù)據(jù)庫連接的代碼單獨寫成一個類,
     * 這樣其他的功能模塊也可以實現(xiàn)代碼的復(fù)用。
     * 建立數(shù)據(jù)庫連接:
     * */
    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;
    }
    
    /*
     * 該方法同樣可以單獨設(shè)置成類。
     * 保存用戶注冊信息到數(shù)據(jù)庫和一個獲得本類實例:
     * */
    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();//建立數(shù)據(jù)庫連接
            String sql="insert into test(name,pwd,sex,age,email) values(?,?,?,?,?)";
            PreparedStatement pstmt=conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());         //為SQL語句第一個參數(shù)賦值
            pstmt.setString(2, user.getPwd());
            pstmt.setString(3, user.getSex());
            pstmt.setInt(4, user.getAge());
            pstmt.setString(5, user.getEmail());
            pstmt.executeUpdate();      //執(zhí)行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 {
        //設(shè)置字符集格式為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);//將用戶注冊信息保存到數(shù)據(jù)庫
        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

<form action="user.do" method="post">
        <table align="center">
            <tr>
                <td>用戶名:</td>
                <td><input type="text" name="name"/></td>
            </tr>
            <tr>
                <td>密碼:</td>
                <td><input type="password" name="pwd"/></td>
            </tr>
            <tr>
                <td>性別:</td>
                <td>
                    <input type="radio" name="sex" value="男"/>男
                    <input type="radio" name="sex" value="女"/>女
                </td>
            </tr>
            <tr>
                <td>年齡:</td>
                <td><input type="text" name="age"/></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                <input type="submit" value="注冊"/>
                <input type="reset" value="重置"/>
                </td>
            </tr>
        </table>
    </form>

success.jsp

<b>注冊成功!</b><br>
    您的注冊信息為:<br/>
    <table align="center">
            <tr>
                <td>用戶名:</td>
                <td><%=request.getParameter("name") %></td>
            </tr>
            <tr>
                <td>密碼:</td>
                <td><%=request.getParameter("pwd") %></td>
            </tr>
            <tr>
                <td>性別:</td>
                <td><%=request.getParameter("sex") %></td>
            </tr>
            <tr>
                <td>年齡:</td>
                <td><%=request.getParameter("age") %></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><%=request.getParameter("email") %></td>
            </tr>
            <tr>
            <td><a href="index.jsp">【回到主頁】</a></td></tr>
    </table>
捕獲.PNG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容