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

SQL Server注册表单录入数据库

程序员文章站 2022-07-13 13:38:13
...

前端界面zhuce_to_doReg.jsp

<%@ page language="java" import="java.util.*,entity.*,dao.*,dao.impl.*" contentType="text/html;charset=gb2312"%>
<%@ page import="dao.impl.UserDaoImpl" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <link rel="stylesheet" href="index.css" type="text/css">
    <title>注册</title>
<style>
#apDiv2{
	position:absolute;
	left:1px;
	top:51px;
	width:100%;
	height:25px;
	z-index:2;
	background-color:#e0f0f9;
}
</style>
<script language="javascript">
	function check(){
		if(document.form1.uname.value==""){
			alert("用户名不能为空!");
			return false;
		}
		if(document.form1.upass.value==""){
			alert("密码不能为空!");
			return false;
		}
		if(document.form1.upass.value!=document.form1.upass2.value){
			alert("2次密码不一样!");
			return false;
		}
		
		}
		function but1(){
		if(check()){
			return true;
		}else
			return;
	}
</script>
</head>
<body>
		<h2 align="center">校园BBS系统</h2>
	<div class="STYLE4" id="apDiv2" >
		您尚未<a href="login1.jsp" target="_blank">登录</a> |
		<a href="zhuce.jsp" target="_blank"> 注册a</a>
		<a href="zhuce1.jsp" target="_blank"> 注册b</a>
	</div>
	<br>
		<form id="form1" name="form1" method="post" action="manage/doReg.jsp" action="return check()">
			<table width="100%" height="115" border="0" cellpadding="0" cellspacing="0">
				<tr align="center">
					<td align="center"><span class="body">&nbsp;&nbsp;用 &nbsp;户&nbsp;&nbsp;名:</span>
					<label>
						<input name="uname" type="text" id="textfield1"/>
					</label>
					</td>
				</tr>
				<tr>
					<td align="center"><span class="body">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</span>
					<label>
						<input name="upass" type="password" id="textfield2"/>
					</label>
					</td>
				</tr>
				<tr>
					<td align="center"><span class="body">重复密码:</span>
					<label>
						<input name="upass2" type="password" id="textfield3"/>
					</label><BR><BR>
					</td>
				</tr>
				
				<tr>
					<td align="center">性别:
						<input name="gender" type="radio" value="1" checked>男
						<input name="gender" type="radio" value="2">女<BR><BR>
					</td>
				</tr>
				<tr>
					<td align="center">请选择头像<BR><BR>
				</tr>
				<tr>
					<td align="center">
						<img src= "image/Avatar/a1.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="a"></img>
						<img src= "image/Avatar/a2.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="b"></img>
						<img src= "image/Avatar/a3.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="c"></img>
						<img src= "image/Avatar/a4.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="d"></img>
						<img src= "image/Avatar/a5.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="e"></img>
						<BR><BR><BR><BR>
						<img src= "image/Avatar/a6.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="f"></img>
						<img src= "image/Avatar/a7.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="g"></img>
						<img src= "image/Avatar/a8.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="h"></img>
						<img src= "image/Avatar/a9.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="i"></img>
						<img src= "image/Avatar/a10.jpg" style="height: 50px; width: 60px; "><input name="head" type="radio" value="j"></img>
				<tr>
				<tr>
					<td align="center">
						<lable>
						<td>
							<input align="center" type="reset" name="button" id="button1" value="重置"/>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input align="center" type="submit" name="button" id="button2" value="注册"/>
						</lable>
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

中间件doReg.jsp

<%@ page language="java" import="entity.*,dao.*,dao.impl.*,java.util.Date;" pageEncoding="GBK"%>
<%
	request.setCharacterEncoding("GBK");//字符编码
	Date date = new Date();//获取时间
	String UserName = request.getParameter("uname");//获取用户名
	String UserPass = request.getParameter("upass");//获取密码
	String HeadPhoto = request.getParameter("head");//获取头像
	int gender = Integer.parseInt(request.getParameter("gender"));//获取性别
	UserDao userDao = new UserDaoImpl();
	User user = new User();
	user.setUserName(UserName);
	user.setUserPass(UserPass);
	user.setGender(gender);
	user.setHead(HeadPhoto);
	user.setRegTime(date);//加入时间
	int num = userDao.addUser(user);//向数据库添加数据
	if(num==1){
		%>
		<meta http-equiv="refresh" content="2;url=../login1.jsp">
		<%
		out.println(userDao.findUser("user"));
	}else{
		response.sendRedirect("../zhuce_to_doReg.jsp");
	}
