数据库工具类JdbcConnectionUtils
public class JdbcConnectionUtils {
String encoding = "";
String names = "";
String url = "jdbc:mysql://localhost:3306/test2?characterEncoding=UTF-8&&useUnicode=true";
String user = "root";
String password = "lizhi123";
Connection conn= null;
public Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return conn;
}
public void closeAll(Connection conn,PreparedStatement pst) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pst != null) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
RegistServlet
@WebServlet(name="RegistServlet",urlPatterns = {"/register"})
public class RegistServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 接收注册页面的值
String name = req.getParameter("username");
String password = req.getParameter("password");
String sex = req.getParameter("sex");
String hiddenbutton = req.getParameter("button");
if(hiddenbutton.equals("add")) {
System.out.println("add");
}else if(hiddenbutton.equals("up")) {
System.out.println("up");
}
//获取多个爱好
String[] hobbits = req.getParameterValues("hobbit");
String love = "";
for(String hobbit : hobbits) {
System.out.println("爱好有:"+hobbit);
love+=hobbit;
}
//获取页面多个请求参数的值
Enumeration<String> enumerations = req.getParameterNames();
while (enumerations.hasMoreElements()) {
System.out.println(enumerations.nextElement());
}
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer = resp.getWriter();
// writer.print(name);
//保存到数据库
JdbcConnectionUtils jdbc = new JdbcConnectionUtils();
Connection conn = jdbc.getConnection();
System.out.println(conn);
PreparedStatement pst = null;
//sql语句
String sql = "insert into stu(username,password,sex,hobbit)values(?,?,?,?)";
//pst
try {
pst = conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, password);
pst.setString(3, sex);
pst.setString(4, love);
int rows = pst.executeUpdate();
if(rows>0) {
writer.print("添加成功");
}else {
writer.print("添加失败");
}
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
jdbc.closeAll(conn, pst);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//处理post方式提交乱码数据
req.setCharacterEncoding("UTF-8");
doGet(req, resp);
}
}
注册页面register.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成都文理注册页面</title>
</head>
<body>
<center>
<form action="register" method="get">
<div style="background-color: gray; width: 60%; height: 400px">
<p>
用户名:<input type="text" name="username" value="" />
</p>
<p>
密码:<input type="password" name="password" value="" />
</p>
<p>
唱歌:<input type="checkbox" name="hobbit" value="唱歌" />
跳舞:<input type="checkbox" name="hobbit" value="跳舞" />
</p>
<p>
男:<input type="radio" name="sex" value="男" />
女:<input type="radio" name="sex" value="女" />
</p>
<p>
<input type="hidden" name="button" value="add">
</p>
<p>
<input type="submit" value="注册">
</p>
</div>
</form>
</center>
</body>
</html>
数据库字段
成功视频
20230309_155423