javaWeb連接數據庫實現簡單登陸註冊功能的全過程
實現步驟
創建maven項目 配置Tomcat 這些都不細說瞭
jar包
因為要連接數據庫所以一定要在maven項目下的xml文件下配置數據庫的jar包依賴
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
這個千萬不要忘瞭 重重之重
數據庫的驅動以及用戶密碼
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 ( ); } } }
這個類可以直接復制使用,但要改成自己的數據庫名字
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 { //當兩次密碼不一致的時候瀏覽器響應給用戶當前註冊的頁面 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>
思路
登陸
這個就是用戶輸入的賬號和密碼看看數據庫裡有沒有,有的話就重定向到成功或這失敗的頁面,相當於瀏覽器響應給用戶的請求結果
註冊
相當於就是往數據庫裡面添加一個賬號或者密碼然返回到登陸頁面
這裡我在註冊類面沒有用到重定向,而是在註冊的jsp頁面裡判斷之後跳轉到登陸頁面
註意:這裡為什麼沒在註冊類裡面用重定向
因為使用重定向之後就直接返回瞭登陸類運行的結果
總結
到此這篇關於javaWeb連接數據庫實現簡單登陸註冊功能的文章就介紹到這瞭,更多相關javaWeb登陸註冊功能內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!