2020-12-05秀日措

知识总结

用户注册


DBHelper:

package com.laibing.bdhelper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//import java.sql.ResultSet;

public class DBHelper {

    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);
        //给占位符赋值
        prepareStatement.setObject(1,username);
        prepareStatement.setObject(2,password);
        prepareStatement.setObject(3,gender);
        //5执行sql
        int i=prepareStatement.executeUpdate();
        //6
//      while(rs.next() ){
//          System.out.println(rs.getObject("userNAME"));
//          System.out.println(rs.getObject("gender")); 
//      }
//      if(i > 0) {
//          System.out.println("添加成功");
//      }
        //7
        prepareStatement.close();
        connection.close();
        //方法的返回值类型
        return i;
    }
}


register:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    <h1>
        ${msg}
    </h1>
        <table align="center"width="800" height="400"cellpadding="0"border="0">
            <form action="doregister.jsp"  method="post">
                用户名:<input type="text" name="username" id="username" placeholder="请输入用户名"/> <br />
                密&nbsp;&nbsp;&nbsp;码:<input type="paaword" name="password" id="password" placeholder="请输入密码"/><br/>
                性别:<input type="radio" name="gender" value="男"/>男
                    <input type="radio" name="gender" value="女"/>女<br />
                <input type="submit" value="注册" /><br  />
            </form>
        </table>
    </body>
<ml>


doregister:

<%@page import="com.laibing.bdhelper.DBHelper"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
         //1、设置字符编码
         request.setCharacterEncoding("UTF-8");
         response.setCharacterEncoding("UTF-8");
         //2、获取表单数据
         String username=request.getParameter("username");
         String password=request.getParameter("password");
         String gennder=request.getParameter("gennder");
         //3、数据校验
         //4、调用数据库的添加方法
         DBHelper bdhelper=new DBHelper();
        int i=bdhelper.save(username,password,gennder);
        //5处理结果
          if(i > 0) {
                request.getSession().setAttribute("msg","添加成功");
                response.sendRedirect("register.jsp");
            }

%>


学习心得

今天下午主要学习了用户注册想,服务器与客户端之间的连接,虽然内容不

多,但是由于一开始讲的有点快,所以没太跟上老师的进度,但好在给了时间

去练习,通过请教同学和老师的指导,最后结果也出来了,总的来说,这两周

时间虽然很短,在学习过程中很多时候都处于懵逼状态,但是收获也很多。

标签

评论

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1