完整的可以與資料庫連接的登錄界面的代碼
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="https://www.cnblogs.com/ningbaby/archive/2023/05/18/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 ></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 = "https://www.cnblogs.com/ningbaby/archive/2023/05/18/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="https://www.cnblogs.com/ningbaby/archive/2023/05/18/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 ></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 = "https://www.cnblogs.com/ningbaby/archive/2023/05/18/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="https://www.cnblogs.com/ningbaby/archive/2023/05/18/#">首頁</a>
<a href="https://www.cnblogs.com/ningbaby/archive/2023/05/18/#">關于</a>
<a href="https://www.cnblogs.com/ningbaby/archive/2023/05/18/#">聯系我</a>
<a href="https://www.cnblogs.com/ningbaby/archive/2023/05/18/#">QQ</a>
<a href="https://www.cnblogs.com/ningbaby/archive/2023/05/18/#">微信</a>
</nav>
<main>
<p style="font-size: 60px; font-weight: bold;"></p>
</main>
</body>
</html>
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陳述句都是DML和DQL型別的語言,
SQL陳述句中存在一些字串,字串最好使用單引號,
4、創建小推車—java.sql.Statement
Statement stat = conn.createStatement();
小推車是JDBC的核心,SQL陳述句的執行以及執行結果的回傳都是Statement實作的
5、小推車帶著SQL陳述句去資料庫執行SQL陳述句,并且回傳執行結果
JDBC操作SQL,SQL一般分為兩類:DML、DQL,兩類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()方法即可,
本文來自博客園,作者:qi_fen_zhong,轉載請注明原文鏈接:https://www.cnblogs.com/ningbaby/p/17411003.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/552862.html
標籤:其他
上一篇:HTTP1.0、HTTP1.1、HTTP2.0 協議的特點
下一篇:返回列表
