课堂测试web写注册界面

2021-04-30 17:26

阅读:573

YPE html>

标签:插入数据   style   serial   har   getname   catch   lag   system   width   

一、题目:WEB界面链接数据库(未完全完成)

1.考试要求:

 技术图片

 

 

1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)

2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)

3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)

4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)

5姓名:输入自己的姓名;

5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(1分)

6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)

7可以演示连接上数据库。(2分)

二、设计思路:

1、首先在users.jsp代码中,用HTML写一个添加表单即可

2、在DBUtil.java文件编写中对数据库进行连接和关闭的操作

3、在Dao.java文件中编写向数据库中插入数据的SQL语句,以及判断Servelet.java中传递的数据是否为空。

4、在 users.jsp 文件中对表单用javascript进行form表单校验,将满足所有添加要求的数据传入Servelet.java中,用post方式传递。

5、在Selvlet.java 文件调用DBUtil.java文件和Dao.java文件,得到Dao.java中判断Servelet.java中传递的数据是否为空的函数,并进行二次表单校验。

6、Servelet.java中若数据合理,调用DBUtil.java的方法,连接数据库将数据通过参数的形式传递给在Dao.java文件中向数据库中插入数据的SQL语句,将数据添加到数据库。若添加成功,给出提示信息并返回users.jsp代码中action跳转的Servlet?method=add页面。若添加失败,给出提示信息并返回users.jsp代码中action跳转的Servlet?method=add页面。

 1.

Dao.java

package Dao;

import java.sql.Connection;
import java.sql.Statement;

import DBUtil.DBUtil;

import Entity.User;

public class Dao {

    public boolean add(User user) {
        //将数据插入数据库的SQL语句
        String sql = "insert into uses1 (username,password,sex,name,stuname,email,xueyuan,xi,banji,year,address,addtext) values(‘"+ user.getUsername() + "‘,‘"+ user.getPassword() +"‘,‘"+ user.getSex() +"‘,‘"+user.getName() +"‘,‘"+ user.getStuname() +"‘,‘"+user.getEmail()+"‘,‘"+ user.getXueyuan() +"‘,‘"+user.getXi()+"‘,‘"+user.getBanji()+"‘,‘"+user.getYear()+"‘,‘"+user.getAddress()+"‘,‘"+user.getAddtext()+"‘)";
        // 创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
      
        try {
            state = conn.createStatement();
            a=state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭z 连接
            DBUtil.close(state, conn);
        }
   
        if (a > 0) {
            f = true;
        }
        return f;

}


}

2、DBUtil.java文件

package DBUtil;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/user?serverTimezone=UTC";//登录数据库
    public static String db_user = "root";
    public static String db_pass = "123";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
     /**
         * 关闭与数据库的连接
     * @param state
     * @param conn
     */
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from users";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("空");
        }else{
            System.out.println("不空");
        }
    }
}

3、User.java(用户属性类)文件

package Entity;

public class User {
    
    private String username;
    private String password;
    private String sex;
    private String name;
    private String stuname;
    private String email;
    private String xueyuan;
    private String xi;
    private String banji;
    private String year;
    private String address;
    private String addtext;
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    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 getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    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 getBanji() {
        return banji;
    }

    public void setBanji(String banji) {
        this.banji = banji;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddtext() {
        return addtext;
    }

    public void setAddtext(String addtext) {
        this.addtext = addtext;
    }

    public User() {}
    
public User(String username,String password,String sex,String name,String stuname,String email,String xueyuan, String xi,String banji,String year,String address,String addtext) {
        
        
        this.username=username;
        this.password=password;
        this.sex=sex;
        this.name=name;
        this.stuname=stuname;
        this.email=email;
        this.xueyuan=xueyuan;
        this.xi=xi;
        this.banji=banji;
        this.year=year;
        this.address=address;
        this.addtext=addtext;
        
    }
    

}

4、Servlet.java文件

package Servlet;

import java.io.IOException;

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.Dao;

import Entity.User;




@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public Servlet() {
        super();
        
    }
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            add(req, resp);
        } 
    }
    
    
    public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String name = req.getParameter("name");
        String stuname = req.getParameter("stuname");
        String email = req.getParameter("email");
        String xueyuan = req.getParameter("xueyuan");
        String xi= req.getParameter("xi");
        String banji= req.getParameter("banji");
        String year= req.getParameter("year");
        String address= req.getParameter("address");
        String addtext= req.getParameter("addtext");
        //调用用户属性类
        User user = new User(username,password,sex,name,stuname,email,xueyuan,xi,banji,year,address,addtext);
        //接受判断函数返回值
        Dao dao =new Dao();
        boolean f=dao.add(user);
        //提示信息
        if(f) {
            req.setAttribute("message", "注册成功!");
            req.getRequestDispatcher("users.jsp").forward(req,resp);
        } 
        else {
            req.setAttribute("message", "已有账号,重复登录!");
            req.getRequestDispatcher("users.jsp").forward(req,resp);
        }
    }
}

5、users.jsp文件

    pageEncoding="UTF-8"%>Insert title here
style="width: 50%;margin:auto;background:rgb(195,195,195)" bordercolor="red" > onsubmit="return check()">
当前位置:添加学生信息
登录账号:
登录密码:
性别:
姓名:
学号:
电子邮箱:
所在学院:
所在系:
所在班级:
入学年份(届):
生源地:
备注:

课堂测试web写注册界面

标签:插入数据   style   serial   har   getname   catch   lag   system   width   

原文地址:https://www.cnblogs.com/xk1013/p/12152102.html


评论


亲,登录后才可以留言!