Servlet連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶登錄的實(shí)現(xiàn)示例
做一個(gè)小案例順便復(fù)習(xí)一下jdbc的知識(shí)
一、需求:
用戶在瀏覽器輸入用戶名和密碼,如果數(shù)據(jù)庫(kù)中有數(shù)據(jù),提醒用戶登錄成功,如果沒(méi)有數(shù)據(jù),提醒用戶重新登錄
二、先復(fù)習(xí)一下JDBC
今天用的時(shí)候有些細(xì)節(jié)忘了,知識(shí)還是需要多次復(fù)習(xí)啊,并不是說(shuō)學(xué)過(guò)就是自己的。
1.概述:
用java程序操作數(shù)據(jù)庫(kù)的一個(gè)技術(shù),是java程序連接數(shù)據(jù)庫(kù)的一套標(biāo)準(zhǔn),本質(zhì)上就是一堆API。
2.開(kāi)發(fā)步驟:
2.1.導(dǎo)jar包:
對(duì)于java項(xiàng)目,直接將jar包復(fù)制到項(xiàng)目然后解析jar(add as library)就可以用了;對(duì)于web項(xiàng)目,需要將jar包放到tomcat的lib目c錄下,在web項(xiàng)目中,當(dāng)Class.forName(“com.mysql.jdbc.Driver”);時(shí)idea是不會(huì)去查找字符串,不會(huì)去查找驅(qū)動(dòng)的。所以只需要把mysql-connector-java-5.1.7-bin.jar拷貝到tomcat下lib目錄就可以了。今天這里出了問(wèn)題,找到這種解決方案。
2.2.步驟:具體步驟都在代碼里
注冊(cè)驅(qū)動(dòng)—>
Class.forName(“com.mysql.jdbc.Driver”);
獲取連接—>
String url = “協(xié)議://IP地址:端口號(hào)/數(shù)據(jù)庫(kù)的名字/”; String url = “jdbc:mysql://localhost:3306/person”; Connection c = DriverManager.getConnection(url, “root”, “root”);
寫(xiě)sql語(yǔ)句—>
String sql = "select * from user where name = and pwd = ";
獲取傳輸器—>
PreparedStatement preparedStatement = connection.prepareStatement(sql);
設(shè)置值—>
preparedStatement.setObject(1,username); preparedStatement.setObject(2,pwd);
獲取結(jié)果集—>
ResultSet resultSet = preparedStatement.executeQuery();
解析結(jié)果集—>
resultSet.next()
三、代碼實(shí)現(xiàn):
1.登錄界面代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶登錄界面</title> </head> <body> <h3 style="text-align: center">用戶登錄</h3> <form action="userLogin" method="post" style="text-align: center" > 用戶名:<input type="text" name="username"><br/> <br/> 密 碼:<input type="password" name="pwd"><br/> <br> <input type="submit" value="提交"> </form> </body> </html>
2.登錄成功界面:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>成功登錄界面</title> </head> <body> <h1> 恭喜登錄成功!?。?lt;/h1> </body> </html>
3.servlet代碼:
package cn.tedu; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.*; //配置訪問(wèn)路徑 @WebServlet(urlPatterns = "/userLogin") public class ServletLogin extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.解決中文亂碼 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset = utf-8"); //2.獲取用戶輸入的名字和密碼 String username = request.getParameter("username"); String pwd = request.getParameter("pwd"); Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; //3.連接數(shù)據(jù)庫(kù) try { //3.1注冊(cè)驅(qū)動(dòng) Class.forName("com.mysql.jdbc.Driver"); //3.2獲取連接 String url = "jdbc:mysql://localhost:3306/person"; connection = DriverManager.getConnection(url,"root","root"); //3.3寫(xiě)sql String sql = "select * from user where name = ? and pwd = ?"; //3.4獲取傳輸器 preparedStatement = connection.prepareStatement(sql); //3.5設(shè)置值 preparedStatement.setObject(1,username); preparedStatement.setObject(2,pwd); //3.6返回結(jié)果集 resultSet = preparedStatement.executeQuery(); if (resultSet.next()){ //重定位,如果結(jié)果返回true,"跳轉(zhuǎn)"到success.html response.sendRedirect("success.html"); }else{ String urls = "login.html"; response.getWriter().write("用戶不存在"+""+"<a href = '"+urls+"'>點(diǎn)擊重新登錄</a>"); } } catch (Exception e) { e.printStackTrace(); //3.6關(guān)閉資源 }finally { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
4.數(shù)據(jù)庫(kù)數(shù)據(jù)
5.登錄成功頁(yè)面:
6.登錄失敗界面:
7.登錄界面:
到此這篇關(guān)于Servlet連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶登錄的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Servle 用戶登錄內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven的pom.xml中resources標(biāo)簽的用法
本文主要介紹了Maven的pom.xml中resources標(biāo)簽的用法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解
這篇文章主要為大家介紹了Apache?SkyWalking?修復(fù)TTL?timer?失效bug詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對(duì)象傳遞示例
本文主要介紹了webservice實(shí)現(xiàn)springboot項(xiàng)目間接口調(diào)用與對(duì)象傳遞示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07spring boot實(shí)現(xiàn)軟刪除的示例代碼
這篇文章主要介紹了spring boot實(shí)現(xiàn)軟刪除的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Java如何實(shí)現(xiàn)HTTP斷點(diǎn)續(xù)傳功能
其實(shí)斷點(diǎn)續(xù)傳的原理很簡(jiǎn)單,就是在Http的請(qǐng)求上和一般的下載有所不同而已,本文將詳細(xì)介紹Java如何實(shí)現(xiàn)HTTP斷點(diǎn)續(xù)傳功能,需要的朋友可以參考下2012-11-11淺談Spring中幾個(gè)PostProcessor的區(qū)別與聯(lián)系
這篇文章主要介紹了淺談Spring中幾個(gè)PostProcessor的區(qū)別與聯(lián)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴
這篇文章主要介紹了關(guān)于Spring源碼是如何解決Bean的循環(huán)依賴,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢
這篇文章主要介紹了Mybatis 實(shí)現(xiàn)一個(gè)搜索框?qū)Χ鄠€(gè)字段進(jìn)行模糊查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01