%>

录入数据库,并验证后转到登录界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="dao.impl.UserDaoImpl" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <link rel="stylesheet" href="index.css" type="text/css">
    <title>登录</title>
<style>
#apDiv2{
	position:absolute;
	left:1px;
	top:51px;
	width:100%;
	height:25px;
	z-index:2;
	background-color:#e0f0f9;
}
</style>


<script language="javascript">
	function checkInfo(){
		var name = document.getElementById("names").value;
		var password = document.getElementById("passs").value;
		if(name ==""){
			alert("请输入账号");
			return false;
		}else if(password==""){
			alert("请输入密码")
			return false;
		}else
		return true;
	}
	function but(){
		if(checkInfo()){
			window.location.href="success11.jsp";
		}else
			return;
	}
	
	<script>
    function jump(){
        window.location.href="http://blog.sina.com.cn/mleavs";
    }
</script>
 

		
</script>

</head>
<body>
		<h2 align="center">校园BBS系统</h2>
<div class="STYLE4" id="apDiv2" >
			您尚未<a href="login1.jsp" target="_blank">登录</a> |
			<a href="zhuce.jsp" target="_blank"> 注册</a> |
			<a href="index3.jsp" target="_blank"> 返回首页>></a>
	 	</div>
	<br>
		<form method="post" action="jsp11.jsp" class="backForm">
		<h2 align="center" style="padding-top: 25px">登录界面</h2><br>
		<table align="center">
			<tr>
				<td>用户名:</td>
				<td><input align="center" type="text" name="username" maxlength="10"></td>
			</tr>
			<tr>
				<td>密&nbsp;&nbsp;码:</td>
				<td><input align="center" type="password" name="password"maxlength="10"></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td>
					<input align="center" type="reset" value="重置">
					<input align="center" type="submit" value="登录">
				</td>
			</tr>
		</table>
		</form>
		
	</body>
</html>

数据库连接类BaseDao.java

package dao.impl;

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

public class BaseDao {
	public final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库驱动
	public final static String url = "jdbc:sqlserver://localhost:1433;DataBaseName=SKT"; // url
	public final static String dbName = "sa"; // 数据库用户名
	public final static String dbPass = "sa123"; // 数据库密码

	/**
	 * 得到数据库连接
	 * 
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 * @return 数据库连接
	 */
	public Connection getConn() throws ClassNotFoundException, SQLException {
		Class.forName(driver); // 注册驱动
		Connection conn = DriverManager.getConnection(url, dbName, dbPass); // 获得数据库连接
		return conn; // 返回连接
	}
	/**
	 * 释放资源
	 * 
	 * @param conn
	 *            数据库连接
	 * @param pstmt
	 *            PreparedStatement对象
	 * @param rs
	 *            结果集
	 */
	public void closeAll(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		/* 如果rs不空,关闭rs */
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		/* 如果pstmt不空,关闭pstmt */
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		/* 如果conn不空,关闭conn */
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 执行SQL语句,可以进行增、删、改的操作,不能执行查询
	 * 
	 * @param sql
	 *            预编译的 SQL 语句
	 * @param param
	 *            预编译的 SQL 语句中的‘?’参数的字符串数组
	 * @return 影响的条数
	 */
	public int executeSQL(String preparedSql, String[] param) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		int num = 0;

		/* 处理SQL,执行SQL */
		try {
			conn = getConn(); // 得到数据库连接
			pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
			if (param != null) {
				for (int i = 0; i < param.length; i++) {
					pstmt.setString(i + 1, param[i]); // 为预编译sql设置参数
				}
			}
			num = pstmt.executeUpdate(); // 执行SQL语句
		} catch (ClassNotFoundException e) {
			e.printStackTrace(); // 处理ClassNotFoundException异常
		} catch (SQLException e) {
			e.printStackTrace(); // 处理SQLException异常
		} finally {
			closeAll(conn, pstmt, null); // 释放资源
		}
		return num;
	}
}

UserDao.java

package dao;

import entity.*;

public interface UserDao {
	//对数据库的操作
	//insert
	//delete
	//update
	//select

	public static final int FEMALE = 1;
	public static final int MALE = 2;

