JSP_5.16_课堂笔记

这篇具有很好参考价值的文章主要介绍了JSP_5.16_课堂笔记。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

完整的可以与数据库连接的登录界面的代码

login.jsp

<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
    
<!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>登录页面</title>
            <script src="js/jquery-3.3.1.min.js"></script>
            <style type="text/css">
                #diceng{
                    width:430px;
                    height:380px;
                    background-color:#FFFFFF33;
                    position:absolute;
                    left:50%;
                    top:50%;
                    margin-left:-215px;
                    margin-top:-190px;
                    border-radius:5px;
                }
                #zhongceng{
                    width:400px;
                    height:350px;
                    background-color:#FFFFFFFF;
                    position:absolute;
                    left:50%;
                    top:50%;
                    margin-left:-200px;
                    margin-top:-175px;
                    border-radius:5px;
                }
                #d1{
                    width:400px;
                    height:60px;
                    font-size:30px;
                    font-family:Avenir,Helvetica,Arial,sans-serif;
                    color:black;
                    display:block;
                    margin-left:40px;
                    margin-top:20px;
                    
                }
                #d2{
                    width:400px;
                    height:35px;
                    font-size:20px;
                    font-family:Avenir,Helvetica,Arial,sans-serif;
                    color:black;
                    display:block;
                    margin-left:40px;
                    
                }
                #d3{
                    width:400px;
                    height:35px;
                    font-size:20px;
                    font-family:Avenir,Helvetica,Arial,sans-serif;
                    color:black;
                    display:block;
                    margin-left:40px;
                    
                }
                #username{
                    width:280px;
                    height:40px;
                    font-size:30px;
                    font-family:Avenir,Helvetica,Arial,sans-serif;
                    color:black;
                    display:block;
                    margin-left:40px;
                    border-radius:5px;
                    
                }
                #password{
                    width:280px;
                    height:40px;
                    font-size:30px;
                    font-family:Avenir,Helvetica,Arial,sans-serif;
                    color:black;
                    display:block;
                    margin-left:40px;
                    border-radius:5px;
                    
                    
                }
                #submit{
                    width:237px;
                    height:40px;
                    margin-top: 5px;
                    background-color: #47a0fc;
                    border: 1px solid #47a0fc;
                    color: #fff;
                    font-size: 14px;
                    cursor: pointer;
                    outline: none;
                    border-radius: 2px;
                    display:block;
                    margin-left:65px;
                    margin-top:15px;
                    border-radius:5px;
                    
                }
                .bjimg {
                    position: fixed;
                    top: 0;
                    left: 0;
                    width: 100%;
                    height: 100%;
                    min-width: 1000px;
                    z-index: -10;
                    zoom: 1;
                    background-color: #fff;
                    background-repeat: no-repeat;
                    background-size: cover;
                    -webkit-background-size: cover;
                    -o-background-size: cover;
                    background-position: center 0;
                    background-image:url("https://w.wallhaven.cc/full/ex/wallhaven-ex9ork.jpg")
                    }
            </style>
        </head>
    <body>
        <div class="bjimg"></div>
        <div id=diceng>
            <div id=zhongceng>
            
                    <div id=d1>欢迎登录优逸客实训平台</div>
                    <div id=d2>用户名</div>
                    <input id=username type='text' placeholder="请输入用户名"></input>
                    <div id=d3>密码</div>
                    <input id=password type="password" placeholder="请输入密码"></input>
                    <button id=submit>登录</button>
            </div>
        </div>
        <script>
    $(document).ready(function(){
        $("#submit").click(function(){
            var username = $("#username").val(); // 获取用户名
            var password = $("#password").val(); // 获取密码
            if(username == "" || password == ""){
                alert("请输入用户名和密码!"); // 输入为空,提示用户
                return;
            }
            $.ajax({
                url: "jdbclogin.jsp?username="+username+"&password="+password, // 后端处理jsp文件
                type: "GET",
                dataType:'json',
                success: function(res){
                    if(res.msg== "success"){
                        window.location.href = "main.jsp"; // 登录成功,跳转到主页
                    }else{
                        alert("用户名或密码错误!"); // 登录失败,提示用户
                    }
                }
            });
        });
    });
</script>
    </body>
</html>

jdbclogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@page import="java.io.PrintWriter"%>
<%
    String username = request.getParameter("username"); // 获取用户名
    String password = request.getParameter("password"); // 获取密码
    PrintWriter writer = response.getWriter();
    response.setCharacterEncoding("UTF-8");
	Class.forName("com.mysql.cj.jdbc.Driver");
	Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "000000"); // 建立数据库连接
	Statement stat = conn.createStatement();
	String sql = "SELECT * FROM user WHERE username='"+username+"' AND password='"+password+"'";
	ResultSet rs = stat.executeQuery(sql); // 执行查询
	if(rs.next()){
		   writer.write("{\"msg\":\"success\"}");
		   writer.flush();
		 }else{
		   writer.write("{\"msg\":\"fail\"}");
		   writer.flush();
		 }
   	rs.close();
   	stat.close();
   	conn.close();

%>

register.jsp

<%@ page language="java" contentType="text/html; UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
	<html>
		<head>
			<meta charset="UTF-8">
			<title>注册页面</title>
			<script src="js/jquery-3.3.1.min.js"></script>
			<style type="text/css">
				#diceng{
					width:430px;
					height:380px;
					background-color:#FFFFFF33;
					position:absolute;
					left:50%;
					top:50%;
					margin-left:-215px;
					margin-top:-190px;
					border-radius:5px;
				}
				#zhongceng{
					width:400px;
					height:350px;
					background-color:#FFFFFFFF;
					position:absolute;
					left:50%;
					top:50%;
					margin-left:-200px;
					margin-top:-175px;
					border-radius:5px;
				}
				#d1{
					width:400px;
					height:40px;
					font-size:30px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					margin-top:20px;
					
				}
				#d2{
					width:400px;
					height:35px;
					font-size:20px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					
				}
				#d3{
					width:400px;
					height:35px;
					font-size:20px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					
				}
				#d7{
					width:400px;
					height:35px;
					font-size:20px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					
				}
				#username{
					width:280px;
					height:30px;
					font-size:30px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					border-radius:5px;
					
				}
				#password1{
					width:280px;
					height:30px;
					font-size:30px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					border-radius:5px;
					
					
				}
				#password2{
					width:280px;
					height:30px;
					font-size:30px;
					font-family:Avenir,Helvetica,Arial,sans-serif;
					color:black;
					display:block;
					margin-left:40px;
					border-radius:5px;
					
					
				}
				#submit{
					width:237px;
					height:40px;
				    margin-top: 5px;
				    background-color: #47a0fc;
				    border: 1px solid #47a0fc;
				    color: #fff;
				    font-size: 20px;
				    cursor: pointer;
				    outline: none;
				    border-radius: 2px;
					display:block;
					margin-left:65px;
					margin-top:15px;
					border-radius:5px;
					
				}
				.bjimg {
				    position: fixed;
				    top: 0;
				    left: 0;
				    width: 100%;
				    height: 100%;
				    min-width: 1000px;
				    z-index: -10;
				    zoom: 1;
				    background-color: #fff;
				    background-repeat: no-repeat;
				    background-size: cover;
				    -webkit-background-size: cover;
				    -o-background-size: cover;
				    background-position: center 0;
					background-image:url("https://w.wallhaven.cc/full/ex/wallhaven-ex9ork.jpg")
					}
			</style>
		</head>
	<body>
		<div class="bjimg"></div>
		<div id=diceng>
			<div id=zhongceng>
					<div id=d1>欢迎注册优逸客实训平台</div>
					<div id=d2>用户名</div>
					<input type='text' placeholder="请输入用户名" id="username"></input>
					<div id=d3>密码</div>
					<input type="password" placeholder="请输入密码" id="password1"></input>
					<div id=d7>再次输入密码</div>
					<input type="password" placeholder="请重新输入密码" id="password2"></input>
					<button id=submit onclick="register()">注册</button>
			</div>
		</div>
	<script type="text/javascript">
    		function register(){
    			var username = $("#username")[0].value;
    			var password1 = $("#password1")[0].value;
    			var password2 = $("#password2")[0].value;
            $.ajax({
                url: "jdbcregister.jsp?username="+username+"&password1="+password1+"&password2="+password2,
                type: "GET",
                dataType:"json",
                success: function(res){
                	
                    if(res.msg == "success"){
                        window.location.href = "login.jsp";
                    }else{
                        alert("注册失败!");
                    }
                }
            })
    }
</script>
	</body>
</html>

