首先是题目要求:

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

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

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

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

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

  5电子邮箱:要求判断正确格式xxxx@xxxx.xxxx;(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("不空");
}
}
}

Javaweb编程的更多相关文章

  1. Javaweb编程中的乱码问题

    程序中的乱码问题,主要出现在我们处理中文数据的过程中出现.从浏览器向服务器请求数据,服务器返回的数据在浏览器中显示为乱码.或者是服务器中的java文件用到中文,也有可能会出现乱码.数据库在处理数据的时 ...

  2. 基于maven javaweb编程缺少java源文件

    前提:基于maven的javaweb的开发环境,可参考 link 一. https://blog.csdn.net/ldlly0505/article/details/79674826 1.在sour ...

  3. JavaWeb王者归来学习笔记

    JavaWeb王者归来学习笔记... Ajax王者归来: Ajax:Asynchronous JavaScript And XML -------Ajax 是一些旧有技术的结合.但却以一种新的姿态.披 ...

  4. 【JavaWeb】从零实现用户登录

    1.数据库预备 1.1 SQL 创建数据库 create database db; 创建表 create table userInfo( id int primary key , name ), pa ...

  5. Javaweb开发之路

    本文作者:DavidLin 欢迎转载,但请保留文章原始出处→_→ 本文地址:http://www.cnblogs.com/univeryinli/p/programming-skill-yinli.h ...

  6. JavaWeb错误处理集锦

    一:起因 (1)自己接下来想走算法的路子,打算把十大算法和数学模型学习一下,算是给自己之前 JavaWeb 的一个总结: (2)记得Java算是第一个比較上手的语言了,更是用JavaWeb走过了非常长 ...

  7. Java Web学习路线

    2016-08-22的早上,本是一个很平静的早上,坐在去往公司的公交车上想到了很多之前上学时的点点滴滴,回想起来还真的是耐人寻味啊,当初青春的懵懂,当初的冲动,当初的做事不考虑后果! 也正是这耐人寻味 ...

  8. 使用JDBC对数据库进行查询的前期准备工作,以及简单的JDBC访问MySQL数据库(Mac)

    首先JDBC的前期数据库数据准备: 1,打开链接好MySQL的Workbench软件,新建一个数据库: 2.然后打开数据库对应的代码编辑窗口,建立表和插入数据记录: 3.然后是打开关于javaWeb编 ...

  9. javascript笔记5-BOM

    Javascript应用的平台很多,不仅仅针对Web.在Web中使用Javascript,BOM(browser object model,浏览器对象模型)是核心. BOM提供了很多对象,用于访问浏览 ...

随机推荐

  1. CF580D_Kefa and Dishes

    D. Kefa and Dishes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. [洛谷P3403] 跳楼机

    题目传送门 套路题,同余最短路. 先只考虑y.z进行连边,再在mod x的意义下进行计算. 这里的“距离”dis[i]指的是,在所有满足a mod x=i的a里,能到达的最小的a是多少. 显然只要能到 ...

  3. 吴裕雄--天生自然python编程:实例(1)

    str = "www.runoob.com" print(str.upper()) # 把所有字符中的小写字母转换成大写字母 print(str.lower()) # 把所有字符中 ...

  4. Android中的AlertDialog和ProgressDialog用法

    手机APP对话框是很多APP都有的下面来看下怎么实现的吧, 打开Android studio 然他自动创建好布局和类; 下面我们修改activity_main.xml中的代码 <?xml ver ...

  5. Django ORM必会13条之外的查询方法

    基于双下划线的查询 # 价格 大于 小于 大于等于 小于等于 filter(price__gt=') # 筛选出大于90 filter(price__lt=') # 筛选出小于90 filter(pr ...

  6. [转]win7 64位下完美安装64位oracle 11g

    最近在网上搜如何在win764位的情况下安装64位的oracle,并且使用PLSQL Developer来管理oracle. 于是开始在oracle官网下载数据库,下载是一件很简单的事情,问题是在百度 ...

  7. 自研接口测试平台(Django2+Bootstrap3+Unittest)

    自研接口测试平台(Django2+Bootstrap3+Unittest) HttpRuleTest是一款面向 HTTP 协议的通用测试框架,只需web端维护接口测试数据,即可实现自动化测试.监控.数 ...

  8. Leetcode-Day Three

    1002. Find Common Characters Given an array A of strings made only from lowercase letters, return a ...

  9. 关于css 的垂直居中

    对于元素的水平居中,我根据我自己之前的一些学习来进行一些总结,如果有不对的地方,欢迎指正~ 一.让大小不固定的元素垂直居中 因为:表格的单元格的特别属性:垂直居中等: `div.parent { di ...

  10. 面向web前端及node开发人员的vim配置

    鉴于 window 下基本用不到 vim,所以下面内容不再提及 window,具体可以在相应 github 中查看手册操作基础:已装有上有 nodejs(npm).没装的可以移步官网:https:// ...