2020-11-05黄其威

知识总结:

package com.itlaobing.dbhelper;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

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
        String sql = "INSERT INTO t_user(username,`pasWord`,gender)VALUES(?,?,?)";
        //4
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        //给占位符赋值
        prepareStatement.setObject(1,username);
        prepareStatement.setObject(2,password);
        prepareStatement.setObject(3,gender);
        //5
        int i = prepareStatement.executeUpdate();
//      
//      if(i > 0) {
//          System.out.println("添加成功");
//      }
        //7
        prepareStatement.close();
        connection.close();
        //方法的返回值
        return i;
    }
}





<%@page import="com.itlaobing.dbhelper.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");
    System.out.println(password);
    String gender = request.getParameter("gender");
    //3、数据校验
    //4、调用数据库的添加方法  //
    DBHelper dbhelper = new DBHelper();
    int i =dbhelper.save(username,password,gender);
    if(i > 0) {
        request.getSession().setAttribute("msg","注册成功");
        response.sendRedirect("index.jsp");
    }else{
        request.getSession().setAttribute("msg","注册失败");
        response.sendRedirect("index.jsp");
    }

%>





<%@ 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>
        <!--表单-->
        <!--
        action="服务器路径"
        method="提交方式"(get/post)  
           enctype="上传文件形式"   -->
        <form action="doregister.jsp" method="post"  >
            用户名<input type="text" name="username" id="username"  placeholder="快点给爷输入" />
            <br>
            密&nbsp;&nbsp;&nbsp;码<input type="password"  name="password" id="username"  placeholder="快点给爷输入" /><br />
            性别:<input type="radio" name="gender" value="男"/> 男
                <input type="radio" name="gender" value="女"/>女<br />
            <!--    籍贯<select name="native">-->
                    <!--<option  value="福州">福州
                    </option>
                自我介绍<textarea-->
                    <!--style="resize:none;" rows="8" cols="80">
                    </textarea> -->
                <input type="submit" value="冲冲冲" />
                <!--<input type="submit"value="出来了" />
                <input type="reset" value="贤者模式" />-->
                
        </form>
        
    </body>
</html>
    

心得体会:通过这两周的实训,我对静态页面的组成有了很大的认知,还有数据库的编写与操作,
都让我受益匪浅,老师讲的深入浅出,很容易让人明白,实训过后获得了很多东西,尽管过程艰难~

标签

评论

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