201819102026陈羡熙

注册

user代码

package com.media.bean;

public class User {
    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;
    }
}

index代码

<html>
<head>
    <title>注册</title>
</head>
<body>
<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>
</body>
</html>

register代码

<jsp:useBean id="user" class="com.media.bean.User" ></jsp:useBean>

<jsp:setProperty name="user" property="*"></jsp:setProperty>


<%

    //加载驱动程序
    Class.forName("com.mysql.jdbc.Driver");

    //建立数据库连接
    String url ="jdbc:mysql://127.0.0.1:3306/book?user=root&password=root";//book 为数据库名

    Connection connection = DriverManager.getConnection(url);//获得连接对象 alt+ 回车 自动导入类

    //执行sql语句
    String sql ="insert into user(username,password,gender,age) values('"+user.getUsername()+"','"+user.getPassword()+"',1,23)";

    Statement stmt = connection.createStatement();

    int count = stmt.executeUpdate(sql);

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

    }
%>
<jsp:getProperty name="user" property="username"/>

在这里插入图片描述
在这里插入图片描述

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页