资源描述
《Web程序设计》
课程设计报告
课程设计题目:职工信息管理系统
专 业 信息管理与信息系统
职工信息管理系统
课程设计目的:
课程设计为学生提供了一个既动手又动脑,独立实践的机会,将课本上的理论知识和实际有机的结合起来,锻炼学生的分析解决实际问题的能力。提高学生适应实际,实践编程的能力。
我使用JSP和Java Bean来构建一个职工信息管理系统。介绍的例子可以分成两大部分,第一部分是普通程序,用于登录系统,第二部分是实现职工信息的增,删,查。
企业信息管理系统由以下几个部分构成:
第一部分home.jsp:登入界面
index.jsp:检测登录代码和密码是否一致,根据由JavaBean返回的结果显示不同的信息。dbBean:会员的合法性检验所用的Bean;
(1)Left.jsp
(2)Right.jsp
(3)Top.jsp
(4)Left.htm
(5)Logout.jsp
(6)Login.htm
第二部分
职工信息的增删查功能
1) dealdelete.jsp
2) dealinsert.jsp
3) dealupdate.jsp
4) delete.jsp
5) insert.htm
6) process.jsp
功能结构图
登入
添加
删除
查询
具体功能
本例的数据库采用Access(.MDB数据库),对数据库的访问采用便于理解的JDBC-ODBC方式,在使用本例前先在本地数据库建立一个ODBC数据源:user。
设置步骤如下:
(1)在开始->设置->控制面版(Win98、NT4.0)中选取“数据源(ODBC)”;在Win 2000 Professional和Server中分别位于“开始->设置->控制面版->管理工具”和“开始->程序->管理工具”下。
(2) 启动“数据源(ODBC)”配置程序,界面如图15-1所示。
图 ODBC数据源管理界面
(3)在图15-1中“系统DSN”选项下单击“添加”按钮,来添加一个系统的数据源(DSN),则出现如图15-2所示数据源驱动程序选择界面:
(3)
图 数据源驱动程序选择界面
(4)在图15-2中选择“Microsoft Access Driver (*.mdb)”单击“完成”加载Access数据库的驱动,则出现如图15-3所示数据库ODBC安装界面:
数据库
登入界面
登入成功首页
增加功能
增加成功
删除功能
删除成功
查询功能
登入核心代码
1) Left.htm
<html>
<body background="stone.gif">
<center><h2>查询用户</h2><hr>
<form method="post" action="right.jsp" target="right">
<table border width="100%" borderColorDark="#ffffec" borderColorLight="#5e5e00">
<tr bgcolor="#f7f7f7"><td width="50%">查询类型:</td>
<td><select name="stype">
<option value="id">职工名</option>
<option value="name">职工名</option>
<option value="sex">性别</option>
<option value="age">年龄</option>
<option value="phone">薪水</option>
</select></td>
<tr bgcolor="#f7f7f7"><td width="50%">查询内容:</td>
<td><input type="text" name="skey" size="12"></td>
<tr bgcolor="#f7f7f7">
<td colspan="2" align="center">
<input type="submit" value="查询">
<input type="reset" value="清空">
</td></table></form></center></body></html>
2) Right.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="check.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%!int size = 8;int p = 1;int totalPage = 1;String str = "";
public String printPage(ResultSet rs, int p, int size,int logId)
{ str = "";
try {for(int k=0;k<(p-1)*size;k++) rs.next(); }
catch(SQLException e) { }
for(int iPage=1; iPage<=size; iPage++) {
str += printRow(rs,iPage,p,logId);
try {
if(!rs.next()) break;
}
catch(Exception e) { }
}
return str;
}
public String printRow( ResultSet rs ,int i,int p,int logId)
{
String temp = "";
try {
if(i%2==1)
temp+="<tr bgcolor='#e7e7e7'>";
else
temp+="<tr bgcolor='#f7f7f7'>";
String id=rs.getString("id");
if(logId<10001)
temp+="<td><a href='update.jsp?updateid="+id+"&p="+p+"' target='left'>"+id+"</a></td>";
else
temp+="<td>"+id+"</td>";
temp+="<td>"+rs.getString("name")+"</td>";
temp+="<td>"+rs.getString("sex")+"</td>";
temp+="<td>"+rs.getString("age")+"</td>";
temp+="<td>"+rs.getString("phone")+"</td>";
temp += "</tr>";
}
catch(SQLException e) { }
return temp;
}
%>
<%
request.setCharacterEncoding("gb2312");
String type="";
String key="";
if(request.getParameter("stype")!=null)
{
type=request.getParameter("stype");
session.setAttribute("stype",type);
}
if(request.getParameter("skey")!=null)
{
key=request.getParameter("skey");
session.setAttribute("skey",key);
}
ResultSet rs=null;
String sql="select * from user";
if(session.getAttribute("stype")!=null)
type=(String)session.getAttribute("stype");
if(session.getAttribute("skey")!=null)
key=(String)session.getAttribute("skey");
if(type!=null && key!=null && !type.equals("") && !key.equals(""))
{
if(type.equals("id")||type.equals("age"))
sql += " where "+type+"="+key;
else
sql += " where "+type+"='"+key+"'";
}
sql+=" order by id asc";
rs=conn.executeQuery(sql);
%>
<center>
<table border="1" borderColorDark="#ffffec" borderColorLight="#5e5e00" width="100%">
<tr bgcolor="#cccccc" align="center">
<th>职工号</th><th>职工名</th><th>性别</th><th>年龄</th><th>薪水</th>
<%
ResultSet rsTmp=null;
String sql2="select count(*) from user";
if(type!=null && key!=null && !type.equals("") && !key.equals(""))
{
if(type.equals("id")||type.equals("age"))
sql2 += " where "+type+"="+key;
else
sql2 += " where "+type+"='"+key+"'";
}
rsTmp = conn.executeQuery(sql2);
int totalrecord=0;
if(rsTmp.next())
totalrecord = rsTmp.getInt(1);
if(totalrecord % size ==0) totalPage = totalrecord / size;
else totalPage = (int) Math.floor( totalrecord / size ) + 1;
if(totalPage == 0) totalPage = 1;
rsTmp.close();
try {
if(request.getParameter("p")==null|| request.getParameter("p").equals(""))
{
if(session.getAttribute("rightp")==null)
p = 1;
else
p = ((Integer)session.getAttribute("rightp")).intValue();
}
else
{
p = Integer.parseInt(request.getParameter("p")); session.setAttribute("rightp",new Integer(p));
}
} catch(NumberFormatException e) {
p = 1;
}
if(p < 1) p = 1;
if(p > totalPage) p = totalPage;
if(rs.next())
{
int logId=Integer.parseInt((String)session.getAttribute("loginId"));
out.println(printPage(rs,p,size,logId));
}
%>
</table>
<form Action="right.jsp" Method="GET">
<body background="stone.gif">
<%
for(int i=1;i<=totalPage;i++) {
out.println("<a href=right.jsp?p=" + i +">" + i + "</a> ");
}
%>
<p>输入页数:<input type="text" name="p" size="3">
页数:<font color="red"><%=p%>/<%=totalPage%></font>
</p></body ></form></center>
<% rs.close();conn.close();%>
3) Top.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ include file="check.jsp"%>
<html>
<body background="stone.gif">
<font color="yellow" size="6"> 欢迎进入职工信息管理系统</font>
<br/><br/><br/>
<div align="centre">
<a href="left.htm" target="left">查询职工信息</a>
<%
String id=(String)session.getAttribute("loginId");
int i=Integer.parseInt(id);
if(i<10001)
{
out.println("<a href='insert.htm' target='left'>添加职工信息</a>");
out.println("<a href='delete.jsp' target='right'>删除职工信息</a>");
}
else
out.println("<a href='update.jsp?updateid="+id+"' target='left'>添加职工信息</a>");
%>
<a href="logout.jsp" target="_top">退出</a>
</div>
</body>
</html>
4) Logout.jsp
<%
session.invalidate();
response.sendRedirect("login.htm");
%>
5) Login.htm
<html>
<body background="stone.gif">
<center>
<h2>职工信息系统登录</h2>
<form method="post" action="process.jsp">
<table border bordercolor="#FF0066" bgcolor='#FFCCFF'>
<tr><td width="40%">用户名:</td>
<td><input type="text" name="username"></td>
<tr><td width="40%">密码:</td>
<td><input type="password" name="password"></td>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录">
<input type="reset" value="清空">
</td></table></form></center></body></html>
增删查核心代码
1) dealdelete.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="checkadmin.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%
request.setCharacterEncoding("gb2312");
String[] delid=request.getParameterValues("isdel");
if(delid!=null)
{
for(int i=0;i<delid.length;i++)
conn.executeUpdate("delete from user where id="+delid[i]);
}
conn.close();
response.sendRedirect("delete.jsp");
%>
2) dealinsert.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="checkadmin.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%
request.setCharacterEncoding("gb2312");
String name="";
String password="";
String sex="";
String age="0";
String phone="";
int id=10000;
if(request.getParameter("username")!=null)
name=request.getParameter("username");
if(request.getParameter("password")!=null)
password=request.getParameter("password");
if(request.getParameter("sex")!=null)
sex=request.getParameter("sex");
if(request.getParameter("age")!=null && !(request.getParameter("age").equals("")))
age=request.getParameter("age");
if(request.getParameter("phone")!=null)
phone=request.getParameter("phone");
ResultSet rs=null;
rs = conn.executeQuery("select max(id) from user");
if(rs.next())
id=rs.getInt(1);
id++;
rs.close();
String sql="insert into user values("+id+",'"+name+"','"+password+"','"+sex+"',"+age+",'"+phone+"')";
conn.executeUpdate(sql);
conn.close();
response.sendRedirect("right.jsp");
%>
3) dealupdate.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%
request.setCharacterEncoding("gb2312");
String id="0";
String name="";
String password="";
String sex="";
String age="0";
String phone="";
if(request.getParameter("updateid")!=null)
id=request.getParameter("updateid");
if(request.getParameter("username")!=null)
name=request.getParameter("username");
if(request.getParameter("password")!=null)
password=request.getParameter("password");
if(request.getParameter("sex")!=null)
sex=request.getParameter("sex");
if(request.getParameter("age")!=null && !(request.getParameter("age").equals("")))
age=request.getParameter("age");
if(request.getParameter("phone")!=null)
phone=request.getParameter("phone");
String sql="update user set name='"+name+"',password='"+password+"',sex='"+sex+"',age="+age+",phone='"+phone+"', where id="+id;
conn.executeUpdate(sql);
conn.close();
response.sendRedirect("right.jsp");
%>
4) delete.jsp
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="checkadmin.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%!
//每页显示的记录个数
int size = 8;
//当前页号
int p = 1;
//全部的页数
int totalPage = 1;
String str = "";
//显示页号为p的一页
public String printPage(ResultSet rs, int p, int size)
{
str = "";
//将访问游标定位到页号为p的页要显示的第一条记录的位置
try {
for(int k=0;k<(p-1)*size;k++)
rs.next();
}
catch(SQLException e) { }
for(int iPage=1; iPage<=size; iPage++) {
str += printRow(rs,iPage);
try {
if(!rs.next()) break;
}
catch(Exception e) { }
}
return str;
}
//显示单行记录
public String printRow( ResultSet rs ,int i)
{
String temp = "";
try {
if(i%2==1)
temp+="<tr bgcolor='#e7e7e7'>";
else
temp+="<tr bgcolor='#f7f7f7'>";
String id=rs.getString("id");
temp+="<td><a href='update.jsp?updateid="+id+"' target='left'>"+id+"</a></td>";
temp+="<td>"+rs.getString("name")+"</td>";
temp+="<td>"+rs.getString("sex")+"</td>";
temp+="<td>"+rs.getString("age")+"</td>";
temp+="<td align='center'><input type='checkbox' name='isdel' value='"+id+"'></td>";
temp += "</TR>";
}
catch(SQLException e) { }
return temp;
}
%>
<%
ResultSet rs=null;
rs = conn.executeQuery("select * from user order by id");
%>
<center>
<form method="post" action="dealdelete.jsp">
<table border="1" borderColorDark="#ffffec" borderColorLight="#5e5e00" width="100%">
<tr bgcolor="#cccccc" align="center">
<th>职工号</th><th>职工名</th><th>性别</th><th>年龄</th><th>是否删除</th>
<%
ResultSet rsTmp=null;
rsTmp = conn.executeQuery("select count(*) as mycount from user");
int totalrecord=0;
if(rsTmp.next())
totalrecord = rsTmp.getInt("mycount");
// 如果记录数是页数的整数倍
if(totalrecord % size ==0) totalPage = totalrecord / size;
// 如果最后还空余一页
else totalPage = (int) Math.floor( totalrecord / size ) + 1;
if(totalPage == 0) totalPage = 1;
rsTmp.close();
try {
if(request.getParameter("p")==null|| request.getParameter("p").equals(""))
{
if(session.getAttribute("deletep")==null)
p = 1;
else
p = ((Integer)session.getAttribute("deletep")).intValue();
}
else
{
p = Integer.parseInt(request.getParameter("p")); session.setAttribute("deletep",new Integer(p));
}
}
// 捕获用户从浏览器地址拦直接输入非数字信息而引出的异常
catch(NumberFormatException e) { p = 1;}
if(p < 1) p = 1;
if(p > totalPage) p = totalPage;
if(rs.next())
out.println(printPage(rs,p,size));
%>
</table>
<p>
<input type="submit" value="删除">
<input type="reset" value="重置">
</form>
<form Action="delete.jsp" Method="GET">
<%
for(int i=1;i<=totalPage;i++) {
out.println("<a href=delete.jsp?p=" + i + ">" + i + "</a> ");
}
%>
<p>输入页数:<input type="text" name="p" size="3">
页数:<font color="red"><%=p%>/<%=totalPage%></font>
</p>
</form>
</center>
<%
rs.close();
conn.close();
%>
5) insert.htm
<%@ page contentType="text/html;charset=gb2312" import="java.sql.*"%>
<%@ include file="checkadmin.jsp"%>
<jsp:useBean id="conn" class="dbBean.DBBean" scope="session"/>
<%!
//每页显示的记录个数
int size = 8;
//当前页号
int p = 1;
//全部的页数
int totalPage = 1;
String str = "";
//显示页号为p的一页
public String printPage(ResultSet rs, int p, int size)
{
str = "";
//将访问游标定位到页号为p的页要显示的第一条记录的位置
try {
for(int k=0;k<(p-1)*size;k++)
rs.next();
}
catch(SQLException e) { }
for(int iPage=1; iPage<=size; iPage++) {
str += printRow(rs,iPage);
try {
if(!rs.next()) break;
}
catch(Exception e) { }
}
return str;
}
//显示单行记录
public String printRow( ResultSet rs ,int i)
{
String temp = "";
try {
if(i%2==1)
temp+="<tr bgcolor='#e7e7e7'>";
else
temp+="<tr bgcolor='#f7f7f7'>";
String id=rs.getString("id");
temp+="<td><a href='update.jsp?updateid="+id+"' target='left'>"+id+"</a></td>";
temp+="<td>"+rs.getString("name")+"</td>";
temp+="<td>"+rs.getString("sex")+"</td>";
temp+="<td>"+rs.getString("age")+"</td>";
temp+="<td align='center'><input type='checkbox' name='isdel' value='"+id+"'></td>";
temp += "</TR>";
}
catch(SQLException e) { }
return temp;
}
%>
<%
ResultSet rs=null;
rs = conn.executeQuery("select * from user order by id");
%>
<center>
<form method="post" action="dealdelete.jsp">
<table border="1" borderColorDark="#ffffec" borderColorLight="#5e5e00" width="100%">
<tr bgcolor="#cccccc" align="center">
<th>职工号</th><th>职工名</th><th>性别</th><th>年龄</th><th>是否删除</th>
<%
ResultSet rsTmp=null;
rsTmp = conn.executeQuery("select count(*) as mycount from user");
int totalrecord=0;
if(rsTmp.next())
totalrecord = rsTmp.getInt("mycount");
// 如果记录数是页数的整数倍
if(totalrecord % size ==0) totalPage = totalrecord / size;
// 如果最后还空余一页
else totalPage = (int) Math.floor( totalrecord / size ) + 1;
if(totalPage =
展开阅读全文