一.基本需求
二.设计思路:
1.首先创建一个与数据库数据属性对应的类User,并添加get和set方法。
2.之后建立另一个类UserDao用于生成一条完整的数据对象。
3.再建立一个类DButil用于与数据库的连接
4.最后建立一个类AddUserServlet用于对数据库的内容进行添加操作
5.同时还要写一个JSP文件完成界面的呈现和细节要求的实现
三.源代码:
1.User类:
package bean;
public class User {
private int id;
private String username;
private String password;
private String name;
private String sex;
private String num;
private String xueyuan;
private String mail;
private String xi;
private String classes;
private String year;
private String home;
private String other;
public User() {
super();
}
public User(int id, String username,String num, String password,String name,String sex,String mail,String xueyuan,String xi,String classes,String year,String home,String other) {
super();
this.id = id;
this.username = username;
this.password = password;
this.name = name;
this.sex = sex;
this.num =num;
this.xueyuan = xueyuan;
this.classes=classes;
this.home=home;
this.other=other;
this.xi=xi;
this.mail = mail;
this.year=year;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 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 getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getXueyuan() {
return xueyuan;
}
public void setXueyuan(String xueyuan) {
this.xueyuan = xueyuan;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
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 getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
public String toString() {
return "User [id=" + id + ", userName=" + username + ", password=" + password + "]";
}
}
2.UserDao类:
package dao;
import bean.User;
import util.DButil;
import java.sql.*;
public class UserDao {
/**
* 生成单例
*/
DButil DB =new DButil();
private static UserDao userDao =new UserDao();
public static UserDao getUserDao()
{
return userDao;
}
public boolean addUser(User user)
{
String sql="insert into info(username,password,sex,name,num,mail,xueyuan,xi,class,year,home,other)values(?,?,?,?,?,?,?,?,?,?,?,?)";
Connection conn =null;
PreparedStatement ps=null;
boolean flag =false;
try {
conn=DB.getConn();
ps=conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getSex());
ps.setString(4, user.getName());
ps.setString(5, user.getNum());
ps.setString(6, user.getMail());
ps.setString(7, user.getXueyuan());
ps.setString(8, user.getXi());
ps.setString(9, user.getClasses());
ps.setString(10, user.getYear());
ps.setString(11, user.getHome());
ps.setString(12, user.getOther());
int a=ps.executeUpdate();
if(a!=0)
{
flag =true;
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DB.close(conn);
DB.close(ps);
}
return flag;
}
}
3.DButil类:
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
*
*/
public final class DButil {
/**
* 数据库的连接函数
* 可修改的
*
* @return 数据库的连接
*/
public Connection getConn()
{
Connection connection =null;
// student 为数据库名称 ? 后面加的是条件
String db_url="jdbc:mysql://localhost:3306/user?serverTimezone=UTC";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(db_url, "root", "123456");
System.out.println("Success connect MySql server!");
} catch (Exception e) {
}
return connection;
}
public void close(Connection conn)
{
if(conn!=null)
{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(Statement state) {
if (state != null) {
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(PreparedStatement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4.AddUserServlet类:
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 bean.User;
import dao.UserDao;
/**
* Servlet implementation class AddUserServlet
*/
@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddUserServlet() {
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
int s=0;
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String num=request.getParameter("num");
String password=request.getParameter("password");
String name=request.getParameter("name");
String sex=request.getParameter("sex");
String mail=request.getParameter("mail");
String xueyuan=request.getParameter("xueyuan");
String xi=request.getParameter("xi");
String classes=request.getParameter("class");
String year=request.getParameter("year");
String home=request.getParameter("home");
String other=request.getParameter("other");
User user=new User();
user.setUsername(username);
user.setPassword(password);
user.setName(name);
user.setSex(sex);
user.setNum(num);
user.setXueyuan(xueyuan);
user.setMail(mail);
user.setXi(xi);
user.setClasses(classes);
user.setYear(year);
user.setHome(home);
user.setOther(other);
UserDao userDao=UserDao.getUserDao();
if(userDao.addUser(user))
{
request.setAttribute("message", "添加成功");
request.getRequestDispatcher("addUser.jsp").forward(request,response);
}
else
{
if(s==1)
{request.setAttribute("message", "添加失败");
request.getRequestDispatcher("addUser.jsp").forward(request,response);
}
else if(s==0)
{request.getRequestDispatcher("addUser.jsp").forward(request,response);
s=1;}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
5.JSP:
pageEncoding="utf-8"%>
loginObject message = request.getAttribute("message");
if(message!=null && !"".equals(message)){
%>
alert("");
登录账号:
登录密码:
姓名:
性别: 男
女
学号:
电子邮箱:
所在学院:
所在系:
所在班级:
入学年份(届):
2016
2017
2018
2019
届
生源地:
备注:
var reg=/^[a-zA-Z]\w*$/;
var flag1=false,flag2=false,flag3=false;
function chang1(obj)
{
if(obj.length<6||name.length>12)
{
alert("用户名要求6-12位"); }
if(!reg.test(obj))
{
alert("用户名由字母开头,后字母、数字或下划线!");
}
}
function chang2(obj)
{
if(obj.length<8)
{
alert("密码要求8位以上"); }
}
function chang3(obj)
{
if(obj.length!=8)
{
alert("学号为8位");
}
else if(obj.length==8)
{
var fdStart = obj.indexOf("2018");
if(fdStart !=0){
alert("学号应以2018开头");
}
}
}
function chang4(obj)
{
if(obj.length!=0){
reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if(!reg.test(obj)){
alert("对不起,您输入的邮箱格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称!
}
}
}