jdbcregister.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.PrintWriter"%>
<%
    String username = request.getParameter("username");
    String password1 = request.getParameter("password1");
    String password2 = request.getParameter("password2");
    response.setCharacterEncoding("UTF-8"); //设置交换数据时的数据格式
    PrintWriter writer = response.getWriter();
    if(password1.equals(password2)){
	    Class.forName("com.mysql.jdbc.Driver");
	    
	    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/study?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "000000");
	    String sql = "Select * from user where username='"+username+"'";
	    Statement stat = conn.createStatement();
	    ResultSet rs = stat.executeQuery(sql);
	        if(rs.next()){
	        	 writer.write("{\"msg\":\"fail\"}");
	             writer.flush();
	        	}else{
	    		String sql2 ="insert into user(username,password) values('"+username+"','"+password1+"')";
	    		int num = stat.executeUpdate(sql2);
	            if(num > 0){
	               		writer.write("{\"msg\":\"success\"}");
	               		writer.flush();
	             	}else{
	               		writer.write("{\"msg\":\"fail\"}");
	               		writer.flush();
	             	}
	            }
	    	}
	        else{
	        	   writer.write("{\"msg\":\"fail\"}");
	        	   writer.flush();
	            }
             
%>

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<title>登陆成功</title>
		<style>
		body {
		background-image: url("https://w.wallhaven.cc/full/j3/wallhaven-j3m8y5.png");
		background-size: cover;
		background-position: center center;
		font-family: 'Microsoft YaHei', sans-serif;
		color: white;
		}
		#header {
		background-color: rgba(0, 0, 0, 0.5);
		padding: 20px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		}
		#header img {
		height: 80px;
		border-radius: 50%;
		margin-right: 20px;
		}
		h1 {
		font-size: 48px;
		margin: 0;
		font-weight: bold;
		font-style: italic;
		text-shadow: 2px 2px 4px #000000;
		}
		nav {
		background-color: rgba(0, 0, 0, 0.5);
		padding: 10px;
		display: flex;
		justify-content: center;
		}
		nav a {
		color: white;
		text-decoration: none;
		margin: 0 10px;
		font-size: 24px;
		font-weight: bold;
		text-transform: uppercase;
		letter-spacing: 2px;
		transition: all 0.3s ease;
		}
		nav a:hover {
		color: #00ff00;
		}
		</style>
	</head>
	<body>
		<header id="header">
			<img src="http://mms2.baidu.com/it/u=983221791,942132541&fm=253&app=138&f=JPEG&fmt=auto&q=75?w=320&h=307" alt="Avatar">
			<h1>登陆成功!</h1>
		</header>
			<nav>
			<a href="#">首页</a>
			<a href="#">关于</a>
			<a href="#">联系我</a>
			<a href="#">QQ</a>
			<a href="#">微信</a>
			</nav>
		<main>
			<p style="font-size: 60px; font-weight: bold;"></p>
		</main>
	</body>
</html>

Avatar

5.16笔记

一、JDBC回顾

JDBC技术Java用来连接操作数据库的工具,JDBC严格意义上属于Java的一种技术。JDBC连接操作数据库时,整体一共分为七步(需要引入编程依赖)。

1、加载驱动(告诉JDBC程序,连接的是哪一个数据库)

Class.forName(“驱动程序名”);
MySQL: com.mysql.jdbc.Driver com.mysql.cj.jdbc.Driver
Oracle: oracle.jdbc.driver.OracleDriver
SQL Server: com.microsoft.jdbc.sqlserver.SQLServerDriver

2、获取和数据库之间的连接—java.sql.DriverManager

Connection conn = DriverManager.getConnection(三个参数);
三个参数也成为数据库连接三要素:
URL:数据库的地址、连接的数据库的名字、连接数据库使用的参数
MySQL: jdbc:mysql://ip:port/databaseName?key=value&key=value
Oracle: jdbc:oracle:thin:@IP地址:端口号:数据库名
SQL Server: jdbc:microsoft:sqlserver://IP地址:端口号;DatabaseName=数据库名
用户名: 数据库的用户名
密码:数据库的密码

3、准备SQL语句

String sql = “xxxxx”;
JDBC操作数据库时,一般执行的SQL语句都是DMLDQL类型的语言。
SQL语句中存在一些字符串,字符串最好使用单引号

4、创建小推车—java.sql.Statement

Statement stat = conn.createStatement();
小推车是JDBC的核心,SQL语句的执行以及执行结果的返回都是Statement实现的

5、小推车带着SQL语句去数据库执行SQL语句,并且返回执行结果

JDBC操作SQL,SQL一般分为两类:DMLDQL,两类SQL语句的执行方式以及返回结果都是不同的。
执行DML类型的SQL:--返回的是一个数字,这个数字代表数据库受影响的行数
int num = stat.executeUpdate(DMLSQL);
执行DQL类型SQL—返回的是一个ResultSet结果集,结果集就是查询回来的虚拟表格,
ResultSet rs = stat.executeQuery(DQLSQL);

6、Java程序处理逻辑

