javaWeb連接數(shù)據(jù)庫實現(xiàn)簡單登陸注冊功能的全過程
實現(xiàn)步驟
創(chuàng)建maven項目 配置Tomcat 這些都不細說了
jar包
因為要連接數(shù)據(jù)庫所以一定要在maven項目下的xml文件下配置數(shù)據(jù)庫的jar包依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
這個千萬不要忘了 重重之重
數(shù)據(jù)庫的驅(qū)動以及用戶密碼
package com.li.Servlet.blit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBC {
private static String url = null;
private static String name = null;
private static String password = null;
static {
try {
Class.forName ( "com.mysql.cj.jdbc.Driver" );
} catch (Exception e) {
e.printStackTrace ( );
}
url = "jdbc:mysql://localhost:3306/zhiqingli?useUnicode=true&characterEncoding=utf8&useSSL=true";
name = "root";
password = "root";
}
public static Connection getConn() throws Exception {
return DriverManager.getConnection ( url, name, password );
}
public static void release(Connection conn, PreparedStatement pst, ResultSet res) {
try {
if (res != null) {
res.close ( );
}
if (pst != null) {
pst.close ( );
}
if (conn != null) {
conn.close ( );
}
} catch (Exception e) {
e.printStackTrace ( );
}
}
}
這個類可以直接復(fù)制使用,但要改成自己的數(shù)據(jù)庫名字
jsp頁面
登陸頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<form action="${pageContext.request.contextPath}/test" method="get">
<h3>賬號:<input type="text" name="account"></h3>
<h3>密碼:<input type="password" name="paw"></h3>
<h3><input type="submit" value="登陸"><a href="Register.jsp" rel="external nofollow" target="_blank">注冊</a></h3>
</form>
</body>
</html>注冊頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>注冊</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/res" method="post">
<h3>賬號:<input type="text" placeholder="請輸入注冊的賬號" name="number"></h3>
<h3>密碼:<input type="password" placeholder="請輸入注冊的密碼" name="word" id="one"></h3>
<h3>確認密碼:<input type="password" placeholder="確認一遍密碼" name="u" id="two"></h3>
<input type="submit" value="提交" οnclick="show()">
</form>
<Script>
function show(){
var one = document.getElementById("one").value;
var two = document.getElementById("two").value;
if (one === two){
alert("注冊成功");
window.open("index.jsp")
}else {
alert("兩次輸入的密碼不一致");
}
}
</Script>
</body>
</html>
登陸成功和失敗的頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>成功頁面</title> </head> <body> <h1>登陸成功,感謝使用</h1> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>失敗頁面</title> </head> <body> <h1>登陸失敗,賬號密碼找不到或者錯誤</h1> </body> </html>
工具類
工具人上線
//登陸使用的方法
Connection conn = null;
PreparedStatement pst = null;
ResultSet res = null;
boolean isQ;
public boolean Select(String username,String paw) {
try {
conn = JDBC.getConn ( );
String sql = "SELECT * FROM `username` where `name`=? and `password`=?";
pst = conn.prepareStatement (sql);
pst.setObject ( 1,username );
pst.setObject ( 2,paw );
res = pst.executeQuery ( );
if (res.next ()){
isQ = true;
System.out.println (res.getObject ( "name" )+" "+res.getObject ( "password" ));
System.out.println ("登陸成功");
}else {
isQ = false;
System.out.println ("登陸失敗");
}
}catch (Exception e){
e.printStackTrace ();
}finally {
JDBC.release ( conn,pst,res );
}
return isQ;
}
//注冊用的方法
public void addUser(String name,String paw){
try {
conn = JDBC.getConn ( );
String sql = "insert into `username`(`name`,`password`) values (?,?)";
pst = conn.prepareStatement ( sql );
pst.setObject ( 1,name );
pst.setObject ( 2,paw );
int i = pst.executeUpdate ( );
if (i>0){
System.out.println ("添加成功");
}
}catch (Exception e){
e.printStackTrace ();
}finally {
JDBC.release ( conn,pst,res );
}
}
登陸的類
package com.li.Servlet.test;
import com.li.Servlet.blit.Way;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class index extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String account = req.getParameter ( "account" );
String paw = req.getParameter ( "paw" );
Way way = new Way ();
if (way.Select ( account,paw )){
resp.sendRedirect ( "/s4/true.jsp" );
}else {
resp.sendRedirect ( "/s4/false.jsp" );
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet ( req, resp );
}
}
注冊的類
package com.li.Servlet.test;
import com.li.Servlet.blit.Way;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class index02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String number = req.getParameter ( "number" );
String word = req.getParameter ( "word" );
String u = req.getParameter ( "u" );
if (word.equals ( u )){
Way way = new Way ( );
way.addUser ( number,word );
}else {
//當兩次密碼不一致的時候瀏覽器響應(yīng)給用戶當前注冊的頁面
resp.sendRedirect ( "/s4/Register.jsp" );
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet ( req, resp );
}
}
兩個映射路徑
<!--登陸-->
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.li.Servlet.test.index</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<!--注冊-->
<servlet>
<servlet-name>res</servlet-name>
<servlet-class>com.li.Servlet.test.index02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>res</servlet-name>
<url-pattern>/res</url-pattern>
</servlet-mapping>
思路
登陸
這個就是用戶輸入的賬號和密碼看看數(shù)據(jù)庫里有沒有,有的話就重定向到成功或這失敗的頁面,相當于瀏覽器響應(yīng)給用戶的請求結(jié)果
注冊
相當于就是往數(shù)據(jù)庫里面添加一個賬號或者密碼然返回到登陸頁面
這里我在注冊類面沒有用到重定向,而是在注冊的jsp頁面里判斷之后跳轉(zhuǎn)到登陸頁面
注意:這里為什么沒在注冊類里面用重定向
因為使用重定向之后就直接返回了登陸類運行的結(jié)果
總結(jié)
到此這篇關(guān)于javaWeb連接數(shù)據(jù)庫實現(xiàn)簡單登陸注冊功能的文章就介紹到這了,更多相關(guān)javaWeb登陸注冊功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring中@ExceptionHandler注解的使用方式
這篇文章主要介紹了Spring中@ExceptionHandler注解的使用方式,@ExceptionHandler注解我們一般是用來自定義異常的,可以認為它是一個異常攔截器(處理器),需要的朋友可以參考下2024-01-01
Spring boot中PropertySource注解的使用方法詳解
這篇文章主要給大家介紹了關(guān)于Spring boot中PropertySource注解的使用方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
java實現(xiàn)兩張圖片2D翻轉(zhuǎn)動畫效果
這篇文章主要為大家詳細介紹了java實現(xiàn)兩張圖片2D翻轉(zhuǎn)動畫效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08
sftp和ftp 根據(jù)配置遠程服務(wù)器地址下載文件到當前服務(wù)
這篇文章主要介紹了sftp和ftp 根據(jù)配置遠程服務(wù)器地址下載文件到當前服務(wù)的相關(guān)資料本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下2016-10-10
Springboot單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口
這篇文章主要介紹了Springboot單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù)
這篇文章主要介紹了Mybatis-Plus實現(xiàn)只更新部分字段的數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

