Adding to the student information database

1. questions asked

 

 

2. Results and Source Code

1) file structure:

 

 

2) Add information interface stuinfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>当前位置:添加学生信息</title>
</head>
<body>
<form action="AddServlet" method="post"> 

<table    align="center" width="600px"  >
<tr>
<td align="center">登录账号:</td>
<td><input type="text" name="uusername"></td>
</tr>

<tr>
<td align="center">登录密码:</td>
<td><input type="password"  name="upwd"></td>
</tr>


<tr>
<td align="center">性别:</td>
<td>
<select   name="usex">
<option></option>
<option></option>
</select> </td>
</tr>

<tr>
<td align="center">姓名:</td>
<td><input type="text"  name="uname"></td>
</tr>


<tr>
<td align="center">学号:</td>
<td><input type="text"   name="ustuid"></td>
</tr>



<tr>
<td align="center">电子邮件:</td>
<td><input type="text"  name="uemail"></td>
</tr>
<tr>
<td align="center">所在学院:</td>
<td><input type="text"   name="uxy"></td>
</tr>


<tr>
<td align="center">所在系:</td>
<td><input type="text"   name="ux"></td>
</tr>
<tr>
<td align="center">所在班级:</td>
<td><input type="text"   name="ubj"></td>
</tr>


<tr>
<td align="center">入学年份(届):</td>
<td>
<select   name="uyear">
<option> 2019</option>
<option> 2018</option>
<option> 2017</option>
<option> 2016</option>
<option>2014</option>
<option>2013</option>
</select> </td>
</tr>    

<tr>
<td align="center">生源地:</td>
<td><input type="text"   name="uwhere"></td>
</tr>

<tr>
<td align="center" >备注</td>
<td><input type="text"   name="ubeizhu"></td>
</tr>

<tr>
<td align="center"><input type="submit" value="添加"> </td>
</tr>
</table>
</form>
</body>
</html>

 

3) user attributes User.java

package service;

public class User {

    private String zh;
    private String pwd;
    private String sex;
    private String name;
    private String stuid;
    private String email;
    private String xueyuan;
    private String xi;
    private String bj;
    private String enter;
    private String wher;
    private String beizhu;
    public String getZh() {
        return zh;
    }
    public void setZh(String zh) {
        this.zh = zh;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getStuid() {
        return stuid;
    }
    public void setStuid(String stuid) {
        this.stuid = stuid;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getXueyuan() {
        return xueyuan;
    }
    public void setXueyuan(String xueyuan) {
        this.xueyuan = xueyuan;
    }
    public String getXi() {
        return xi;
    }
    public void setXi(String xi) {
        this.xi = xi;
    }
    public String getBj() {
        return bj;
    }
    public void setBj(String bj) {
        this.bj = bj;
    }
    public String getEnter() {
        return enter;
    }
    public void setEnter(String enter) {
        this.enter = enter;
    }
    public String getWher() {
        return wher;
    }
    public void setWher(String wher) {
        this.wher = wher;
    }
    public String getBeizhu() {
        return beizhu;
    }
    public void setBeizhu(String beizhu) {
        this.beizhu = beizhu;
    }
    
    
    
    
}

4) access to the database link DBUtil.java

Package util; 

Import the java.sql.Connection;
 Import the java.sql.DriverManager;
 Import java.sql.PreparedStatement;
 Import the java.sql.ResultSet;
 Import java.sql.SQLException; 

public  class the DBUtil { 
    

    // database connection address 
    Private  static String = the URL of "jdbc: MySQL: // localhost: 3306 / = false & serverTimezone the Test useSSL = UTC?" ; 

    // database user name 
    Private  static String userName = "root" ;
     // database password 
    Private  static String password = "18735" ; 

    public  staticThe getConnection Connection () { 
        Connection Conn = null ; 

        the try { 

            the Class.forName ( "com.mysql.cj.jdbc.Driver"); // load drivers 

            System.out.println ( "load success driving !!!" ); 
        } the catch (a ClassNotFoundException E) {
             // the TODO: Exception handle 
            e.printStackTrace (); 
        } 

        the try { 

            // specified by three parameters getConenction DriverManager class method, the database is connected 
            Conn = the DriverManager.getConnection (the URL, UserName, Password); 
            the System .out.println ( "database connection success!" );

            // return a connection object
             // return Conn; 

        } the catch (SQLException E) {
             // the TODO: Exception handle 
            e.printStackTrace (); 
        } 
        return Conn; 
    } 
    public  static  void main (String [] args) throws SQLException {   // Test if the database communication 
        Connection Conn = the getConnection (); 
        the PreparedStatement pstmt = null ; 
        the ResultSet RS = null ; 
        String SQL = "SELECT * from stuInfo" ;
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        System.out.println(getConnection());
        while(rs.next()){
            System.out.println("空");
        }

        }


}

 

5) Data processing interface, submit data to the database UserDao.java

