eclipse連接數(shù)據(jù)庫(kù)并實(shí)現(xiàn)用戶注冊(cè)登錄功能
文章目錄 MySQL配置環(huán)境變量navicat部署tomcat導(dǎo)入驅(qū)動(dòng)修改代碼連接mysql數(shù)據(jù)庫(kù)運(yùn)行:
MySQL配置環(huán)境變量
mysql肯定是必不可少的,這個(gè)就不用多說(shuō)了,自己去官網(wǎng)下載就行我們來(lái)配置一下mysql的環(huán)境變量
步驟:
我的電腦右鍵——屬性——高級(jí)系統(tǒng)——環(huán)境變量
在系統(tǒng)變量下點(diǎn)擊新建:
變量名:MYSQL_HOME
變量值:C:\phpStudy\PHPTutorial\MySQL
這里的變量值填的是自己mysql所在的文件夾,以自己電腦路徑為準(zhǔn)
然后找到path,點(diǎn)擊編輯,然后新建一個(gè)
%MYSQL_HOME%\bin
點(diǎn)擊確定(共三個(gè))
測(cè)試
在windows命令行下輸入
mysql -u root -p
按下回車,輸入密碼即可進(jìn)入數(shù)據(jù)庫(kù)
navicat
我自己電腦用的是phpstudy,感覺(jué)更方便一點(diǎn),集成環(huán)境,并且有可視化工具,這里推薦使用navicat(有破解教程)
鏈接: https://pan.baidu.com/s/1dpNhsSUy_yAw-qi63toSzQ
提取碼: wumm
部署tomcat
我們點(diǎn)擊菜單欄windows>>preference>>在左邊欄找到server>>Runtime Environme
點(diǎn)擊add,添加tomcat
選擇需要的版本,點(diǎn)擊next,選擇自己安裝的tomcat路徑
點(diǎn)擊finish:
tomcat版本不要太高,不然可能會(huì)出現(xiàn)報(bào)錯(cuò);
新建一個(gè)web項(xiàng)目
其他選擇默認(rèn),next>>next>>next>>finish
將tomcat服務(wù)器顯示在控制臺(tái)上,以及將web應(yīng)用部署到tomcat中
1、window>>show view>>servers
2、點(diǎn)擊控制臺(tái)鏈接:No servers are available. Click ths link to create a new server.
3、在彈出的對(duì)話框中選擇tomcat版本
4、點(diǎn)擊next,添加我們的項(xiàng)目:選中我們的項(xiàng)目,點(diǎn)擊add,finish。
我們會(huì)在右邊菜單欄看到文件夾servers,里邊就是tomcat的一些相關(guān)文件
右鍵面板中的tomcat v8.0…點(diǎn)擊start,開(kāi)啟服務(wù)器即可
導(dǎo)入驅(qū)動(dòng)
導(dǎo)入的驅(qū)動(dòng)一定不能過(guò)高,最好和自己電腦mysql版本一致
我電腦上的mysql是5.5.53,用的驅(qū)動(dòng)是5.1.47,可用!
鏈接: https://pan.baidu.com/s/1senUiVzre2B7It-TIdjO5w
提取碼: 5ksf
下載好解壓,直接將jar包拖入lib中,右鍵build path一下
這樣就是導(dǎo)入成功!
修改代碼
找到我們創(chuàng)建的inc.jsp,修改下列代碼,以自己電腦為準(zhǔn),這個(gè)是和代碼相匹配的
另外,所有web文件都要放在WEB-INF下
連接mysql數(shù)據(jù)庫(kù)
連接名隨意,默認(rèn)端口為3306
在數(shù)據(jù)庫(kù)中我們要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫(kù)庫(kù)名為demo,表名為users,
添加字段:username、password、email
這就相當(dāng)于登陸的用戶名和密碼,可直接在數(shù)據(jù)庫(kù)中添加,也可通過(guò)注冊(cè)界面
填寫(xiě)注冊(cè)信息,同樣會(huì)顯示在數(shù)據(jù)庫(kù)中
運(yùn)行:
(1)login.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <form name="form1" action="login_action.jsp" method="post"> <table width="200" border="1"> <tr> <td colspan="2">登錄窗口</td> <tr> <td>用戶名</td> <td><input type="text" name="username" size="10"></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password" size="10"></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="登錄"> <a href="register.jsp" rel="external nofollow" >注冊(cè)新用戶</a></td> </tr> </table> </form> </body> </html> (2)login_action.jsp <%@ page pageEncoding="utf-8" %> <%@ include file="inc.jsp"%> <% //get parameters String username = request.getParameter("username"); String password = request.getParameter("password"); //check null if (username == null || password == null) { response.sendRedirect("login.jsp"); } //validate boolean isValid = false; String sql = "select * from users where username='"+username+"' and password='"+password+"'"; try { Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url, usr, pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(rs.next())isValid = true; rs.close(); stm.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); out.println(e); } finally { } if (isValid) { session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { response.sendRedirect("login.jsp"); } %> (3)register.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <form name="form1" action="register_action.jsp" method="post"> <table width="200" border="1"> <tr> <td colspan="2">注冊(cè)窗口</td> <tr> <td>用戶名</td> <td><input type="text" name="username" size="10"></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password1" size="10"></td> </tr> <tr> <td>確認(rèn)密碼</td> <td><input type="password" name="password2" size="10"></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" size="10"></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="注冊(cè)"> <a href="login.jsp" rel="external nofollow" >返回</a></td> </tr> </table> </form> </body> </html> (4)register_action.jsp <%@ page pageEncoding="utf-8" %> <%@ include file="inc.jsp"%> <% //get parameters String username = request.getParameter("username"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); String email = request.getParameter("email"); //check null if (username == null || password1 == null || password2 == null || !password1.equals(password2)) { response.sendRedirect("register.jsp"); } //validate boolean isValid = false; String sql = "select * from users where username='"+username+"'"; try { Class.forName(drv).newInstance(); Connection conn = DriverManager.getConnection(url, usr, pwd); Statement stm = conn.createStatement(); ResultSet rs = stm.executeQuery(sql); if(!rs.next()) { sql = "insert into users(username,password,email) values('"+username+"','"+password1+"','"+email+"')"; stm.execute(sql); isValid = true; } rs.close(); stm.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); out.println(e); } if (isValid) { response.sendRedirect("login.jsp"); } else { response.sendRedirect("register.jsp"); } %> (5)welcome.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'welcome.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> <style type="text/css"> body{ background-color: beige; } </style> </head> <body> <table width="100%"> <tr> <td><img src="images/111.jpgA,LDYVS." height="90"></td> </tr> <tr> <td colspan="2"> <hr> </td> </tr> <tr> <td> <table> <tr> <td><a href="welcome.jsp" rel="external nofollow" >Main</a></td> </tr> <tr> <td><a href="menu1.jsp" rel="external nofollow" >Menu1</a></td> </tr> <tr> <td><a href="menu2.jsp" rel="external nofollow" >Menu2</a></td> </tr> <tr> <td><a href="menu3.jsp" rel="external nofollow" >Menu3</a></td> </tr> <tr> <td><a href="menu4.jsp" rel="external nofollow" >Menu4</a></td> </tr> <tr> <td><a href="menu5.jsp" rel="external nofollow" >Menu5</a></td> </tr> <tr> <td><a href="menu6.jsp" rel="external nofollow" >Menu6</a></td> </tr> <tr> <td><a href="menu7.jsp" rel="external nofollow" >Menu7</a></td> </tr> <tr> <td><a href="menu8.jsp" rel="external nofollow" >Menu8</a></td> </tr> </table> </td> <td> <form name="form1" action="logout.jsp" method="post"> <table width="200" border="1"> <tr> <td colspan="2">登錄成功</td> <tr> <td>歡迎你,</td> <td><%=(String) session.getAttribute("username")%></td> </tr> <tr> <td colspan="2"><input type="submit" name="submit" value="退出"></td> </tr> </table> </form> </td> </tr> </table> </body> </html> (6)logout.jsp <% session.removeAttribute("username"); response.sendRedirect("login.jsp"); %> (7)inc.jsp <%@ page import="java.sql.Connection"%> <%@ page import="java.sql.DriverManager"%> <%@ page import="java.sql.Statement"%> <%@ page import="java.sql.ResultSet"%> <%@ page import="java.sql.ResultSetMetaData"%> <% String drv = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Demo"; String usr = "sa"; String pwd = ""; %>
到此這篇關(guān)于eclipse連接數(shù)據(jù)庫(kù)并實(shí)現(xiàn)用戶注冊(cè)登錄功能的文章就介紹到這了,更多相關(guān)eclipse連接數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用feign發(fā)送http請(qǐng)求解析報(bào)錯(cuò)的問(wèn)題
這篇文章主要介紹了使用feign發(fā)送http請(qǐng)求解析報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03解決@Autowired注入static接口的問(wèn)題
這篇文章主要介紹了解決@Autowired注入static接口的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06java 反射和動(dòng)態(tài)代理詳解及實(shí)例代碼
這篇文章主要介紹了java 反射和動(dòng)態(tài)代理詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09使用springboot整合RateLimiter限流過(guò)程
這篇文章主要介紹了使用springboot整合RateLimiter限流過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06java 使用JDBC構(gòu)建簡(jiǎn)單的數(shù)據(jù)訪問(wèn)層實(shí)例詳解
以下是如何使用JDBC構(gòu)建一個(gè)數(shù)據(jù)訪問(wèn)層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫(kù)中查詢的數(shù)據(jù)封裝到對(duì)應(yīng)的對(duì)象中……),數(shù)據(jù)庫(kù)的建立,以及如何連接到數(shù)據(jù)庫(kù),需要的朋友可以參考下2016-11-11java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了java中timer的schedule和scheduleAtFixedRate方法區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12簡(jiǎn)單講解Android開(kāi)發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法
這篇文章主要介紹了Android開(kāi)發(fā)中觸摸和點(diǎn)擊事件的相關(guān)編程方法,包括事件偵聽(tīng)器等安卓開(kāi)發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12