1、用户登录界面log.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312">
<title>用户登陆</title>
</head>
<body>
<form method="post" action="Login.jsp">
<DIV align="center">
<P>用户名<input type ="text" name="userName" value="" size="15" height="15"></P>
<P>密 码<input type ="password" name="Password1" value="" size="15" height="15"></P>
<P><INPUT type=submit value=登陆 name=B1> <INPUT type=reset value=重填 name=B2> <INPUT οnclick="window.open('newuser.jsp')" type=button value=注册 name=B3></P>
</DIV>
</form>
<form method="get" action="sh.jsp"><input type="submit" name="check" value="查询"></form>
</body>
</html>
2、处理用户登录的程序login.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@include file="ConnDB.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="robots" content="noindex,nofollow" />
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录检查</title>
</head>
<body>
<%
String userName = request.getParameter("userName");
String Password = request.getParameter("Password1");
Statement stmt_Check=conn.createStatement();
String sql = "select * from admin where userPwd='"+ Password + "' and userName='" + userName +"'";
ResultSet RsLoginCheck = stmt_Check.executeQuery(sql);
if(RsLoginCheck.next())
{
out.print("成功");
// response.sendRedirect("index.jsp");
}
else
{
out.print("用户名或密码错误!!!");
}
%>
<br />
</body>
</html>
</body>
</html>
<%
//*********** 数据库关闭代码 *******************************
RsLoginCheck.close(); //关闭记录集对象
stmt_Check.close(); //关闭语句对象
conn.close(); //关闭连接对象
%>
3、数据插入Access,既注册newuser.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!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=gb2312">
<title>注册</title>
</head>
<body>
<form name="form1" method="get" action="act.jsp">
<DIV align="center">
<P>用户名 <input type ="text" name="userName"size="15" height="15"></P>
<P>密 码 <input type ="text" name="Password1"size="15" height="15"></P>
<P>确认密码<input type ="text" name="Password2"size="15" height="15"></P>
<p>性别:
<select name="sex">
<option value="0">男</option>
<option value="1">女</option>
</select> </p>
<P><input onClick="return check();" type="submit" name="submit" value="确定注册" height="15"></P>
</DIV>
</form>
</body>
</html>
<SCRIPT language=javascript type="text/javascript">
<!--
function checkspace(checkstr) {
var str = '';
for(i = 0; i < checkstr.length; i++) {
str = str + ' ';
}
return (str == checkstr);
}
function check()
{
if(checkspace(document.form1.userName.value)) {
document.form1.userName.focus();
alert("管理员用户名不能为空!");
return false;
}
if(checkspace(document.form1.Password1.value)) {
document.form1.Password1.focus();
alert("密码不能为空!");
return false;
}
if(checkspace(document.form1.Password1.value!=document.form1.Password2.value)){
document.form1.Password1.focus();
alert("两次密码不一样!");
return false;
}
document.form1.submit();
}
//-->
</SCRIPT>
4、处理注册的程序act.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@include file="ConnDB.jsp"%>
<%
Statement st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("select id from admin");
rs.last();
int no;
if(rs.getRow()!=0){
no=rs.getInt(1);
no++;
}else{no=1;}
rs.close();
request.setCharacterEncoding("gb2312");
String uN=request.getParameter("userName");
String Pwd=request.getParameter("Password1");
String sex=request.getParameter("sex");
String id=""+no;
out.print(id);
String sql="insert into admin values("+id+",'"+uN+"','"+Pwd+"','"+sex+"')";
st.executeUpdate(sql);
st.close();
conn.close();
out.print("成功");
//response.sendRedirect("index.jsp");
%>
5、链接数据库程序ConnDB.jsp
<%
String dbname = ""; //Acess 数据库用户名,没有则为空
String user = ""; //Acess 数据库密码,没有则为空
//数据库连接字符串
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=F://Tomcat 5.5//webapps//test//Database//#data.mdb";
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立连接
Connection conn= DriverManager.getConnection(url);
%>