第五次作業

月亮滿了.發表於2020-10-08

index.jsp

<form method="post" action="register.jsp">
    使用者名稱:<input type="text" name="username"><br>
    密碼: <input type="text" name="password"><br>
    性別: <input type="text" name="gender"><br>
    年齡: <input type="text" name="age"><br>
    <button type="submit">註冊</button>
</form>

UserBean.jsp

package com.media.bean;

public class UserBean {
    private String username;
    private String password;
    private int gender;
    private int age;
    private int status;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }
}

register.jsp

<%
    UserBean user = new UserBean();
    user.setUsername(request.getParameter("username"));
    user.setPassword(request.getParameter("password"));
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";
    Connection connection = DriverManager.getConnection(url);
    Statement stmt = connection.createStatement();
    String sql = "insert into user (username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";
    //String sql = "update user set status = 3 where id = 2";
    //String sql = "delete from user where id = 4";
    int count = stmt.executeUpdate(sql);
    if(count == 0 ){
        out.print("註冊失敗");
    }else{
        out.print("註冊成功");
    }
%

在這裡插入圖片描述
index

<form action="home.jsp " method="post">
    <input type ="text " name="username">
    <input type ="text " name="password">
    <button type ="submit ">提交</button>
</form>

home.jsp

<%
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/book";
    Connection connection = DriverManager.getConnection(url,"root","root");
    String sql = "select * from user where username = ? and password = ?";
    PreparedStatement ps = connection.prepareStatement(sql);
    ps.setString(1,request.getParameter("username"));
    ps.setString(2,request.getParameter("password"));
    ResultSet rs = ps.executeQuery();
    while (rs.next()){
        out.print(rs.getString("username")+"-"+rs.getString(3)+"<br>");
    }

    /*
    Statement stmt = connection.createStatement();
    String sql = "select * from user";
    ResultSet rs = stmt.executeQuery(sql);
    while (rs.next()){
        out.print(rs.getString("username")+"-"+rs.getNString(3)+"<br>");
    }
     */
%>

在這裡插入圖片描述
在這裡插入圖片描述