- userbean.java
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;
}
}
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$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
<%@ page import="com.media.bean.UserBean" %>
<%@ 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="com.media.bean.UserBean"></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 sqL = "insert into user(username,password,gender , age) values ('" +user . getUsername()+"','"+user . getPassword()+"',1,22)";
//String sqL ="update user set status = 3 where id = 12";
//String sql = "delete from user where id = 12";
int count = stmt.executeUpdate (sqL);
if(count > 0){
out . print("注册成功");
}else{
out. print("注册失败");
}
%>
-运行结果