2020-12-05解玲安

一、创建rejiester.jsp文件,将表单文件html中的代码复制到文件中

<%@ page language="java" contentType="text/html; charset=UTF-8"
<form action="dorejiester.jsp" method=" post" >
用户名:<input type="text" name="username" placeholder="请输入用户名" />
密&nbsp;码:<input type="password" name="password" placeholder="请输入密码"/>
性别:<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女" />女
<input type="submit" value="注册" />

二、创建dorejiester.jsp文件
<%
//1、设置字符编码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//2、获取表单数据
String username= request.getParameter("username");

String password= request.getParameter("password");
String gender= request.getParameter("gender");

//3、调用数据库添加语句
DBHelp dbhple = new DBHelp();
int i =dbhple.save(username,password,gender);
//4、处理返回结果
if(i > 0){
request.getSession().setAttribute("msg", "注册成功");
response.sendRedirect("rejiester.jsp");
}else{
request.getSession().setAttribute("msg", "注册失败");
response.sendRedirect("rejiester.jsp");
}
%>
三、将之前创建的DBHelp与dorejiester.jsp连接
public int save(String userName,String passWord,String gender ) throws Exception {
//1加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2创建数据库链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_user","root","");
//3拼写sql语句
String sql = "INSERT INTO t_user (userName,passWord,gender)VALUES(?,?,?)";
//4发送sql
PreparedStatement prepareStatement = connection.prepareStatement(sql);
//5给占位符赋值
prepareStatement.setObject(1, userName);
prepareStatement.setObject(2, passWord);
prepareStatement.setObject(3, gender);
//6执行ql
int i = prepareStatement.executeUpdate();
//7关闭资源
prepareStatement.close();
connection.close();
//返回值
return i;

学习心得:
今天的学习内容对我来说有难度,讲的大概的框架和条理能听懂,但在实际上手的过程中还是错误频出,有点吃力。
两周的学习接近尾声,在这段时间里我学到了很多东西,不同于以往的实践,这次让我收获颇多,很充实,也很感谢老师在这段时间的辛苦教学。

标签

评论

this is is footer