eclipse連接數(shù)據(jù)庫并實現(xiàn)用戶注冊登錄功能
文章目錄 MySQL配置環(huán)境變量navicat部署tomcat導入驅(qū)動修改代碼連接mysql數(shù)據(jù)庫運行:
MySQL配置環(huán)境變量
mysql肯定是必不可少的,這個就不用多說了,自己去官網(wǎng)下載就行我們來配置一下mysql的環(huán)境變量
步驟:
我的電腦右鍵——屬性——高級系統(tǒng)——環(huán)境變量

在系統(tǒng)變量下點擊新建:

變量名:MYSQL_HOME
變量值:C:\phpStudy\PHPTutorial\MySQL
這里的變量值填的是自己mysql所在的文件夾,以自己電腦路徑為準

然后找到path,點擊編輯,然后新建一個
%MYSQL_HOME%\bin

點擊確定(共三個)
測試
在windows命令行下輸入
mysql -u root -p
按下回車,輸入密碼即可進入數(shù)據(jù)庫

navicat
我自己電腦用的是phpstudy,感覺更方便一點,集成環(huán)境,并且有可視化工具,這里推薦使用navicat(有破解教程)
鏈接: https://pan.baidu.com/s/1dpNhsSUy_yAw-qi63toSzQ
提取碼: wumm
部署tomcat
我們點擊菜單欄windows>>preference>>在左邊欄找到server>>Runtime Environme

點擊add,添加tomcat

選擇需要的版本,點擊next,選擇自己安裝的tomcat路徑

點擊finish:

tomcat版本不要太高,不然可能會出現(xiàn)報錯;
新建一個web項目


其他選擇默認,next>>next>>next>>finish
將tomcat服務器顯示在控制臺上,以及將web應用部署到tomcat中
1、window>>show view>>servers
2、點擊控制臺鏈接:No servers are available. Click ths link to create a new server.
3、在彈出的對話框中選擇tomcat版本
4、點擊next,添加我們的項目:選中我們的項目,點擊add,finish。
我們會在右邊菜單欄看到文件夾servers,里邊就是tomcat的一些相關(guān)文件
右鍵面板中的tomcat v8.0…點擊start,開啟服務器即可

導入驅(qū)動
導入的驅(qū)動一定不能過高,最好和自己電腦mysql版本一致
我電腦上的mysql是5.5.53,用的驅(qū)動是5.1.47,可用!

鏈接: https://pan.baidu.com/s/1senUiVzre2B7It-TIdjO5w
提取碼: 5ksf
下載好解壓,直接將jar包拖入lib中,右鍵build path一下

這樣就是導入成功!
修改代碼
找到我們創(chuàng)建的inc.jsp,修改下列代碼,以自己電腦為準,這個是和代碼相匹配的

另外,所有web文件都要放在WEB-INF下

連接mysql數(shù)據(jù)庫
連接名隨意,默認端口為3306

在數(shù)據(jù)庫中我們要創(chuàng)建一個數(shù)據(jù)庫庫名為demo,表名為users,
添加字段:username、password、email

這就相當于登陸的用戶名和密碼,可直接在數(shù)據(jù)庫中添加,也可通過注冊界面
填寫注冊信息,同樣會顯示在數(shù)據(jù)庫中
運行:


(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" >注冊新用戶</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">注冊窗口</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>確認密碼</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="注冊"> <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ù)庫并實現(xiàn)用戶注冊登錄功能的文章就介紹到這了,更多相關(guān)eclipse連接數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea查看properties中文變成unicode碼的解決方案
這篇文章主要介紹了idea查看properties中文變成unicode碼的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
java 使用JDBC構(gòu)建簡單的數(shù)據(jù)訪問層實例詳解
以下是如何使用JDBC構(gòu)建一個數(shù)據(jù)訪問層,包括數(shù)據(jù)轉(zhuǎn)換(將從數(shù)據(jù)庫中查詢的數(shù)據(jù)封裝到對應的對象中……),數(shù)據(jù)庫的建立,以及如何連接到數(shù)據(jù)庫,需要的朋友可以參考下2016-11-11
java中timer的schedule和scheduleAtFixedRate方法區(qū)別詳解
這篇文章主要為大家詳細介紹了java中timer的schedule和scheduleAtFixedRate方法區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
簡單講解Android開發(fā)中觸摸和點擊事件的相關(guān)編程方法
這篇文章主要介紹了Android開發(fā)中觸摸和點擊事件的相關(guān)編程方法,包括事件偵聽器等安卓開發(fā)中常用的接口的基本使用方法,需要的朋友可以參考下2015-12-12

