2. 数据库表结构
本实例中主要出现了三个数据库表,表名和字段分别如下所示:
计划采购表:jhcg_table
字段名称 中文名称 类型 长度
Goods_no 物品编号 vchar 10
Goods_name 物品名称 Vchar 50
Amount 采购数量 Int
Price 采购单价 float
Gold 币种 Vchar 15
Units 单位 Vchar 10
Date 时间 Date
Remark 备注 vchar 100
库存统计表:kctj_table
字段名称 中文名称 类型 长度
Goods_no 物品编号 Vchar 10
Goods_name 物品名称 Vchar 50
amount 库存数量 Int
Date 时间 Date
remark 备注 Vchar 100
实际采购表:sjcg_table
字段名称 中文名称 类型 长度
Goods_no 物品编号 Vchar 10
Goods_name 物品名称 Vchar 50
Amount 采购数量 Int
Price Price 采购单价 Float
Gold 币种 Vchar 15
Units 采购单位 Vchar 10
Date 时间 Date
Remark 备注 vchar 100
其中业务逻辑非常简单,即根据计划采购表和库存统计表生成实际采购表。同时,对各表完成数据库的增、删、改、查等通用操作。
3. JSP设计
① 插入操作
完成对数据库表的记录插入功能,其中计划采购表的插入主页面(insert_jhcg.htm)为:
图1 计划采购表插入主页面
insert_jhcg.htm将用户输入传送给demo_insert_jhcg.jsp,完成插入操作。改jsp文件的功能代码为:
<html>
<body>
<jsp:useBean id="DBConn" class="dbaccess.DBConnBean" scope="page"/>
<jsp:useBean id="DBBean" class="dbaccess.DBQueryBean" scope="page"/>
<hr>
<!--test JavaBean-->
<%
if (DBConn == null||DBBean == null){
out.println("JavaBean not found!");
return;
}
%>
<!--try db_demo connection-->
<%
try{
DBConn.Connect();
}catch(Exception e){
out.println(e.getMessage());
}
%>
<!--execute sql statement-->
<%
String insGoodno = request.getParameter("ed_jhcg_no");
String insGoodname = request.getParameter("ed_jhcg_name");
int insAmount = (Integer.valueOf(request.getParameter("ed_jhcg_amount"))).intValue();
float insPrice = (Float.valueOf(request.getParameter("ed_jhcg_price"))).floatValue();
String insGold = request.getParameter("ed_jhcg_gold");
String insUnit = request.getParameter("ed_jhcg_unit");
String insRemark = request.getParameter("ed_jhcg_remark");
String sqlStatement = "insert into jhcg_table(good_no,good_name,amount,
price,gold,unit,remark) values("+"´"+insGoodno+"´"+","+"´"+insGoodname+"´"+",
"+insAmount+","+insPrice+","+"´"+insGold+"´"+","+"´"+insUnit+"´"+","+"´"+
insRemark+"´"+")";
try{
DBBean.query(sqlStatement);
}catch(Exception e){
out.println(e.getMessage());
}
%>
<a href="demo_main.htm">Records inserted...Click here to return</a></p>
</body>
</html>
② 查询操作
该查询主页面主要提供对三个数据库表的条件查询功能,如下图所示:
图2 查询主页面
query.htm将用户选择查询的数据库表和查询条件发送给demo_query.jsp,由jsp文件完成数据库查询操作和查询结果集的返回及显示,其功能代码如下:
<html>
<body>
<%
String sqlStatement;
String sqlField = "";
String whichTable = "";
String whereClause = "";
String queryNo = "";
String queryName = "";
%>
<jsp:useBean id="DBConn" class="dbaccess.DBConnBean" scope="page"/>
<jsp:useBean id="DBBean" class="dbaccess.DBQueryBean" scope="page"/>
<hr>
<!--test JavaBean-->
<%
if (DBConn == null||DBBean == null){
out.println("JavaBean not found!");
return;
}
%>
<!--try db_demo connection-->
<%
try{
DBConn.Connect();
}catch(Exception e){
out.println(e.getMessage());
}
%>
<!--prepare sql statement-->
<%
String queryRequest = request.getParameter("rb_request");
//out.println("queryRequest:"+queryRequest);
String whichCB = "";
if (queryRequest.equals("1")){
whichCB = "ck_jhcg";
whichTable = "jhcg_table";
queryNo = request.getParameter("ed_jhcg_no");
queryName = request.getParameter("ed_jhcg_name");
if (!queryNo.equals(""))
whereClause = " where good_no="+"´"+queryNo+"´";
if (!queryName.equals("")){
if (!queryNo.equals(""))
whereClause += " and good_name="+"´"+queryName+"´";
else whereClause = " where good_name="+"´"+queryName+"´";
}
}
if (queryRequest.equals("2")){
whichCB = "ck_kctj";
whichTable = "kctj_table";
queryNo = request.getParameter("ed_kctj_no");
queryName = request.getParameter("ed_kctj_name");
if (!queryNo.equals(""))
whereClause = " where good_no="+"´"+queryNo+"´";
if (!queryName.equals("")){
if (!queryNo.equals(""))
whereClause += " and good_name="+"´"+queryName+"´";
else whereClause = " where good_name="+"´"+queryName+"´";
}
}
if (queryRequest.equals("3")){
whichCB = "ck_sjcg";
whichTable = "sjcg_table";
queryNo = request.getParameter("ed_sjcg_no");
queryName = request.getParameter("ed_sjcg_name");
if (!queryNo.equals(""))
whereClause = " where good_no="+"´"+queryNo+"´";
if (!queryName.equals("")){
if (!queryNo.equals(""))
whereClause += " and good_name="+"´"+queryName+"´";
else whereClause = " where good_name="+"´"+queryName+"´";
}
}
String[] printTitle = request.getParameterValues(whichCB);
%>
<!--create query sql statement-->
<%
sqlStatement = "select ";
for(int i = 0;i<printTitle.length;i++){
sqlField += printTitle[i]+",";
}
sqlStatement += sqlField.substring(0,sqlField.length()-1)+" from "+whichTable;
if (!whereClause.equals(""))
sqlStatement += whereClause;
%>
<!--show query response-->
<%
try{
DBBean.query(sqlStatement);
}catch(Exception e){
out.println("Database Error!");
}
int rows = DBBean.getRowcount();
int cols = DBBean.getColumncount();
%>
<Table align="center" width="80%" border=1>
<tr align=center>
<%
for(int i = 0;i < printTitle.length;i++){
out.println("<td><b>");
out.println(printTitle[i]);
out.println("</b></td>");
}
%>
</tr>
<%
for (int i = 0;i < rows;i++){
out.println("<tr>");
for (int j = 0;j < cols;j++)
out.println("<td>"+DBBean.get(i,j)+"</td>");
out.println("</tr>");
}
%>
</Table>
<br>
<hr>
<a href="demo_main.htm">Click here to return</a></p>
</body>
</html>
③ 生成实际采购表
生成数据库表是一个隐式操作,程序根据计划采购表和库存统计表的相应字段生成实际采购表,不需要用户的任何输入,其功能代码如下(demo_create.jsp):
<%@page import="Java.util.*"%>
<html>
<body>
<jsp:useBean id="DBConn" class="dbaccess.DBConnBean" scope="page"/>
<jsp:useBean id="DBBean" class="dbaccess.DBQueryBean" scope="page"/>
<hr>
<!--test JavaBean-->
<%
if (DBConn == null||DBBean == null){
out.println("JavaBean not found!");
return;
}
%>
<!--try db_demo connection-->
<%
try{
DBConn.Connect();
}catch(Exception e){
out.println(e.getMessage());
}
%>
<!--prepare sql statement-->
<%
int amount_jhcg,amount_kctj;
Vector updateRs = new Vector();
DBBean.query("delete * from sjcg_table"); //delete all old records in sjcg_table
DBBean.query("select jhcg_table.good_no,jhcg_table.good_name,jhcg_table.amount,kctj_table.amount,jhcg_table.unit from jhcg_table left join kctj_table on kctj_table.good_no=jhcg_table.good_no");
int rows = DBBean.getRowcount();
int cols = DBBean.getColumncount();
for (int i = 0;i < rows;i++){
String record[] = new String[4];
record[0] = DBBean.get(i,0);
record[1] = DBBean.get(i,1);
amount_jhcg = (Integer.valueOf(DBBean.get(i,2))).intValue();
if (DBBean.get(i,3) == null) amount_kctj = 0;
else amount_kctj = (Integer.valueOf(DBBean.get(i,3))).intValue();
record[2] = Integer.toString(amount_jhcg - amount_kctj);
record[3] = DBBean.get(i,4);
updateRs.addElement(record);
}
for (int i = 0;i < rows;i++){
String insRecord[] = (String [])updateRs.elementAt(i);
String insGoodno,insGoodname,insUnit,insAmount;
insGoodno = insRecord[0];
insGoodname = insRecord[1];
insAmount = insRecord[2];
insUnit = insRecord[3];
String sqlStatement = "insert into sjcg_table(good_no,good_name,amount,unit) values?quot;+"´"+insGoodno+"´"+","+"´"+insGoodname+"´"+","+insAmount+","+"´"+insUnit+"´"+")";
DBBean.query(sqlStatement);
DBBean.query("delete * from sjcg_table where amount<=0");
}
%>
<a href="demo_main.htm">Database created...Click here to return</a></p>
</body>
</html>
上述的开发工具综合应用介绍了基于Java开发电子商务应用系统的全过程,包括应用开发平台搭建、业务流程分析、JavaBean封装和JSP开发等内容,其中JSP开发中涉及到了通用SQL(查询和插入数据库表)和游标操作(生成实际采购表),基本可以完成任何网络数据库应用的需求。本实例基本上可以将前面介绍的基于Java的电子商务开发技术串接起来,指导读者进行电子商务应用开发。
(未完待续)
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/