欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java web 项目中的注册功能代码(用户注册信息写入数据库)

程序员文章站 2022-07-03 17:27:31
...
  1. 用户注册界面
<%@ 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>
<script type="text/javascript">
	function registerdata() {
		var sname=document.getElementById("sname")
		var sid = document.getElementById("sid");
		var spassword = document.getElementById("spassword");
		if (sname.value == "") {
			alert("姓名不能为空!");
			sname.focus();
			return false;
		}else if (sid.value == "") {
			alert("学号不能为空!");
			sid.focus();
			return false;
		} else if (spassword.value == "") {
			alert("密码不能为空!");
			spassword.focus();
			return false;
		} else if (spassword.value.length<6|spassword.value.length>12) {
			alert("密码长度不符合要求,请输入6~12位新密码!");
			spassword.focus();
			return false;
		}
	}
</script>
</head>
<body>
	<form action="RegisterServlet" method="POST" onsubmit="return zhucedata();">
		<p>
			姓名:<input name="sname" id="sname" type="text">
		</p>
		<p>
			学号:<input name="sid" id="sid" type="text">
		</p>
		<p>
			密码:<input name="spassword" id="spassword" type="password">
		</p>
		<p>
			专业: <select name="sprofession">
				<option>信息与计算科学</option>
				<option>数学与统计</option>
				<option>大数据</option>
			</select>
		</p>
		<p>
			年级: <select name="sgrade">
				<option>2019级</option>
				<option>2018级</option>
				<option>2017级</option>
				<option>2016级</option>
				<option>2015级</option>
			</select>
		</p>

		<input type="submit" value="完成">
	</form>
</body>
</html>
  1. 服务器中的Servlet
package com.qst.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.qst.service.RegisterDAO;
import com.qst.service.Student;

/**
 * Servlet implementation class ZhuceServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//定义输入输出时的字符编码形式
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		//创建一个新的对象并赋予它值,该值由this调用requestDataObj(request)返回
		Student student = this.requestDataObj(request);
		//创建一个 ResumeDAO的对象 dao
		RegisterDAO dao = new RegisterDAO();
		//用 dao对象调用ResumeDAO中的函数add
		dao.add(student);
		//调用完成后,进行页面跳转,跳转至cue.jsp页面
		response.sendRedirect("cue.jsp");
	}

	private Student requestDataObj(HttpServletRequest request) {
		Student student = new Student();
		// 将表单中的数据取出并赋予、给新的字符串变量
		String sname = request.getParameter("sname");
		String sid = request.getParameter("sid");
		String spassword = request.getParameter("spassword");
		String sprofession = request.getParameter("sprofession");
		String sgrade = request.getParameter("sgrade");
		// 将取出的数据用Student进行封装,封装成一个对象,方便后面的调用和值的传递
		student.setSname(sname);
		student.setSid(sid);
		student.setSpassword(spassword);
		student.setSprofession(sprofession);
		student.setSgrade(sgrade);
		student = new Student(sname, sid, spassword, sprofession, sgrade);
		//返回封装的类,即该方法被调用时会返回一个封装类student
		return student;
	}

}

  1. 封装类Student
package com.qst.service;

public class Student {
	private String sname;
	private String sid;
	private String spassword;
	private String sprofession;
	private String sgrade;

	public Student() {
		super();
	}

	public Student(String sname, String sid, String spassword, String sprofession, String sgrade) {
		super();
		this.sname = sname;
		this.sid = sid;
		this.spassword = spassword;
		this.sprofession = sprofession;
		this.sgrade = sgrade;
	}

	/**
	 * @return the sname
	 */
	public String getSname() {
		return sname;
	}

	/**
	 * @param sname
	 *            the sname to set
	 */
	public void setSname(String sname) {
		this.sname = sname;
	}

	/**
	 * @return the sid
	 */
	public String getSid() {
		return sid;
	}

	/**
	 * @param sid
	 *            the sid to set
	 */
	public void setSid(String sid) {
		this.sid = sid;
	}

	/**
	 * @return the spassword
	 */
	public String getSpassword() {
		return spassword;
	}

	/**
	 * @param spassword
	 *            the spassword to set
	 */
	public void setSpassword(String spassword) {
		this.spassword = spassword;
	}

	/**
	 * @return the sprofession
	 */
	public String getSprofession() {
		return sprofession;
	}

	/**
	 * @param sprofession
	 *            the sprofession to set
	 */
	public void setSprofession(String sprofession) {
		this.sprofession = sprofession;
	}

	/**
	 * @return the sgrade
	 */
	public String getSgrade() {
		return sgrade;
	}

	/**
	 * @param sgrade
	 *            the sgrade to set
	 */
	public void setSgrade(String sgrade) {
		this.sgrade = sgrade;
	}

}

  1. RegisterDAO类中的add()方法
package com.qst.service;

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

import com.qst.dao.DBU;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

//将java语言转化成数据库语言,并提交到数据库(调用DBU类中的方法)
public class RegisterDAO {
	public int add(Student student) {
		int basicinfoID = 0;
		String sql = "insert into dual(" + "sname, sid, spassword, sprofession, sgrade)"
				+ "values(?,?,?,?,?)";
		Connection conn = DBU.getConnection();
		PreparedStatement pstmt = null;
		try {
			conn.setAutoCommit(false);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, student.getSname());
			pstmt.setString(2, student.getSid());
			pstmt.setString(3, student.getSpassword());
			pstmt.setString(4, student.getSprofession());
			pstmt.setString(5, student.getSgrade());
			pstmt.executeUpdate();
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DBU.closeJDBC(null, pstmt, conn);
		}
		return basicinfoID;

	}
}

5 数据库中建一个用户为shixun 用户中建一个student表
表中有sname, sid, spassword, sprofession, sgrade这五个属性,
并且设置sid 为主键