	public User findUser(String userName);
	public User findUser(int userId);
	public int addUser(User user);
	public int updateUser(User user);
	
}

UserDaoImpl.java

package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import entity.User;
import dao.UserDao;

public class UserDaoImpl extends BaseDao implements UserDao {
	
	
	public User[] users = new User[10];
	
	
	public int addUser(User user){
		String sql = "insert into TBL_USER(UserName,UserPass,HeadPhoto,regTime,gender) values (?,?,?,?,?)";
		String [] param = new String [5]; 
		param[0]=user.getUserName();
		param[1]=user.getUserPass();
		param[2]=user.getHead();
		SimpleDateFormat nowTime = new SimpleDateFormat("yyyy-MM-dd");//将时间转化为指定格式的字符串
		param[3]=nowTime.format(user.getRegTime());
		param[4]=String.valueOf(user.getGender());
		BaseDao useradd = new BaseDao();
		useradd.executeSQL(sql, param);
		return 1;
	}
	
	
	public int updateUser(User user){
		String sql = "update TBL_USER set UserName=?,UserPase=?,gender=?,HeadPhoto=?,regTime=? where userId=?";
		String [] param = new String [6]; 
		param[0]=user.getUserName();
		param[1]=user.getUserPass();
		param[2]=String.valueOf(user.getGender());
		param[3]=user.getHead();
		SimpleDateFormat nowTime = new SimpleDateFormat("yyyy-MM-dd");//将时间转化为指定格式的字符串
		param[4]=nowTime.format(user.getRegTime());
		param[5]=String.valueOf(user.getUserId());
		BaseDao userupd = new BaseDao();
		userupd.executeSQL(sql, param);
		return 1;
	}
	
	
	public User findUser(String userName){
		Connection conn = null;
		PreparedStatement pstmt = null;
		String sql = "select * from TBL_USER where UserName='" + userName + "' ";
		ResultSet rs = null;
		try {
			conn = this.getConn();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			User user = new User();
			if(rs.next()){
				user.setUserId(rs.getInt("userId"));
				user.setUserName(rs.getString("UserName"));
				user.setUserPass(rs.getString("UserPass"));
				user.setGender(rs.getInt("gender"));
				user.setHead(rs.getString("HeadPhoto"));
				user.setRegTime(rs.getDate("regTime"));
				return user;
			}	
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeAll(conn, pstmt, rs);
		}
		
		return null;
	}
	
	
	public User findUser(int userId){
		String sql = "select * from TBL_USER where userId='" + userId + "' ";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = this.getConn();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			User user = new User();
			if(rs.next()){
				user.setUserId(rs.getInt("userId"));
				user.setUserName(rs.getString("UserName"));
				user.setUserPass(rs.getString("UserPass"));
				user.setGender(rs.getInt("gender"));
				user.setHead(rs.getString("HeadPhoto"));
				user.setRegTime(rs.getDate("regTime"));
				return user;
			}	
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.closeAll(conn, pstmt, rs);
		}
		
	return null;
	}
}

User.java

package entity;

import java.util.Date;

public class User {
	private int userId;
	private String userName;
	private String userPass;
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}
	public int getGender() {
		return gender;
	}
	public void setGender(int gender) {
		this.gender = gender;
	}
	public String getHead() {
		return head;
	}
	public void setHead(String head) {
		this.head = head;
	}
	public Date getRegTime() {
		return regTime;
	}
	public void setRegTime(Date regTime) {
		this.regTime = regTime;
	}
	private int gender;
	private String head;
	private Date regTime;
}

SQL Server注册表单录入数据库


SQL Server注册表单录入数据库


SQL Server注册表单录入数据库


SQL Server注册表单录入数据库


USE [SKT]
GO

/****** Object:  Table [dbo].[TBL_USER]    Script Date: 2019/11/20 14:12:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TBL_USER](
    [userId] [int] IDENTITY(1,1) NOT NULL,    --注意这里的IDENTITY是自动增长,如果没有加自动增长,就会提示userId为空
    [UserName] [varchar](20) NOT NULL,
    [UserPass] [varchar](20) NOT NULL,
    [HeadPhoto] [varchar](100) NOT NULL,
    [regTime] [datetime] NOT NULL,
    [gender] [smallint] NOT NULL,
 CONSTRAINT [PK_TBL_USERS] PRIMARY KEY CLUSTERED 
(
    [userId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO