1.先创建文件index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<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>
2.再创建数据库UserBeen
<%@ page import="java.sql.Connection" %>
<%@ page import="com.media.been.UserBean" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %><%--
Created by IntelliJ IDEA.
User: j
Date: 2020/9/23
Time: 9:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="user" class="com.media.been.UserBean"></jsp:useBean>
<jsp:setProperty name="user" property="*"></jsp:setProperty>
<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:10.3.22.7/book";
Connection connection = DriverManager.getConnection(url,"root","root");
Statement stmt = connection.createStatement();
String sql= "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("注册失败");
}
%>
3.创建register.jsp
package com.media;
public class UserBean {
private String username;
private String password;
private String gender;
private int age;
private int status;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String 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;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
4.运行结果