}
//获得指定商品信息
public static String[] getRowStock(String id)
{
String[] s=new String[6]; 自定义了各种对数据操作的静态方法,包括
对各个数据库的增删改查,其中对于 user 表中的删除仅能由权限 2 以上的管理人员执行,
代码如下(手工编写)
package paim;
import .*;
import .*;
import .*;
public class DBUtil_SM
{
//获取用户信息
public static List<String[]> getVIPList()
{
ArrayList<String[]> vl=new ArrayList<String[]>();
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select id,name,age,sex,workuntil,tel,lvl,total from VIP_IM";
ResultSet rs=(sql);
while(())
{
String[] s=new String[8];
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
(s);
}
();
();
();
}catch(Exception e)
{
();
}
return vl;
}
//获取库存信息
public static List<String[]> getStockList()
{
ArrayList<String[]> sl=new ArrayList<String[]>();
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select id,name,price,count,vprice,type from stock order by type";
ResultSet rs=(sql);
while(())
{
String[] s=new String[6];
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
(s);
}
();
();
();
}catch(Exception e)
{
();
}
return sl;
}
//添加用户信息
public static void toInsertID(String[] s)
{
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="insert into VIP_IM
values('"+s[0]+"','"+s[1]+"','"+s[2]+"','"+s[3]+"','"+s[4]+"','"+s[5]+"','"+s[6]+"','"+s[7]+"')";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//删除用户信息
public static void toDelID(String id)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
("delete from VIP_IM where id='"+id+"'");
("delete from user where id='"+id+"'");
();
();
}catch(Exception e)
{
();
}
}
//更新用户数据
public static void toUpdateID(String[] s)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="update VIP_IM set
name='"+s[1]+"',age="+s[2]+",sex='"+s[3]+"',workuntil='"+s[4]+"',tel='"+s[5]+"',lvl='"+s[6]+"',total="+s[7]+"
where id='"+s[0]+"'";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//提取一个用户信息
public static String[] getRowID(String id)
{
String[] s=new String[8];
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
ResultSet rs=("select id,name,age,sex,workuntil,tel,lvl,total from VIP_IM where
id='"+id+"'");
if(())
{
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
}
();
();
();
}catch(Exception e)
{
();
}
return s;
}
//删除库存信息
public static void toDelStock(String id)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
("delete from stock where id='"+id+"'");
();
();
}catch(Exception e)
{
();
}
}
//添加库存信息
public static void toInsertStock(String[] s)
{
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="insert into stock
values('"+s[0]+"','"+s[1]+"','"+s[2]+"','"+s[3]+"','"+s[4]+"','"+s[5]+"')";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//更新物品信息
public static void toUpdateStock(String[] s)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="update stock set
name='"+s[1]+"',price="+s[2]+",count="+s[3]+",vprice="+s[4]+",type='"+s[5]+"' where id='"+s[0]+"'";
(sql);
();
();
}catch(Exception e)
{
();
}
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
ResultSet rs=("select id,name,price,count,vprice,type from stock where
id='"+id+"'");
if(())
{
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
}
();
();
();
}catch(Exception e)
{
();
}
return s;
}
//用户登录
public static int getUser(String[] k)
{
String[] s=new String[4];
int i=0;
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
ResultSet rs=("select id,name,lit,psw from user where id='"+k[0]+"'");
if(())
{
for(int j=0;j<;j++)
{
s[j]=(j+1);
}
i=5;
}else
{
return i;
}
if(k[1].equals(s[3]))
{
if(s[2].equals("1"))
{
i=1;
}else
{
i=2;
}
}else
{
i=3;
}
();
();
();
}catch(Exception e)
{
();
}
return i;
}
//修改密码
public static void toUpdatePsw(String[] s)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="update user set psw='"+s[1]+"' where id='"+s[0]+"'";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//获取积分列表
public static List<String[]> getTotalList()
{
ArrayList<String[]> sl=new ArrayList<String[]>();
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select id,name,total from total order by total";
ResultSet rs=(sql);
while(())
{
String[] s=new String[3];
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
(s);
}
();
();
();
}catch(Exception e)
{
();
}
return sl;
}
// 获 取 商 品 积 分
public static int getTotalS(String id)
{
int t=0;
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select total from total where id='"+id+"'";
ResultSet rs=(sql);
if(())
{
t=(1);
}
();
();
();
}catch(Exception e)
{
();
}
return t;
}
//获取用户积分
public static int getTotalV(String id)
{
int t=0;
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select total from VIP_IM where id='"+id+"'";
ResultSet rs=(sql);
if(())
{
t=(1);
}
();
();
();
}catch(Exception e)
{
();
}
return t;
}
// 获 取 积 分 商 品 名 称
public static String getTotalName(String id)
{
String t="";
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select name from total where id='"+id+"'";
ResultSet rs=(sql);
if(())
{
t=(1);
}
();
();
();
}catch(Exception e)
{
();
}
return t;
}
//积分兑换表
public static void addList(String[] s)
{
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="insert into totallist(id,name,ltotal,tname,ttotal)
values('"+s[0]+"','"+s[1]+"','"+s[2]+"','"+s[3]+"','"+s[4]+"')";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//用户消费记录
public static List<String[]> getIDTotal(String id)
{
ArrayList<String[]> sl=new ArrayList<String[]>();
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select id,name,ltotal,tname,ttotal from totallist where id='"+id+"'";
ResultSet rs=(sql);
while(())
{
String[] s=new String[5];
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
(s);
}
();
();
();
}catch(Exception e)
{
();
}
return sl;
}
//用户账户插入
public static void toInsertUser(String[] s)
{
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="insert into user values('"+s[0]+"','"+s[1]+"','"+s[2]+"','"+s[3]+"')";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//积分物品添加
public static void addTotal(String[] s)
{
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="insert into total values('"+s[0]+"','"+s[1]+"','"+s[2]+"')";
(sql);
();
();
}catch(Exception e)
{
();
}
}
// 更 新 积 分 物 品 信 息
public static void toUpdateTotal(String[] s)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="update total set name='"+s[1]+"',total='"+s[2]+"' where id='"+s[0]+"'";
(sql);
();
();
}catch(Exception e)
{
();
}
}
//删除积分物品信息
public static void toDelTotal(String id)
{
try
{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
("delete from total where id='"+id+"'");
();
();
}catch(Exception e)
{
();
}
}
//获取积分列表
public static String[] getTotalListS(String id)
{
String[] s=new String[3];
try{
("");
Connection con=("jdbc:odbc:dbsm","","");
Statement st=();
String sql="select id,name,total from total where id='"+id+"'";
ResultSet rs=(sql);
while(())
{
for(int i=0;i<;i++)
{
s[i]=(i+1);
}
}
();
();
();
}catch(Exception e)
{
();
}
return s;
}
}
用于响应 jsp 请求根据情况调用 DBUtil_SM 中的方法,返回结果,代
码如下(手工编写)
package paim;
import .*;
import .*;
import .*;
public class SMServlet extends HttpServlet
{
@Override
public void doGet(HttpServletRequest request,HttpServletResponse response) throws
IOException,ServletException
{
doPost(request,response);
}
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws
IOException,ServletException
{
String action=("action");
if(("del_id"))//删除用户
{
String id=("id");
(id);
("/").include(request,response);
}
if(("add_id"))//添加用户
{
String[] s=new String[9];
if(("id")!=null) {
s[0]= new String(("id").getBytes("ISO8859_1"), "GB2312");}
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("age")!=null) {
s[2] = new String(("age").getBytes("ISO8859_1"), "GB2312");}
if(("sex")!=null) {
s[3] = new String(("sex").getBytes("ISO8859_1"), "GB2312");}
if(("workuntil")!=null) {
s[4] = new String(("workuntil").getBytes("ISO8859_1"), "GB2312");}
if(("tel")!=null) {
s[5]= new String(("tel").getBytes("ISO8859_1"), "GB2312");}
if(("lvl")!=null) {
s[6]= new String(("lvl").getBytes("ISO8859_1"), "GB2312");}
if(("total")!=null) {
s[7]= new String(("total").getBytes("ISO8859_1"), "GB2312");}
if(("psw")!=null) {
s[8]= new String(("psw").getBytes("ISO8859_1"), "GB2312");}
String[] g=new String[2];
g[0]=s[0];
g[1]=s[8];
if((g)!=0)//返回 0 即不存在用户
{
("/ include(request,response);
}else
{
String[] user=new String[4];
user[0]=s[0];
user[1]=s[1];
user[2]="1";
user[3]=s[8];
(s);
(user);
("/"). include(request,response);
}
}
if(("update_id"))//修改用户信息
{
String lit=("lim");
String[] s=new String[8];
s[0]=("id");
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("age")!=null) {
s[2] = new String(("age").getBytes("ISO8859_1"), "GB2312");}
if(("sex")!=null) {
s[3] = new String(("sex").getBytes("ISO8859_1"), "GB2312");}
if(("workuntil")!=null) {
s[4] = new String(("workuntil").getBytes("ISO8859_1"), "GB2312");}
if(("tel")!=null) {
s[5]= new String(("tel").getBytes("ISO8859_1"), "GB2312");}
if(("lvl")!=null) {
s[6]= new String(("lvl").getBytes("ISO8859_1"), "GB2312");}
if(("total")!=null) {
s[7]= new String(("total").getBytes("ISO8859_1"), "GB2312");}
(s);
if(("use"))//根据用户权限显示页面
{
("/ include(request,response);
}else
{
("/"). include(request,response);
}
}
if(("del_stock"))//删除物品
{
String id=("id");
(id);
("/").include(request,response);
}
if(("add_stock"))//添加物品信息
{
String[] s=new String[6];
if(("id")!=null) {
s[0] = new String(("id").getBytes("ISO8859_1"), "GB2312");}
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("price")!=null) {
s[2] = new String(("price").getBytes("ISO8859_1"), "GB2312");}
if(("count")!=null) {
s[3]= new String(("count").getBytes("ISO8859_1"), "GB2312");}
if(("vprice")!=null) {
s[4] = new String(("vprice").getBytes("ISO8859_1"), "GB2312");}
if(("type")!=null) {
s[5]= new String(("type").getBytes("ISO8859_1"), "GB2312");}
(s);
("/"). include(request,response);
}
if(("update_stock"))//修改物品信息
{
String[] s=new String[6];
if(("id")!=null) {
s[0] = new String(("id").getBytes("ISO8859_1"), "GB2312");}
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("price")!=null) {
s[2] = new String(("price").getBytes("ISO8859_1"), "GB2312");}
if(("count")!=null) {
s[3]= new String(("count").getBytes("ISO8859_1"), "GB2312");}
if(("vprice")!=null) {
s[4] = new String(("vprice").getBytes("ISO8859_1"), "GB2312");}
if(("type")!=null)
{s[5]= new String(("type").getBytes("ISO8859_1"), "GB2312");}
(s);
("/"). include(request,response);
}
if(("login"))//登陆验证
{
String[] k=new String[2];
k[0]=("id");
k[1]=("psw");
int i=(k);
if(i==1)//判断是否为用户
{
("/
forward(request,response);
}
if(i==0||i==5||i==3)//用户名不存在 密码错误
{
("/ include(request,response);
}
if(i==2)//管理员跳转
{
("/"). include(request,response);
}
}
if(("password"))//密码修改
{
String[] k=new String[2];
k[0]=("id");
k[1]=("opsw");
String[] s=new String[2];
s[0]=k[0];
s[1]=("npsw1");
int i=(k);
if(i==1)
{ (s);
("/
forward(request,response);
}
if(i==0||i==5||i==3)
{
("/ include(request,response);
}
if(i==2)
{
(s);
("/"). include(request,response);
}
}
if(("total"))//兑换积分 积分余额 积分消费判断操作
{
String[] id=new String[2];
id[0]=("id");
id[1]=("tid");
int[] t=new int[5];
t[0]=(id[0]);
t[1]=(id[1]);
t[3]=t[0]-t[1];
String tname=(id[1]);
String[] sl=(id[0]);
String[] ll=new String[6];
if(t[3]<0)
{
("/ forward(request,response);
}
else
{
sl[7]=(t[3]);
ll[0]=sl[0];
ll[1]=sl[1];
ll[2]=(t[3]);
ll[3]=(id[1]);
ll[4]=(t[1]);
(ll);
(sl);
("/ forward(request,response);
}
}
if(("addtotal"))//添加积分物品
{
String[] s=new String[3];
if(("id")!=null) {
s[0] = new String(("id").getBytes("ISO8859_1"), "GB2312");}
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("total")!=null) {
s[2] = new String(("total").getBytes("ISO8859_1"), "GB2312");}
(s);
("/"). include(request,response);
}
if(("del_total"))//删除积分物品
{
String id=("id");
(id);
("/"). include(request,response);
}
if(("totalupdate"))//积分物品修改
{
String[] s=new String[3];
if(("id")!=null) {
s[0] = new String(("id").getBytes("ISO8859_1"), "GB2312");}
if(("name")!=null) {
s[1] = new String(("name").getBytes("ISO8859_1"), "GB2312");}
if(("total")!=null) {
s[2] = new String(("total").getBytes("ISO8859_1"), "GB2312");}
(s);
("/"). include(request,response);
}
}
}
为配置文件代码如下(自动生成后添加参数)
<?xml version="" encoding="ISO-8859-1"?>
<web-app xmlns="
xmlns:xsi="
xsi:schemaLocation="
version="">
<display-name>SuperMarket</display-name>
<description>
SuperMarket
</description>
<servlet>
<servlet-name>SMServlet</servlet-name>
<servlet-class></servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SMServlet</servlet-name>
<url-pattern>/SMServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
</welcome-file>
</welcome-file-list>
</web-app>
文件为管理员管理模块的主界面,代码如下(HTML 格式为自动生成,jsp 代
码为手工编写)
<%@ page
contentType="text/html;charset=gbk"
import="_SM,.*"
%>
<html>
<head>
<link rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<p align="center"><strong>用户信息管理</strong>
<%
("GBK");
("GBK");
List<String[]> vl=();
List<String[]> sl=();
List<String[]> tl=();
%>
<table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0
width=100%>
<tr bgcolor=cccccc align=center>
<td>用户名</td><td>年龄</td><td>性别</td><td>工作单位</td><td>联系方式</td><td>会员等级
</td><td>积分</td><td colspan="2">管理</td>
</tr>
<%
for(String[] s:vl)
{
if(s[4]==null||s[4].equals(""))
{
s[4]="暂无信息";
}
if(s[5]==null||s[5].equals(""))
{
s[5]="暂无信息";
}
%>
<tr align="center">
<td><a href=" %></a></td><td><%=s[2] %></td><td><%=s[3]
%></td><td><%=s[4] %></td><td><%=s[5] %></td><td><%=s[6] %></td><td><%=s[7] %></td>
<td><a href=" 修 改 </a></td><td><a
href="SMServlet?action=del_id&&id=<%=s[0]%>">删除</a></td>
</tr>
<%
}
%>
</table>
<p align="right"><a href="">添加用户</a>
<p align="center"><strong>库存信息管理</strong>
<table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0
width=100%>
<tr bgcolor=cccccc align=center>
<td> 商 品 编 号 </td><td> 名 称 </td><td> 价 格 </td><td> 会 员 价 </td><td> 类 型 </td><td> 库 存 </td><td
colspan="2">管理</td>
</tr>
<%
for(String[] s:sl)
{
%>
<tr align=center>
<td><%=s[0] %></td><td><%=s[1] %></td><td><%=s[2] %></td><td><%=s[4] %></td><td><%=s[5]
%></td><td><%= s[3] %></td>
<td><a href=" 修 改 </a></td><td><a
href="SMServlet?action=del_stock&&id=<%=s[0]%>">删除</a></td>
</tr>
<%
}
%>
</table>
<p align="right"><a href="">添加物品</a>
<p align="center"><strong>积分物品信息管理</strong>
<table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0
width=100%>
<tr bgcolor=cccccc align=center>
<td>编号</td><td>物品</td><td>所需积分</td><td colspan="2">管理</td>
</tr>
<%
for(String[] s:tl)
{
%>
<tr align="center">
<td><%=s[0] %></td><td><%=s[1] %></td><td><%=s[2] %></td>
<td><a href="
<td><a href="SMServlet?action=del_total&&id=<%=s[0]%>">删除</a></td>
</tr>
<%
}
%>
</table>
<p align="right"><a href="">添加物品</a>
</body>
</html>
<%@ page
contentType="text/html;charset=gbk"
import="_SM,.*"
%>
<html>
<head>
<title>用户信息修改</title>
<link rel="stylesheet" type="text/css">
<script language="javascript">
function checkCount(psw,pst,id,name,age)
{
var Strw=;
var Strt=;
if(Strw==""||Strt=="")
{
alert("密码不能为空!!!");
return false;
}
if(Strw==Strt)
{
return true;
}else
{
alert("密码不一致!!!");
return false;
}
}
</script>
</head>
<body>
<p><strong>用户信息</strong>
<%
("GBK");
("GBK");
String mark1="";
String mark2="";
String id=("id");
String lit=("lit");
String[] s=(id);
if(s[3].equals("男"))
{
mark1="checked";
}
else
{
mark2="checked";
}
String s4=s[4];
if(s[4]==null||s[4].equals(""))
{
s4="暂无信息";
}
String s5=s[5];
if(s[5]==null||s[5].equals(""))
{
s5="暂无信息";
}
String str=new String("");
str=("str");
%>
文件为修改用户属性页面,HTML 为自动生成填写参数,JSP 以及 JavaScript
部分为手工编写
<form action="SMServlet" method="post" >
<table table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0
width=100%>
<input type="hidden" name="action" value="update_id">
<input type="hidden" name="id" value="<%=s[0]%>">
<tr><td align="center">姓 名:<INPUT type="text" name="name" value="<%=s[1]%>"></td></tr>
<tr><td align="center">年 龄:<input type="text" name="age" value="<%=s[2]%>"></td></tr>
<tr><td align="center">联系方式:<input type="text" name="tel" value="<%=s5%>"></td></tr>
<tr><td align="center"> 性 别 :<input type="radio" name="sex" value=" 男 " <%= mark1 %>> 男 <input
type="radio" name="sex" value="女" <%= mark2 %>>女</td></tr>
<tr><td align="center"> 工 作 单 位 :<TEXTAREA name="workuntil" cols=73 rows=2><%= s4
%></TEXTAREA></td></tr>
<%
if(lit==null||("")){
%>
<input type="hidden" name="lim" value="use">
<tr><td align="center"> 用 户 级 别 :<%= s[6] %><input type="hidden" name="lvl"
value="<%=s[6]%>"></td></tr>
<tr><td align="center"> 积 分 :<%= s[7] %><input type="hidden" name="total"
value="<%=s[7]%>"></td></tr>
<tr><td align="center"><INPUT type=submit value="保存并提交用户信息"></td></tr>
</table>
</form>
<b><p align="left">修改密码</p></b>
<form action="SMServlet" method="post" onsubmit="return
checkCount(,);">
<table table border=1 borderColorDark=#ffffec borderColorLight=#5e5e00 cellPadding=1 cellSpacing=0
width=100%>
<tr><td><input type="hidden" name="id" value="<%=id%>"></td></tr>
<tr><td><input type="hidden" name="action" value="password"></td></tr>
<tr><td align="center">原密码:<input type="password" name="opsw" value=""></td></tr>
<tr><td align="center">新密码:<input type="password" id="psw" name="npsw1" value=""></td></tr>
<tr><td align="center">再次确认:<input type="password" name="npsw2" id="pst" value=""></td></tr>
<tr><td align="center"><INPUT type=submit value="确认修改">
<% if(!(str==null||("")))
{
%>
<b>密码错误</b>
<% }
%> </td></tr>
</table>
</form>
<p align=right><a href="
<%
}else {
%>
<input type="hidden" name="lim" value="adm">
<tr><td align="center">用户级别<input type="text" name="lvl" value="<%=s[6]%>"></td></tr>
<tr><td align="center">积分:<input type="text" name="total" value="<%=s[7]%>"></td></tr>
<tr><td align="center"><INPUT type=submit value=" 保 存 并 提 交 用 户 信 息 "></td></tr>
</table>
</form>
<p align=right><a href="">返回上一级</a>
<%
} %>
</body>
</html>
1. 其他 jsp 文件为用户界面的搭建布局,多为自动生成,手工补足参数;