欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

9.30课堂作业

程序员文章站 2022-05-12 12:29:49
...

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>

register.jsp

<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="yjx.usebean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>

<%
    Class.forName("com.mysql.jdbc.Driver");

    String url ="jdbc:mysql://localhost:3306/book";
    Connection connection = DriverManager.getConnection(url,"root","root");

    Statement stmt = connection.createStatement();

    String  = "insert into user(username,password,gender , age) values (" +user . getUsername()+",”+user . getPassword()+", 1, 22)";
    int count = stmt . executeUpdate (sqL);

    if(count > 0){
    out . print("注册成功");
    }else{
    out. print("注册失败");
    }
%>

usebean.java

package yjx;

public class usebean {

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

运行结果:
9.30课堂作业
9.30课堂作业