|
//文件datagird.java
package datagird;
import java.io.IOException;
import java.io.PrintWriter;
import javaDB.IDB;
import javax.Servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class datagird extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/HTML; charset=GBK");
PrintWriter out = response.getWriter();
/*数据库处理接口
* 主要有两个函数
* 1.selectList 返回ArrayList数组
* 数组第一行为列标题 第二行开始为数据
* 2.selectSet 返回ResultSet数据集
*/
javaDB.IDB idb=new javaDB.jdbcoracle();
/*
* 当使用此表格时,需要在此修改代码
*/
int pageN=1;//当前页-自动计算
int Num=idb.getcount("select count(*) from CZHT_DEPARTMENT");
int pageNum=0;//总页数-自动计算
int pageLeng=10;//页大小
if(Num%pageLeng==0)
pageNum=(int)Math.floor(Num/pageLeng);
else
pageNum=(int)Math.floor(Num/pageLeng)+1;
/*表单处理 用于处理表格事件,添加 删除 更新 等
* 每次页面显示时都经由此处验证,假如是要对页面处理,则页面经处理后
* 才会对数组集填充,把最新的结果显示出来。
*/
int action;
try{
action=Integer.parseInt(request.getParameter("act"));//动作类型
}
catch (Exception e)
{
e.printStackTrace();
action=-1;
}
if (action>0)
{
switch(action)
{
case 1:
//添加函数
String addsql="INSERT INTO CZHT_DEPARTMENT VALUES('"+
request.getParameter("DEPCODE")+"','"+
idb.toChinese(request.getParameter("DEPNAME"))+"','"+
idb.toChinese(request.getParameter("DEPALLNAME"))+"','"+
request.getParameter("_DEPCOD")+"','"+
request.getParameter("HTQDFLAG")+"','"+
request.getParameter("AREA")+"','"+
request.getParameter("POPULATION")+"')";
//out.println("添加");
//out.println(addsql);
idb.update(addsql);
break;
case 2:
//删除函数,根据指定字段DEPCODE 进行删除
String delsql="DELETE FROM CZHT_DEPARTMENT where DEPCODE='"+
request.getParameter("DEPCODE")+"' ";
idb.update(delsql);
out.println("删除"+delsql);
break;
case 3:
//更新函数,根据指定字段DEPCODE 进行更新
String upsql="update CZHT_DEPARTMENT set "+
"DEPNAME='"+idb.toChinese(request.getParameter("DEPNAME"))+//中文转换
"',DEPALLNAME='"+idb.toChinese(request.getParameter("DEPALLNAME"))+//中文转换
"',P_DEPCOD='"+request.getParameter("P_DEPCOD")+
"',HTQDFLAG='"+request.getParameter("HTQDFLAG")+
"',AREA='"+request.getParameter("AREA")+
"',POPULATION='"+request.getParameter("POPULATION")+
"' where DEPCODE='"+request.getParameter("DEPCODE")+"' ";
//out.println("更新");
//out.println(upsql);
idb.update(upsql);
break;
case 4:
//换页函数
pageN=Integer.parseInt(request.getParameter("pageN"));//为哀求页
if(pageN<=0)
pageN=1;
if(pageN>pageNum)
pageN=pageNum;
break;
} //end switch
}//end if ------------ 表单处理结束------------------
/*---------------数据源填充---------------------
*通过数据库读取函数返回java.util.ArrayList类型,
*将java.util.ArrayList 类型变量做为参数传递给
*表格输出过程
*
*当使用此表格时,需要在此修改代码
*/
String sqlselectlist="select DEPCODE,DEPNAME,DEPALLNAME,P_DEPCOD,HTQDFLAG,AREA,POPULATION from "+
"(select rownum as rn,CZHT_DEPARTMENT.* from CZHT_DEPARTMENT) where rn<"+(pageN*pageLeng+1)+" and rn>"+(pageN-1)*pageLeng;
java.util.ArrayList data[]=idb.selectList(sqlselectlist);
int RowsLeng=data[0].size();//列数
int pageSize=data.length;//页大小
//标题数组,可以指定中文的显示标题,
String title[]=new String[RowsLeng];
for(int i=0;i<title.length;i++)
{
title=data[0].get(i).toString();
}
title[0]="编号";
title[1]="名称";
title[2]="全称";
title[3]="上级编号";
title[4]="操纵代码";
title[5]="面积";
title[6]="人口";
//编纂参数数组true为可编纂false为不可编纂。默认全部可以编纂,
boolean EDIT[]=new boolean[RowsLeng];
for(int i=0;i<EDIT.length;i++)
{
EDIT=true;
}
EDIT[0]=false;//将第一列设为不可编纂
//---------------------页面输出--------------------
//css
out.println("<link href='../css/javatable.css' rel='stylesheet' type='text/css' />");
//javascript
out.println("<script language='javascript' src='../js/javatable.js'></script>");
out.println("<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#CCCCCC'>");
//打出表头
out.println("<tr id='TRTITLE' class='TITLE'>");
for(int j=0;j<RowsLeng;j++)
{
out.println(" <td>"+title[j]+"</td>");
}
out.println(" <td>操纵</td>");
out.println("</tr>");
//--------表格主体---------
for(int i=1;i<pageSize;i++)
{
//表格显示部门********************
out.println("<form action='' method='post'>");
out.println(" <input name='act' type='hidden' value=''/>");
out.println(" <input name='"+data[0].get(0)+"' type='hidden' value='"+data.get(0)+"'/>");
out.println("<tr id='TRIDSHOW_"+i+"' class='tr1'>");
for(int j=0;j<RowsLeng;j++)
{
out.println(" <td>"+data.get(j)+"</td>");
}
out.println(" <td width='100'><img src='../images/edit.gif' title='编纂'>" +
" <label onclick=\"javascript:if ( confirm ('确定删除?') )"+
"{form.act.value='2';setdisabled();form.submit();}\"><img src='../images/del.gif' title='删除'></label></td>");
out.println("</tr>");
out.println("</form>");
//表格编纂部门********************
out.println("<form action='' method='post'>");
out.println(" <input name='act' type='hidden' value=''/>");
out.println("<tr id='TRIDHIDE_"+i+"' class='Selected' style='display:none'>");
for(int j=0;j<RowsLeng;j++)
{
if(EDIT[j])
out.println(" <td><input class='test' type='text' name='"+data[0].get(j)+"' value='"+data.get(j)+"' /></td>");
else
out.println(" <td><input class='test' type='text' name='"+data[0].get(j)+"' value='"+data.get(j)+"' disabled=\"true\" /></td>");
}
out.println(" <td width='100'><label class='butText' onclick=\"javascript:form.act.value='3';setdisabled();form.submit();\">更新</label> <label class='butText'>取消</label></td>");
out.println("</tr>");
out.println("</form>");
}
//表格添加部门***********************
out.println("<form action='' method='post'>");
out.println(" <input name='act' type='hidden' value=''/>");
out.println("<tr id='TRIDADD' class='Selected' style='display:none'>");
for(int j=0;j<RowsLeng;j++)
{
out.println(" <td><input class='test' type='text' name='"+data[0].get(j)+"' /></td>");
}
out.println(" <td width='100'><label class='butText' onclick=\"javascript:form.act.value='1';setdisabled();form.submit();\">确定</label> <label class='butText'>取消</label></td>");
out.println("</tr>");
out.println("</form>");
out.println("</table>");
out.println("<div style='width:100%' align='center'><table width='100%' class='Footer'><tr><td align='center'>"+
"<form action='' method='post' style='margin:0'><label onclick=\"javascript:form.pageN.value--;form.submit();\">上一页</label> <label onclick=\"javascript:form.pageN.value++;form.submit();\">下一页</label> 共"+pageNum+" 页 当前"+pageN+" 页 "+
" <label> <input type='text' name='pageGO' style='width:40px' /> </label> <label> <input type='button' name='Submit' value='转到' style='height:20px' onclick=\"javascript:form.pageN.value=form.pageGO.value;form.submit();\"/>"+
" </label><input name='pageN' type='hidden' value='"+pageN+"'/><input name='act' type='hidden' value='4'/></form> </td><td width='100px' ><img src='../images/add.gif' title='添加'></td></tr></table></div>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
}
//文件javatable.js
// JavaScript Document
//进入编纂状态
function StartEdit(tr_id)
{
document.getElementById("TRIDHIDE_"+tr_id).style.display="";
document.getElementById("TRIDSHOW_"+tr_id).style.display="none";
}
//退出编纂状态
function StopEdit(tr_id)
{
document.getElementById("TRIDHIDE_"+tr_id).style.display="none";
document.getElementById("TRIDSHOW_"+tr_id).style.display="";
}
//添加一行
function Add()
{
document.getElementById("TRIDADD").style.display="";
}
//取消添加
function CancelAdd()
{
document.getElementById("TRIDADD").style.display="none";
}
//设置disabled属性,在数据提交前使所有文本框有效
function setdisabled()
{
var ES=document.getElementsByTagName("input");
for(var j=0;j<ES.length;j++)
{
if(ES[j].type=="text")
ES[j].disabled="";
}
} |
|