jsp连接oracle实现登录注册(简单)

要求
tomcat、oracle数据库服务启动
需要一个Oracle的驱动器,放在WEB-INFO的lib。版本号如下:
jsp连接oracle实现登录注册(简单)_第1张图片
数据库文件
在Oracle数据库中的scott的连接,建立一个名字叫t_user的表,表格的字段为username,password。

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
input[type="submit"] {
     
	width: 100px;
	margin: 0 5px;
}
input[type="button"] {
     
	width: 100px;
	margin: 0 5px;
}
</style>
</head>
<body>
	<center>
	<font face="楷体" size="6" color="#000">登录界面</font>
	<form action="index.jsp" method="post">
	<table width="300" height="180" border="5" bordercolor="#A0A0A0">
		<tbody>
			<tr>
				<td><label>用户名:</label></td>
				<td><input type="text" name="name" maxlength = "12" placeholder="请输入用户名!" /></td>
			</tr>
			<tr>
				<td><label>&nbsp;&nbsp;&nbsp; 码:</label></td>
				<td><input type="password" name="pwd" maxlength = "16" placeholder="请输入密码!" /></td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<input type="submit" value="登陆" />
				<input type="button" value="注册" onclick = "window.open('reg.jsp')" /></td>
			</tr>
		</tbody>
	</table>
	</form>
	</center>
</body>
</html>

效果图如下:
jsp连接oracle实现登录注册(简单)_第2张图片

index.jsp

实现检查登录界面与数据库连接
实现数据库连接,判断登录时用户名和密码是否正确。

<%@ page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
	<%
		String uName = new String(request.getParameter("name"));
	String uPwd = request.getParameter("pwd");

	Connection conn = null;
	PreparedStatement pstm = null;
	ResultSet rs = null;
	String driverClass = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:orcl";
	String user = "scott";
	String password = "tiger";
	String sql = "select username,password from t_user";
	try {
     
		Class.forName(driverClass);
		conn = DriverManager.getConnection(url, user, password);
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		while (rs.next()) {
     
			if (rs.getString("username").equals(uName) && rs.getString("password").equals(uPwd)) {
     
				RequestDispatcher dis = request.getRequestDispatcher("success.jsp");
				dis.forward(request, response);
				return;
			} 
		}
		response.sendRedirect("login.jsp");
	} catch (ClassNotFoundException e) {
     
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (SQLException e) {
     
		// TODO Auto-generated catch block
		e.printStackTrace();
	} finally {
     
		try {
     
			if (rs != null) {
     
				rs.close();
				rs = null;
			}
			if (pstm != null) {
     
				pstm.close();
				pstm = null;
			}
			if (conn != null) {
     
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	%>
</body>
</html>

reg.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册界面</title>
<style>
input[type="submit"] {
     
	width: 100px;
	margin: 0 5px;
}

input[type="reset"] {
     
	width: 100px;
	margin: 0 5px;
}
</style>
<script>
	function addCheck() {
     
		var username = document.getElementById("username").value;
		var password = document.getElementById("password").value;
		var newword = document.getElementById("newword").value;
		if (username == "") {
     
			alert("用户名不能为空!");
			document.getElementById("username").focus();
			return false;
		}
		if (password == "") {
     
			alert("密码不能为空!");
			document.getElementById("password").focus();
			return false;
		}
		if (password != newword) {
     
			alert("两次输入密码不相同!");
			document.getElementById("newword").focus();
			return false;
		}
	}
	function validate() {
     
		var flag = addCheck();
		if (flag == false)
			return false;
		return true;
	}
</script>
</head>
<body>
	<center>
		<font face="楷体" size="6" color="#000">注册界面</font>
		<form action="index2.jsp" method="post"
			onsubmit="return validate()">
			<table width="300" height="180" border="5" bordercolor="#A0A0A0">
				<tr>
					<th>用户名:</th>
					<td><input type="text" name="name" id="username"
						placeholder="输入16个字符以内" maxlength="16"
						onfocus="if(this.value == '输入16个字符以内') this.value =''"></td>
				</tr>
				<tr>
					<th>输入密码:</th>
					<td><input type="password" name="pwd" id="password"
						placeholder="输入20个字符以内" maxlength="20"
						onfocus="if(this.value == '输入20个字符以内') this.value =''"></td>
				</tr>
				<tr>
					<th>确认密码:</th>
					<td><input type="password" name="nwd" id="newword"
						placeholder="重新输入密码" maxlength="20"
						onfocus="if(this.value == '重新输入密码') this.value =''"></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="submit"
						value="注  册"> <input type="reset" value="重  置"></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

效果图如下:
jsp连接oracle实现登录注册(简单)_第3张图片

index2.jsp

实现检查注册界面与数据库连接
实现数据库连接,把注册的用户名和密码添加到数据库中。

<%@ page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
	<%
		String uName = request.getParameter("name");
		String uPwd = request.getParameter("pwd");
		
		Connection conn =null;
		PreparedStatement pstm = null;
		ResultSet rs =  null;
		String driverClass = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String user = "scott";
        String password = "tiger";
        String sql="insert into t_user(username,password) values(?,?)";
        try{
     
        	Class.forName(driverClass);
			conn = DriverManager.getConnection(url, user, password);
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, uName);
			pstm.setString(2, uPwd);
			rs=pstm.executeQuery();
			if(rs.next()){
     
				out.print("用户注册成功!");
				response.sendRedirect("login.jsp");
			}else{
     
				out.print("用户注册失败!");
				response.sendRedirect("reg.jsp");
			}
        }catch (ClassNotFoundException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
     
			try {
     
				if(rs!=null) {
     	
					rs.close();		
					rs =null;
				}
				if(pstm!=null) {
     	
					pstm.close();
					pstm =null;
				}
				if(conn!=null) {
     	
					conn.close();
					conn =null;
				}
			} catch (SQLException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}       
	%>
</body>
</html>

你可能感兴趣的:(作品,jsp,数据库,jdbc)