Javaweb编程

  首先是题目要求:

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

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

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

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

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

  5电子邮箱:要求判断正确格式[email protected];(1分)

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

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

 

  然后附上代码

  我是分为以下几个部分:

  一、界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>

</head>
<body>
    <div align="center">
        
        <div class="a">
            <a href="add.jsp">信息添加</a>
        </div>

    </div>
</body>
</html>
<%@ 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>Insert title here</title>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: red;">添加学生信息</h1>
        <form action="CourseServlet?method=add" method="post" onsubmit="return check()">
            <div class="a">
                
                登录账号:<input type="text" id="zhanghao" name="zhanghao"/>
            </div>
            <div class="a">
                
            登录密码    :<input type="password" id="mima" name="mima"/>
            </div>
            <div class="a">
                
                性别:<input type="radio" name="sex" id="sex" value="男"><input type="radio" name="sex" id="sex" value="女"></div>
            <div class="a">
                
                姓名:<input type="text" id="name" name="name" />
            </div>
            <div class="a">
                
                学号:<input type="text" id="xuehao" name="xuehao"/>
            </div>
            <div class="a">
                
                电子邮件:<input type="text" id="youxiang" name="youxiang" />
            </div>
            <div class="a">
                
                所在学院:<input type="text" id="school" name="school" />
            </div>
            <div class="a">
                
                所在系:<input type="text" id="xi" name="xi" />
            </div>
            <div class="a">
                
                所在班级:<input type="text" id="classes" name="classes" />
            </div>
            <div class="a">
                
                入学年份(届):<select id="nianfen" name="nianfen">
                         <option value="1997">1997</option>
                        <option value="1998">1998</option>
                         <option value="1999">1999</option>
                         <option value="2000">2000</option>

                  </select></div>
            <div class="a">
                
                生源地<input type="text" id="shengyuandi" name="shengyuandi" />
            </div>
            <div class="a">
                
                备注<input type="text" id="beizhu" name="beizhu"/>
            </div>
            
            
            
            <div class="a">
                <button type="submit" >添加</button>
            </div>
            </form>
             <div class="a">
                
        </div>
      </div>
     
    <script type="text/javascript">
        function check() {
            var zhanghao = document.getElementById("zhanghao");;
            var mima = document.getElementById("mima");
            var sex = document.getElementById("sex");;
            var name = document.getElementById("name");
            var xuehao = document.getElementById("xuehao");;
            var youxiang = document.getElementById("youxiang");
            var school = document.getElementById("school");;
            var xi = document.getElementById("xi");
            var classes = document.getElementById("classes");;
            var nianfen = document.getElementById("nianfen");
            var shengyuandi = document.getElementById("shengyuandi");;
            var beizhu = document.getElementById("beizhu");
            
            
            //非空
             if (!zhanghao.value.match(/^[a-zA-Z]\w{5,11}$/)) {

                   

                    alert("用户名由六到十二英文母.数字和下划线组成,以英文字母开头");

                    

                    zhanghao.focus();

                    return false;

            }
            
        
                else if (!mima.value.match(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}$/)) {

               

                 alert(" 密码由八 位以上 英文 和数 字 组成");

              

                 mima.focus();

                 return false;

         }
            
                
                else if (!xuehao.value.match(/^2018\d{4}$/)) {

             

                 alert(" 学号由八位构成且 前四位为2018");

               

                 xuehao.focus();

                 return false;

         }
            
                 else if (!youxiang.value.match(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/)) {

                 

                 alert(" 邮箱格式错误");

            

                 youxiang.focus();

                 return false;

         } 
                 else    if(sex.value == ‘‘) {
                    alert(‘性别为空‘);
                    sex.focus();
                    return false;
                }
                 else    if(name.value == ‘‘) {
                    alert(‘姓名为空‘);
                    name.focus();
                    return false;
                }
                 else    if(school.value == ‘‘) {
                    alert(‘所在学校为空‘);
                    school.focus();
                    return false;
                }
                 else    if(xi.value == ‘‘) {
                    alert(‘所在系为空‘);
                    xi.focus();
                    return false;
                }
                 else    if(classes.value == ‘‘) {
                    alert(‘所在班级为空‘);
                    classes.focus();
                    return false;
                }
                 else    if(nianfen.value == ‘‘) {
                    alert(‘年份为空‘);
                    nianfen.focus();
                    return false;
                }
                
                 else    if(shengyuandi.value == ‘‘) {
                    alert(‘生源地为空‘);
                    shengyuandi.focus();
                    return false;
                }
                 else    if(beizhu.value == ‘‘) {
                    alert(‘备注为空‘);
                    beizhu.focus();
                    return false;
                }
            
        }
            
        </script>
    
</body>
</html>

在界面的后半部分,加入了对输入的判断,实现题目所给要求。

  二、将数据录入数据库

  dao层:

package dao;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import entity.Course;
import util.DBUtil;

/**
 * 课程Dao
 * Dao层操作数据
 * @author Hu
 *
 */
public class CourseDao {

