刚学JAVA,属菜鸟级别人物,想开发一个WEB应用程序,但是java连接mysql不会,于是在网上搜了一堆,最后使用了我认为最简单的,做了一个简单的登陆模块,下面我把我写的源程序提供出来,方便初学者学习。
首先,我们先建一个title.jsp页面,代码如下:
1
<%@ page language="java" contentType="text/html; charset=UTF-8"2
pageEncoding="UTF-8"%>3
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">4
<html>5
<head>6
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">7
<title>Insert title here</title>8
<style type="text/css">9

.txt
{10
BORDER-RIGHT: #ffffff 1px groove;11
BORDER-TOP: #ffffff 1px groove;12
FONT: 12px Verdana, Geneva, sans-serif;13
BORDER-LEFT: #ffffff 1px groove;14
WIDTH: 100px;15
COLOR: #000000;16
BORDER-BOTTOM: #ffffff 1px groove;17
HEIGHT: 18px;18
BACKGROUND-COLOR: #DFF1F919
}20

21

.tableborder1
{22
BORDER: #ffffff 1px groove;23
BACKGROUND-COLOR: #DFF1F924
FONT: 12px Verdana, Geneva, sans-serif;25
COLOR: #000000;26
BACKGROUND-COLOR: #DFF1F927
}28
</style>29
</head>30
<body>31
<%32
boolean isLog = false;33

try
{34
isLog = ((String) session.getAttribute("isLog")).equals("1");35

} catch (Exception e)
{36

37
}38
%>39
<table width="842" align="center" cellpadding="3" cellspacing="1"40
class="">41
<tr>42
<td width="832" class="tableborder1">:: <a href="index.jsp"43
target="_top">首页</a> <a href="mail/index.jsp" target="_top">邮件</a> <a44
href="product/index.jsp" target="_top">商品搜索</a> <a45
href="jive/index.jsp" target="_top">论坛</a> <%46

if (isLog)
{47
%><a href="logout.jsp" target="_top">注销</a> <%48
}49
%>50
</td>51
</tr>52
<tr>53
<td width="832" class="tableborder1">54
<%55

if (isLog)
{56
%> 欢迎光临! <%=session.getAttribute("name")%>,您是第 <%=session.getAttribute("userLogCount")%>57
次登录,您上次登录的时间是: <%=session.getAttribute("userLastLogTime")%> <%58

} else
{59
%>60
<form name="form1" method="post" action="login.jsp">61
用户名 <input type="text" name="userId" size="15"62
class="txt"> 密码 <input type="password" name="password"63
size="15" class="txt">64
<input type="submit" name="Submit" value="确定">65
</form>66
<%67
}68
%>69
</td>70
</tr>71
</table>72
</body>73
</html>
isLog是为了记录用户是否登陆;
session.getAttribute("name"),
session.getAttribute("userLogCount"),
session.getAttribute("userLastLogTime"),这三个是获取从数据库中读取出来的用户名、登陆次数、上次登陆的时间,显示在页面上,具体与数据的操作会在后面谈到。
第一个页面建立完成,也相当于我们的外表收拾好了,那我们开始展示自己吧。
建立login.jsp页面,该页面用于与数据库操作,代码如下:
1
<%@ page language="java" contentType="text/html; charset=UTF-8"2
pageEncoding="UTF-8"%>3
<%@ page import="java.sql.*"%>4
<%@ page import="connection.*"%>5
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">6
<html>7
<head>8
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">9
<title>Insert title here</title>10
</head>11
<body>12
<%13
//获取用户输入的数据14
String id = request.getParameter("userId");15
String psw = request.getParameter("password");16