如果执行的是DML类型的SQL语句,Java程序只需要判断是否执行成功即可
如果执行的是DQL类型的SQL语句,Java程序需要获取ResultSet结果集当中封装的虚拟表格数据。

7、释放JDBC程序使用的资源

ResultSet Statement Connection
如果执行的是DML类型的SQL,只需要释放两个Statement Connection
如果执行的是DQL类型的SQL,需要释放这三个

释放顺序:先创建的后释放后创建的先释放
释放调用这三者的close()方法即可。文章来源地址https://www.toymoban.com/news/detail-448875.html

到了这里,关于JSP_5.16_课堂笔记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 云计算课堂笔记——参考模型

    12.9参考模型 OSI(open system internet)七层参考模型(不管厂商怎么弄,就要按七层模型来弄(同一标准)) OSI/RM(参考模型) ISO--国际公有化组织——提出网络参考模型标准(统一标准) 分层的意义: 1.降低层次之间关联性,上一层都在下层基础上提供增值服务 2.大而化小的思

    2024年02月02日
    浏览(36)
  • mysql课堂笔记 mac

    目录 启动mac上的mysql 进入mysql mac windows 创建数据库 创建表 修改字段数据类型 修改字段名 增加字段 删除字段 启动mac上的mysql 直接输入你的开机密码即可。 编辑 进入mysql mac windows (root为你的用户名) 输入密码 创建数据库 创建表 示例: 没有加 ; 回车意味着这一句语句还没

    2024年02月09日
    浏览(30)
  • 人工智能导论课堂笔记

    时间:2022年10月19日下午 班级:2022级人工智能应用技术1班 作业问题: Python安装注意事项 1.下载Python3.X的版本,如:3.10, 3.9, 3.8,不推荐下载2.7版本(已经不使用) 2.在命令行中,无法运行path-添加,需要知道安装的路径; Pycharm安装注意: 1.官网下载,推荐下载免费(社区

    2024年02月01日
    浏览(32)
  • 03-JVM虚拟机-课堂笔记

    灵魂三问: JVM是什么? JVM广义上指的是一种规范。狭义上的是JDK中的JVM虚拟机。 为什么要学习JVM? 面试过程中,经常会被问到JVM。 研发过程中,肯定会面临一些重难点问题与JVM有关系。例如:线程死锁、内存溢出、项目性能优化等等。 基础不牢,地动山摇。想深入掌握

    2024年01月16日
    浏览(33)
  • 【云计算与大数据概述 】课堂笔记

    1.1 云计算基础 1.1.1 云计算简介 云计算的技术内容包括分布式计算技术,虚拟化技术,网络技术,服务器技术,数据中心技术,云计算平台技术,存储技术等 云计算的定义:一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需求提供给计算机和其他

    2024年02月06日
    浏览(32)
  • 云计算与虚拟化技术【课堂笔记】

    鲲鹏产业学院 云计算与虚拟化技术 课堂笔记 目录 一、云计算系统及演进 云计算的定义 云计算的三个阶段 云计算的三种服务模式 云计算的四种部署模型 二、未完待续 总结          云计算的本质是一种服务提供模型 ,通过这种模型可以随时、随地、按需地通过网络访

    2024年02月01日
    浏览(31)
  • linux复习笔记01(小滴课堂)

           点击下一步在自定义硬件中: 我们可以删除我们不使用的,后续如果需要再加上即可。    然后我们就可以开启这台虚拟机了。 我们可以进行下载cetos7.   这里选择简体中文就可以。   时间的设置。   可以开启下网络。   在这里选择设置root密码,我设置的密码是

    2024年02月10日
    浏览(24)
  • redis复习笔记06(小滴课堂)

    分布式锁核心知识介绍和注意事项 基于Redis实现分布式锁的几种坑 综合伪代码: 运行:

    2024年02月22日
    浏览(27)
  • alibaba学习笔记03(小滴课堂)

    自定义Ribbon负载均衡策略实战 启动3个视频服务和一个订单服务: 我们可以看到它是随机调用的。 也可以使用其他负载均衡策略。 讲解新一代负载均衡组件feign介绍 这种方式去写死接口肯定是不妥当的。 于是我们使用feign负载均衡组件: 改造微服务 集成Feign实现远程方法调

    2024年01月19日
    浏览(30)
  • JS课堂笔记(4.17-4.21)

    1.在程序中,一组被重复执行的语句被称为循环体,能否继续重复执行,取决于循环的终止条件。由循环体及循环的终止条件组成的语句,被称为循环语句。 2.循环执行的过程是①第一次循环:第一次赋值,然后条件判断,执行循环体,最后执行累计。 ②非第一次循环:条件

    2023年04月23日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包