    /**
     * 添加
     * @param course
     * @return
     */
    public boolean add(Course course) {
        String sql = "insert into course(name, zhanghao, mima, sex, xuehao, youxiang, school, xi, classes, nianfen, shengyuandi, beizhu) values(‘" + course.getName() + "‘,‘" + course.getZhanghao() + "‘,‘" + course.getMima() + "‘,‘" + course.getSex() + "‘,‘" + course.getXuehao() + "‘,‘" + course.getYouxiang() + "‘,‘" + course.getSchool() + "‘,‘" + course.getXi() + "‘,‘" + course.getClasses() + "‘,‘" + course.getNianfen() + "‘,‘" + course.getShengyuandi() + "‘,‘" + course.getBeizhu() + "‘)";
        //创建数据库链接
        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 {
            //关闭连接
            DBUtil.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        return f;
    }
    

}

 

  entity层:

package entity;


public class Course {

    private int id;
    private String name;
    private String zhanghao;
    private String mima;
    private String sex;
    private String xuehao;
    private String youxiang;
    private String school;
    private String xi;
    private String classes;
    private String nianfen;
    private String shengyuandi;
    private String beizhu;
    
    public String getZhanghao() {
        return zhanghao;
    }

    public void setZhanghao(String zhanghao) {
        this.zhanghao = zhanghao;
    }

    public String getMima() {
        return mima;
    }

    public void setMima(String mima) {
        this.mima = mima;
    }

    public String getBeizhu() {
        return beizhu;
    }

    public void setBeizhu(String beizhu) {
        this.beizhu = beizhu;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getXuehao() {
        return xuehao;
    }

    public void setXuehao(String xuehao) {
        this.xuehao = xuehao;
    }

    public String getYouxiang() {
        return youxiang;
    }

    public void setYouxiang(String youxiang) {
        this.youxiang = youxiang;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getXi() {
        return xi;
    }

    public void setXi(String xi) {
        this.xi = xi;
    }

    public String getClasses() {
        return classes;
    }

    public void setClasses(String classes) {
        this.classes = classes;
    }

    public String getNianfen() {
        return nianfen;
    }

    public void setNianfen(String nianfen) {
        this.nianfen = nianfen;
    }

    public String getShengyuandi() {
        return shengyuandi;
    }

    public void setShengyuandi(String shengyuandi) {
        this.shengyuandi = shengyuandi;
    }

    public Course() {}
    
    public Course(int id, String name, String zhanghao, String mima, String sex, String xuehao, String youxiang, String school, String xi, String classes, String nianfen, String shengyuandi, String beizhu) {
        this.id = id;
        this.name = name;
        this.zhanghao = zhanghao;
        this.mima = mima;
        this.sex = sex;
        this.xuehao = xuehao;
        this.youxiang = youxiang;
        this.school = school;
        this.xi = xi;
        this.classes = classes;
        this.nianfen = nianfen;
        this.shengyuandi = shengyuandi;
        this.beizhu = beizhu;
    }
    
    public Course(String name, String zhanghao, String mima, String sex, String xuehao, String youxiang,String school, String xi, String classes, String nianfen, String shengyuandi, String beizhu) {
        this.name = name;
        this.zhanghao = zhanghao;
        this.mima = mima;
        this.sex = sex;
        this.xuehao = xuehao;
        this.youxiang = youxiang;
        this.school = school;
        this.xi = xi;
        this.classes = classes;
        this.nianfen = nianfen;
        this.shengyuandi = shengyuandi;
        this.beizhu = beizhu;
    }
}

 

  servlet层:

package servlet;

import java.io.IOException;
import java.util.List;

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.CourseDao;
import entity.Course;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/CourseServlet")
public class CourseServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
   

        
        CourseDao dao = new CourseDao();
        
        /**
         * 方法选择
         */
        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);
            }
        }

        /**
         * 添加
         * @param req
         * @param resp
         * @throws IOException 
         * @throws ServletException 
         */
        private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.setCharacterEncoding("utf-8");
            //获取数据
            String name = req.getParameter("name");
            String zhanghao = req.getParameter("zhanghao");
            String mima = req.getParameter("mima");
            String sex = req.getParameter("sex");
            String xuehao = req.getParameter("xuehao");
            String youxiang = req.getParameter("youxiang");
            String school = req.getParameter("school");
            String xi = req.getParameter("xi");
            String classes = req.getParameter("classes");
            String nianfen = req.getParameter("nianfen");
            String shengyuandi = req.getParameter("shengyuandi");
            String beizhu = req.getParameter("beizhu");
            
            Course course = new Course(name, zhanghao, mima, sex, xuehao, youxiang, school, xi, classes, nianfen, shengyuandi, beizhu);
            
            //添加后消息显示
            System.out.println("123");
            if(dao.add(course)) {
                req.setAttribute("message", "添加成功");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            } else {
                req.setAttribute("message", "重复,请重新录入");
                req.getRequestDispatcher("add.jsp").forward(req,resp);
            }
        }    
        
        
}

 

  util层:

  

package util;

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/test?useUnicode=true&characterEncoding=UTF-8";
    public static String db_user = "root";
    public static String db_pass = "";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.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 course";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("空");
        }else{
            System.out.println("不空");
        }
    }
}
相关文章
相关标签/搜索