try
{17
//初始化DBcon类18
DBcon con = new DBcon();19
//调用类DBcon的Query方法,用于查询数据20
ResultSet rs = con21
.Query("select * from user_info where userID='" + id22
+ "' and password='" + psw + "'");23
session.setAttribute("isLog", new String("0"));24

25

if (!rs.next())
{26
//登陆失败,重新登陆27
response.sendRedirect("index.jsp");28
//调用类DBcon的Release方法,用于关闭所有连接29
con.Release();30

} else
{31
//登陆成功,获取数据32
session.setAttribute("id", rs.getInt("ID"));33
session.setAttribute("name", rs.getString("userID"));34
session.setAttribute("email", rs.getString("email"));35
//设置isLog值为1,表示用户登陆成功36
session.setAttribute("isLog", new String("1"));37
//获取用户登陆次数38
int count = rs.getInt("userLogCount");39
session.setAttribute("userLogCount", new Integer(count));40
//用户登陆成功,用户登陆次数加141
count++;42
session.setAttribute("userLastLogTime", rs43
.getString("userLastLogTime"));44
java.util.Date time1 = new java.util.Date();45
String sqltime = new Timestamp(time1.getTime()).toString();46
//更新用户登陆次数及最后登陆时间47
con.Update("update user_info set userLogCount=" + count48
+ ",userLastLogTime='" + sqltime49
+ "' where userID='" + id + "'");50
//调用类DBcon的Release方法,用于关闭所有连接51
con.Release();52
//登陆成功页面显示53
response.sendRedirect("index.jsp");54
}55

} catch (Exception e)
{56
out.println(e.getLocalizedMessage());57
}58
%>59
</body>60
</html>
上面基本上展示了你的实力了,但是是不是缺少点东西呢,也许你会纳闷我的数据库连接在哪里?呵呵…… 不急,下面就将为你奉上。不知道你有没有注意到上面的DBcon类,那个就是了!
建立一个connection包,然后在里面新建一个类:DBcon
1
package connection;2

3
import java.sql.*;4

5

public class DBcon
{6
ResultSet rs = null;7
Connection con = null;8
Statement stmt = null;9

10
// get connection11

public Statement getCon()
{12

13

try
{14
//使用jdbc-ODBC桥连接15
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");16

} catch (ClassNotFoundException e)
{17
System.out.println(e.getLocalizedMessage());18
}19

20

try
{21
//建立数据连接22
con = DriverManager23
.getConnection(24
"jdbc:odbc:Driver={SQL Server};Server=.;DataBase=Shoppings",25
"", "");26
stmt = con.createStatement();27

28

} catch (SQLException e)
{29
System.out.println(e.getLocalizedMessage());30
}31

32
return stmt;33
}34

35
// query user_info36

public ResultSet Query(String Strsql)
{37
stmt = getCon();38

try
{39
rs = stmt.executeQuery(Strsql);40

} catch (SQLException e)
{41
System.out.println(e.getLocalizedMessage());42
}43
return rs;44
}45

46
// update data47

public void Update(String Strsql)
{48
stmt = getCon();49

try
{50
stmt.execute(Strsql);51

} catch (SQLException e)
{52
System.out.println(e.getLocalizedMessage());53
}54
}55

56
//close all connection57

public void Release()
{58

try
{59
rs.close();60

} catch (SQLException e)
{61
System.out.println(e.getLocalizedMessage());62
}63

try
{64
stmt.close();65

} catch (SQLException e)
{66
System.out.println(e.getLocalizedMessage());67
}68

try
{69
con.close();70

} catch (SQLException e)
{71
System.out.println(e.getLocalizedMessage());72
}73
}74
}75

上面就是我封装的数据库操作类,功能不多,如果需要请自己扩充。好了,基本完成,那我们就来建议一个页面来显示吧。
建立一个index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="title.jsp" />
<center>
--- 主页 ---
</center>
</body>
</html>
这个就是我们的主页了。
<jsp:include page="title.jsp" />是为了把我们在前面所建立的title页面导进来。别忙着调试,不知道你在看login.jsp页面的时候有没有注意到我应用两个package,一个是sql,一个是我们自己建立的包connection,如果想与数据库交往sql是必须的。在DBcon类中我们也引用到了sql。
好了,如果你环境是配置成功的,现在你可以跑跑你的新作了。是不是发现有点不足呢,登陆而不能注销的连接,用户是不是很郁闷,所以,我们还差一个logout.jsp。代码很简单
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<%
session.invalidate();
response.sendRedirect("index.jsp");
%>
<br>
<br>
正在注销……
</center>
</body>
</html>
好了,现在我们的小程序基本上结束了,当然这只是为新手菜鸟级别的人物所准备的,方便新手们了解页面之前是如何传值和与数据库进行交往的,也是为了给自己作笔记,所以请高手们多多指正。
浙公网安备 33010602011771号