package dao;

import java.sql.*;

import service.User;
import util.DBUtil;

public class UserDao {
    public boolean addx(User user) {
        //创建数据库连接
        String sql="insert into stuinfo(zh,pwd,sex,name,stuid,email,xueyuan,xi,bj,enter,diqu,beizhu)values('"+user.getZh()+"','"+user.getPwd()+"','"+user.getSex()+"','"+user.getName()+"','"+user.getStuid()+"','"+user.getEmail()+"',"
                + "'"+user.getXueyuan()+"','"+user.getXi()+"','"+user.getBj()+"','"+user.getEnter()+"','"+user.getWher()+"','"+user.getBeizhu()+"')";
        Connection conn = DBUtil.getConnection();        
        Statement state = null;        
        try {
            state = conn.createStatement();
            int a= state.executeUpdate(sql);
            state.close();
            return a>0?true:false;
        } catch (Exception e) {
            e.printStackTrace();
        }   
        return false;
    }

}

6) connection to the server, interactive view and modify data AddServlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;

import service.User;

/**
 * Servlet implementation class AddServlet
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        doGet(request, response);

        String zh = request.getParameter("uusername");
        System.out.println(zh);
        String pwd = request.getParameter("upwd");
        String email = request.getParameter("uemail");
        String name = request.getParameter("uname");
        String sex = request.getParameter("usex");
        String where = request.getParameter("uwhere");
        String enter = request.getParameter("uyear");
        String stuid = request.getParameter("ustuid");
        String xy = request.getParameter("uxy");
        String xi = request.getParameter("ux");
        String bj = request.getParameter("ubj");
        String beizhu = request.getParameter("ubeizhu");
        Integer kpeople = Integer.valueOf(request.getParameter("ustuid"));

        //int s=Integer.parseInt(stuid);
        // 登录账号
        String a = "^[a-zA-Z][a-zA-Z0-9]{6,12}$";
        // E-mail

        String d = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";

//
        String b = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,12}$";
        if (!zh.matches(a)) { System.out.println(zh); PrintWriter out =
          response.getWriter(); out.print(
          "<script>alert('用户名有误');window.location.href='stuInfo.jsp'</script>");
          }
          
          else
         if (!pwd.matches(b)) {
            PrintWriter out = response.getWriter();
            out.print("<script>alert('密码有误!');window.location.href='stuInfo.jsp'</script>");

        } 
        else if (!email.matches(d)) {
            PrintWriter out = response.getWriter();
            out.print("<script>alert('邮箱有误!');window.location.href='stuInfo.jsp'</script>");
        } 
        else if(stuid.length()!=8||kpeople<20180000||kpeople>20189999)
        {  PrintWriter out = response.getWriter();
            out.print("<script>alert('学号有误!');window.location.href='stuInfo.jsp'</script>");
        }
        else {
            User user = new User();
            user.setZh(zh);
            user.setPwd(pwd);
            user.setName(name);
            user.setSex(sex);
            user.setWher(where);
            user.setXueyuan(xy);
            user.setEmail(email);
            user.setXi(xi);
            user.setStuid (stuid);
            user.setBj(bj);
            user.setEnter(enter);
            user.setBj(bj);
            user.setBeizhu(beizhu);
            System.out.println(zh);
            UserDao user1 = new UserDao();
            user1.addx(user);
            PrintWriter out = response.getWriter();
            out.print("<script>alert('注册成功!');</script>");

        }
    }
}

7) the volume table of test results

 

 

 

 

 

 

 

 

 

 

 

 3. learnings

The operation of the database it was a white, after reporting interface complete information, and then connect to the database (success), but finally lost in how data can not reach the database. Physically and mentally fatigued after investigation of more than two hours can be considered to take shape:

1) the database has a column name and keyword repetition, to know not just named

2) sql statements before and after the entry does not match

3) sql statement I do not know when to have a space (like this !!!!!), and finally re-written once before they succeed, hey, only a success or failure.

Have to say, " seems unusual is the most strange, such as the Hard Way " decade sword, exactly when we can iron pillar ground into a needle? !

 

Guess you like

Origin www.cnblogs.com/ywqtro/p/11715657.html