首先是题目要求:
1登录账号:要求由6到12位字母、数字、下划线组成,只有字母可以开头;(1分)
2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母、数字组成。(1分)
3性别:要求用单选框或下拉框实现,选项只有“男”或“女”;(1分)
4学号:要求八位数字组成,前四位为“2018”开头,输入自己学号;(1分)
5姓名:输入自己的姓名;
6点击“添加”按钮,将学生个人信息存储到数据库中。(3分)
7可以演示连接上数据库。(2分)
然后附上代码
我是分为以下几个部分:
一、界面:
首页 Insert title here添加学生信息
1997
1998
1999
2000
届添加
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;
}
}
在界面的后半部分,加入了对输入的判断,实现题目所给要求。
二、将数据录入数据库
dao层:
packagedao;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;importentity.Course;importutil.DBUtil;/*** 课程Dao
* Dao层操作数据
*@authorHu
**/
public classCourseDao {/*** 添加
*@paramcourse
*@return
*/
public booleanadd(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;
}returnf;
}
}
entity层:
packageentity;public classCourse {private intid;privateString name;privateString zhanghao;privateString mima;privateString sex;privateString xuehao;privateString youxiang;privateString school;privateString xi;privateString classes;privateString nianfen;privateString shengyuandi;privateString beizhu;publicString getZhanghao() {returnzhanghao;
}public voidsetZhanghao(String zhanghao) {this.zhanghao =zhanghao;
}publicString getMima() {returnmima;
}public voidsetMima(String mima) {this.mima =mima;
}publicString getBeizhu() {returnbeizhu;
}public voidsetBeizhu(String beizhu) {this.beizhu =beizhu;
}public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}publicString getXuehao() {returnxuehao;
}public voidsetXuehao(String xuehao) {this.xuehao =xuehao;
}publicString getYouxiang() {returnyouxiang;
}public voidsetYouxiang(String youxiang) {this.youxiang =youxiang;
}publicString getSchool() {returnschool;
}public voidsetSchool(String school) {this.school =school;
}publicString getXi() {returnxi;
}public voidsetXi(String xi) {this.xi =xi;
}publicString getClasses() {returnclasses;
}public voidsetClasses(String classes) {this.classes =classes;
}publicString getNianfen() {returnnianfen;
}public voidsetNianfen(String nianfen) {this.nianfen =nianfen;
}publicString getShengyuandi() {returnshengyuandi;
}public voidsetShengyuandi(String shengyuandi) {this.shengyuandi =shengyuandi;
}publicCourse() {}public Course(intid, 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;
}publicCourse(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层:
packageservlet;importjava.io.IOException;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importdao.CourseDao;importentity.Course;/*** Servlet implementation class CourseServlet*/@WebServlet("/CourseServlet")public class CourseServlet extendsHttpServlet {private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicCourseServlet() {super();//TODO Auto-generated constructor stub
}
CourseDao dao= newCourseDao();/*** 方法选择*/
protected void service(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {
req.setCharacterEncoding("utf-8");
String method= req.getParameter("method");if ("add".equals(method)) {
add(req, resp);
}
}/*** 添加
*@paramreq
*@paramresp
*@throwsIOException
*@throwsServletException*/
private void add(HttpServletRequest req, HttpServletResponse resp) throwsIOException, 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= newCourse(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层:
packageutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;/*** 数据库连接工具
*@authorHu
**/
public classDBUtil {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 staticConnection 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();
}returnconn;
}/*** 关闭连接
*@paramstate
*@paramconn*/
public static voidclose (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 voidclose (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) throwsSQLException {
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("不空");
}
}
}