?? ecminfupd.java
字號:
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.lang.*;
public class ecmInfUpd extends HttpServlet{
int UpdCount=0,Ucount;
String dbName,tableName,Lab,Ustr="";
String[] Uarray=new String[20];
ResultSet rs;
tableSql t;
String temp,insert0,delete0,cancel0,update0;
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
ServletOutputStream out=resp.getOutputStream();
temp="插入";
insert0=new String(temp.getBytes("GBK"),"ISO8859_1");
temp="刪除";
delete0=new String(temp.getBytes("GBK"),"ISO8859_1");
temp="取消";
cancel0=new String(temp.getBytes("GBK"),"ISO8859_1");
temp="更改";
update0=new String(temp.getBytes("GBK"),"ISO8859_1");
dbName=req.getParameter("DbName");
Lab=req.getParameter("UpdLab");
tableName=req.getParameter("TableName");
if (req.getParameter("DbName").equals("Ecmdb")){
t = new tableSql("Ecmdb");
System.out.println("open Ecmdb");
}
resp.setContentType("text/html");
out.println("<HTML>");
out.println("<HEAD><TITLE>Update Goods</TITLE></HEAD>");
out.println("<BODY bgcolor=\"#f5f5f5\">");
out.println("<FORM METHOD=POST ACTION=\"/servlet/ecmInfUpd\">");
out.println("<INPUT TYPE=\"hidden\" name=\"DbName\" value=\""+dbName+"\">");
out.println("<INPUT TYPE=\"hidden\" name=\"TableName\" value=\""+tableName+"\">");
out.println("<INPUT TYPE=\"hidden\" name=\"UpdLab\" value=\""+Lab+"\">");
ModifyRecords(req,out);
if (Ucount>1){
UpdCount=t.recordModify(Ustr,Ucount,Uarray);
System.out.println("i enter to modify records");
}
out.println("<HR>");
//out.println("<FONT LANG=\"ZH-CN\" SIZE=4 COLOR=\"#0000ff\">");
String temp000,temp001;
temp000="請選擇要更改的記錄(每次只能更改一條)";
temp001=new String(temp000.getBytes("GBK"),"ISO8859_1");
out.println("<B>"+temp001+"</B><BR>");
if (dbName.equals("Sysdb"))
DispTable("select * from "+tableName,out);
else
DispTable("select * from "+tableName,out);
out.println("<BR>");
out.println("<HR>");
out.println("<INPUT TYPE=SUBMIT VALUE="+update0+">");
out.println("<INPUT TYPE=RESET VALUE="+cancel0+">");
out.println("</FORM>");
//out.println("<BR><HR>");
if (UpdCount>0)
out.println("You have succeed to modifyed "+UpdCount+" records!");
else out.println("You have fail to modifyed!");
out.println("</BODY></HTML>");
}
///構造更新語句
public void ModifyRecords(HttpServletRequest req,ServletOutputStream out)
throws ServletException,IOException{
int j=0;
int k=0;///i用于控制選擇更改的記錄不能多于一個
String kkey,values;
String[] tmp=new String[20];
Enumeration keys;
keys=req.getParameterNames();
System.out.println("i ente the while");
while (keys.hasMoreElements()) {
kkey=(String)keys.nextElement();
values=req.getParameter(kkey);////get parameter's value
///get Uarray
if (kkey.startsWith("field")){
System.out.println("i enter to get Uarray");
//Uarray[j]=values;
//out.println("UpdArray["+j+"]:"+Uarray[j]+"<BR>");
tmp[j]=values;
//out.println("tmp["+j+"]:"+tmp[j]+"<BR>");
j++;
System.out.println(j);
}
//tmp[0]=System.currentTimeMillis();
System.out.println(j);
///get Ucount, Ucount用于設置update語句參數的個數
Ucount=j;
////將數組tmp反置獲得Uarray Uarray用于設置update語句中?參數值
for(int i=0;i<=(Ucount-1);i++){
Uarray[i]=tmp[j-i-1];
}
///get Ustr
if (kkey.startsWith("record")){
++k;
///k>2同時更改兩個或更多記錄則出錯處理
if (k>=2){
out.println("<BR><BR><B>Error:</B>");
out.println("<BR><BR>You can only select one to modify!<BR>");
}
///k<2執行操作
else{
System.out.println("i enter to get Ustr");
Ustr="Update "+tableName+" set "+Lab+" where "+tableName+".num="+values;
//out.println("Update "+tableName+" set "+Lab+" where "+tableName+".num="+values);
try{
int i;
rs=t.recordSql("select * from "+tableName+" where "+tableName+".num="+values);
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
//out.println("<FONT LANG=\"ZH-CN\" SIZE=4 COLOR=\"#0000ff\">");
//out.println("<B>You will modify the num "+values+" record</B><BR>");
String temp000,temp001;
temp000="請輸入你要更改的信息";
temp001=new String(temp000.getBytes("GBK"),"ISO8859_1");
out.println(temp001+"<BR>");
//out.println("</FONT>");
//為記錄每個字段設置一個文本框
while (rs.next()){
for(i=2;i<=numCols-2;i++){
out.println(rsmd.getColumnLabel(i)+": "+"<INPUT TYPE=TEXT NAME=\""+"field"+i+"\""+"value=\""+rs.getString(i)+"\"><BR>");
}
String temp;
temp=new Long(System.currentTimeMillis()).toString();
out.println("<INPUT TYPE=hidden NAME=\""+"field"+i+"\""+"value=\""+temp+"\"><BR>");
}
}//end try
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
}///end else
}////end if
}////end while
}
public void Destroy(){
t.closeConnect();
}
////display the Parameter and values
public void WatchParameters(HttpServletRequest req,ServletOutputStream out)
throws ServletException,IOException{
int i=1;
Enumeration keys;
String kkey,values;
keys=req.getParameterNames();
while (keys.hasMoreElements()) {
kkey=(String)keys.nextElement();
values=req.getParameter(kkey);
out.println("<P>"+"key: "+kkey+" equals value:"+values);
}
}
public void DispTable(String query,ServletOutputStream out) throws ServletException,IOException{
//out.println("<H1>TABLE RESULTS:</H1>");
out.println("<TABLE BORDER=\"1\" ALIGN=\"CENTER\">");
try{
synchronized(this) {
int i=0,j=0;
String tmpstr,checkbo;
rs=t.recordSql(query);
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
//display each column title
out.print("<TD>column name");
for(i=1;i<=numCols;i++){
//out.println(new String(name.getBytes("GBK"),"ISO8859_1") );
out.print("<TD>"+new String(rsmd.getColumnLabel(i).getBytes("GBK"),"ISO8859_1") );
System.out.println("display the title");
System.out.println(new String(rsmd.getColumnLabel(i).getBytes("GBK"),"ISO8859_1") );
}
out.println("<TR>");
System.out.println("enter display the table records");
//display each column data
while (rs.next()){
//tmpstr=rs.getString(2);
//out.print("<TD><INPUT TYPE=checkbox NAME=\"record"+i+"\">");
j++;
checkbo=rs.getString(1);
out.print("<TD><INPUT TYPE=\"checkbox\" NAME=\"record"+j+"\""+" value=\""+checkbo+"\">");
out.print("<TD>"+checkbo);
for(i=2;i<=numCols;i++){
tmpstr=rs.getString(i);
if(rs.wasNull())
out.print("<TD>NULL");
else
out.print("<TD>"+new String(tmpstr.getBytes("GBK"),"ISO8859_1") );
}
out.println("<TR>");
}
}
out.println("</TABLE>");
}
catch(SQLException ex){
System.out.println("\n***SQLException caught ***\n");
while (ex != null){
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message:"+ex.getMessage());
System.out.println("Vendor:"+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
out.println("</TABLE>");
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -