jsp连接数据库实现注册登陆(超级详细)

要求
tomcat8.0、mysql
需要一个mysql的驱动器,放在WEB-INFO的lib。版本号如下:
在这里插入图片描述
数据库文件
在mysql数据库中建立一个名字叫db_01的数据库。在数据库的建立一个名字叫tb_user的表,表格的字段为UName,Pwd。

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <center>
    <font face = "宋体" size = "6" color = "#000">欢迎JSP</font><hr>
    <div>
        <img alt="" width = "600" height = "400" src="D:\Images.jpg">
    </div>
    <table width = "200" border ="1" bordercolor = "#00F">
        <tr>
          <!--<td><input type = "button" value = "登      陆" onclick = "window.location.href('login.jsp')"></td>--->
		  <td><input type = "button" value = "登      陆" onclick = "window.open('login.jsp')"></td>
          <td><input type = "button" value = "注      册" onclick = "window.open('register.jsp')"></td>
        </tr> 
    </table>
  </center>
  </body>
</html>

login.jsp


<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 
  <body>
    <center>
  	<font face="楷体" size="6" color="#000" >登录界面</font>
  	<%  
    String flag = request.getParameter("errNo");  
    try{
         if(flag!=null)
            out.println("用户名不存在或密码错误");
    }catch(Exception e){
        e.printStackTrace();
    }
   %>
  	<form action = "loginCh.jsp" method="post">
      <table width="300" height = "180" border="5" bordercolor="#A0A0A0"> 
 		<tr>
 		  <th>账  户:</th>
 		  <td><input type="text" name="name"  value = "请输入用户名" maxlength = "16" onfocus = "if(this.value == '请输入用户名') this.value =''"></td>
 	    </tr>
 	    <tr>
 		  <th>密  码:</th>
 		  <td><input type="password" name="pwd" maxlength = "20"></td>
 	    </tr>
 	    <tr>
 	      <td colspan = "2" align = "center">
 		    <input type="submit" name="submit" value="登       录">
 		    <input type="button" value="返       回"
 			  "window.location.href('/webText')">
 	      </td>
 	    </tr>
 	  </table>
 	</form>
  </center>
  </body>
</html>

checkRegister.jsp


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 
  </head>
  <body>
    <%      
            String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");  
            String pwd = request.getParameter("password");
 
            String driverClass = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/db_01";
            String username = "root";
            String password = "root";
            Class.forName(driverClass);//加载驱动 
            Connection conn = DriverManager.getConnection(url,username,password);//得到连接
            PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
              ResultSet rs = pStmt.executeQuery();
                if(rs.next()){
                    out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
                }else{
                    PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
                        int rst = tmt.executeUpdate();
                        if (rst != 0){
                              out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");  
                        }else{
                           out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");  
                        }
                }
     %>
  </body>
</html>

loginCh.jsp


<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
 
  </head>
  <body>
    <%      
            String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");  
            String pwd = request.getParameter("password");
 
            String driverClass = "com.mysql.jdbc.Driver";
            String url = "jdbc:mysql://localhost:3306/db_01";
            String username = "root";
            String password = "root";
            Class.forName(driverClass);//加载驱动 
            Connection conn = DriverManager.getConnection(url,username,password);//得到连接
            PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
              ResultSet rs = pStmt.executeQuery();
                if(rs.next()){
                    out.println("<script language='javascript'>alert('该用户已存在,请重新注册!');window.location.href='register.jsp';</script>");
                }else{
                    PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
                        int rst = tmt.executeUpdate();
                        if (rst != 0){
                              out.println("<script language='javascript'>alert('用户注册成功!');window.location.href='index.jsp';</script>");  
                        }else{
                           out.println("<script language='javascript'>alert('用户注册失败!');window.location.href='register.jsp';</script>");  
                        }
                }
     %>
  </body>
</html>

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
    <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>
  <body>
    <center>
	<font face="楷体" size="6" color="#000">注册界面</font>
	<form action = "checkRegister.jsp" method = "post" onsubmit = "return validate()">
  	<table width="300" height = "180" border="5" bordercolor="#A0A0A0">
  	  <tr>
		<th>用户名:</th>
		<td><input type="text" name="username" value="输入16个字符以内" maxlength = "16" onfocus = "if(this.value == '输入16个字符以内') this.value =''"></td>
 	  </tr>
 	  <tr>
 		<th>输入密码:</th>
 		<td><input type="text" name="password" value="输入20个字符以内" maxlength = "20" onfocus = "if(this.value == '输入20个字符以内') this.value =''"></td>
 	  </tr>
 	  <tr>
 		<th>确认密码:</th>
 		<td><input type="text" name="newword" value="重新输入密码" 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>

success.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>Feilong_登录成功</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <body>
    <center>
    <%
     String name = new String(request.getParameter("username").getBytes("8859_1"));
     out.println("欢迎你:" + name);
    %><br>
    <a href="login.jsp">重新登陆</a>
    </center>
  </body>
</html>

  • 48
    点赞
  • 445
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
教务管理系统(SQL数据库) 摘要 :课题目标是设计并实现一个B/S体系结构的教务信息管理系统。结合实践,理解网页开 发技术和数据库的基本知识,学习相关开发工具和应用软件,熟悉系统设计的过程,熟 练掌握网络数据库编程方法。 本系统用JSP技术来编写本系统,数据库用SQL SERVER 2000来连接系统。本论文主要涉及软件,数据库与网络技术等。涵盖知识面广,可有效地 提高学生综合运用所学知识分析解决问题的能力,增强学生对事物的理解与掌握能力,培 养学生掌握科学的研究方法,正确的设计思想,独立思考,勇于进取,探索创新,为今后 进一步学习与工作奠定了良好的基础. 关键词:教务信息管理系统;数据库JSP 目 录 1 引言 1 2 系统分析 2 2。1 功能需求分析 2 2。2 本系统采用的关键技术 3 2。2。1 JSP技术 3 2.2.2 JavaBean技术 4 2。2。3 JDBC技术 5 2。3 可行性分析 6 2。4 系统运行环境 7 3 系统总体设计 8 3.1 总体功能 8 3。2 处理流程设计 9 3。2.1 系统操作流程 9 3。2.2 数据增加流程 10 3。2。3 数据修改流程 10 3。3.4 数据删除流程 11 4 系统详细设计 13 4。1 后台数据库设计 13 4。1。1 SQL SERVER 介绍 13 4.1。2 数据库表结构 14 4。3 系统模块设计 16 4。3.1 用户登陆 16 4。3。2 学生信息管理 16 4.3。2 教师信息管理 18 5 系统调试与测试 20 5。1 程序调试 20 5.2 程序的测试 20 5。2.1 测试的重要性及目的 20 5.2。2 测试的步骤 22 5.2.3 测试的主要内容 22 6 结论 24 6。1 系统评价 24 6.2 安全性问题 24 致谢 26 参考文献 27 功能需求分析 经过调研,基于B/S的教务信息管理系统的要求描述如下: 针对基于B/S的教务信息管理系统用户群情况,我们决定将本系统分为三个部分:学生用 户部分,教师用户部分和超级管理员用户部分.考虑到数据信息的隐私性问题,我们也对 各个用户的功能设置做了调整。例如成绩属于个人隐私,学生用户只允许查看自己的成绩 ,无权查看其他用户的成绩等。总体来说,学生用户只具有查看权限,而教师用户具有 比学生用户高的管理权限,但只局限于成绩部分。超级管理员用户具有最高权限,可以 修改查看学生用户教师用户乃至超级管理员用户的全部信息。 用户进入登陆页面会通过登陆框中用户所输入的信息判断用户是学生用户,教师用户,超 级管理员用户或者非法用户.并通过判断进入相对应的页面。 在学生用户界面中,用户只拥有最基本的查看权限,不具备任何的修改权限.用户可以查 看学生的基本信息,自己的各种课程信息,显示自己各门功课的成绩。 在教师用户界面中,教师对部分信息具有修改权限。例如录入学生成绩。 在超级管理员用户界面中,用户拥有最高权限。可以添加修改包括学生,教师和管理员 在内的任何信息。更新数据的过程中,所有的数据都尽最大可能的作到数据的级联。在 添加学生基本信息的同时做到对相关信息的级联添加.所有的添加操作之前都要确定数据 库中是否存在相同的记录,以确保数据的唯一性,把数据库被破坏的可能性降到最低。 所有的添加功能都在添加的同时把数据更新到数据库,并马上在界面上显示出结果以能 够让用户及时的知道更新的内容。超级管理员对所有的数据都具有添加,删除,修改,查看 等基本功能。在所有的删除操作之前,系统都级联的删除其他表中的相关信息。 以下是部分界面图,如图片不清,请直接点击图片,如需要看更详细的资料,请直接联系 客服! 图1(双击并最大化图片,可看清晰 图片) 图2(双击并最大化图片,可看清 晰图片) 图3(双击并最大化图片,可看清晰 图片) ----------------------- 教务管理系统(SQL数据库)-1401191959246956全文共4页,当前为第1页。 教务管理系统(SQL数据库)-1401191959246956全文共4页,当前为第2页。 教务管理系统(SQL数据库)-1401191959246956全文共4页,当前为第3页。 教务管理系统(SQL数据库)-1401191959246956全文共4页,当前为第4页。
首先需要在JSP中导入JDBC驱动,然后建立数据库连接。可以按照以下步骤实现: 1. 导入JDBC驱动 ```java <%@ page import="java.sql.*" %> ``` 2. 建立数据库连接 ```java String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?useSSL=false"; String username = "root"; String password = "root"; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } ``` 3. 实现用户注册 ```java String username = request.getParameter("username"); String password = request.getParameter("password"); PreparedStatement pstmt = null; String sql = "insert into user(username, password) values(?, ?)"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } ``` 4. 实现用户登录 ```java String username = request.getParameter("username"); String password = request.getParameter("password"); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select * from user where username=? and password=?"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); rs = pstmt.executeQuery(); if (rs.next()) { // 登录成功 } else { // 登录失败 } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } ``` 以上代码仅供参考,具体实现需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值