?? update.java
字號:
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.awt.*;
public class Update
{
String tablename;
Column colhead=new Column("head","none"),c1=colhead,c2;
word list=null;
String temp,temp_v; //詞語的內容
int wordtype=-1; //詞語的類型
String col_update,value_update;//要更新列名和值
public void updateline(word wordlist,TextArea Check,database db)
{
System.out.println("*************刪除行****************");
list=wordlist;
list=list.next;
int colnum=0;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if( list.type!=1 )
{
Check.append("表名"+list.name+"錯誤\n");
return;
}
/*已經通過表名驗證*/
tablename=list.name; //獲得表名
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(!list.name.equals("set"))
{
Check.append("缺少set\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(list.type!=1)
{
Check.append("要更改的列名錯誤\n");
return;
}
col_update=list.name;
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(!list.name.equals("="))
{
Check.append("缺少=\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(list.type!=2)
{
Check.append("更新的值錯誤\n");
return;
}
value_update=list.name;
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(!list.name.equals("where"))
{
Check.append("缺少where\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(list.type!=1)
{
Check.append("輸入的列錯誤\n");
return;
}
while( (list.name!=";") )
{
if(list.type!=1)
{
Check.append("列名錯誤\n");
return;
}
temp=list.name;
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
System.out.println(temp+" "+list.name);
if(!list.name.equals("="))
{
Check.append("缺少=\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(list.type!=2) //取列的值
{
Check.append("缺少值\n");
return;
}
temp_v=list.name;
c2=new Column(temp,temp_v);c1.next=c2;c1=c2;c2=c2.next;
colnum++;
//列的數目
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
if(list.name.equals(";"))
{
break;
}
if(list.name.equals("and"))
{
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結\n");
return;
}
/**************************************/
continue;
}
Check.append("命令"+list.name+"錯誤\n");
}
/*已經讀取到;*/
list=list.next;
/**************************************/
if( list!=null )
{
//命令;結束后還有字符,說明錯誤
Check.append("符號';'后面還有字符\n");
return;
}
c1=colhead.next;
while(c1!=null)
{
System.out.println("column: "+c1.name+" = "+c1.value);
c1=c1.next;
}
/*#######################update############################*/
try
{
if(db==null)
{
Check.append("沒有創建數據庫\n");
return;
}
Document document=db.document;
Node root=document.getDocumentElement();//root是根元素
NodeList nodes=root.getChildNodes();
Node updatetable=null;
int i=0;
for(i=0;i<nodes.getLength();i++)
{
System.out.println(nodes.item(i).getNodeName()+"="+nodes.item(i).getAttributes().item(0).getNodeValue());
if((nodes.item(i).getAttributes().item(1).getNodeValue()).equals(tablename))
{
updatetable=nodes.item(i);
break;
}
}
if(i==nodes.getLength())
{
Check.append("沒有"+tablename+"表\n");
return;
}
/**********找到了要的表,然后是讀出列的個數***********/
int colnum_check=Integer.parseInt(updatetable.getAttributes().item(0).getNodeValue());
if(colnum_check<colnum)
{
Check.append("你的條件選擇的列的數目有問題\n");
return;
}
Node temp_element=updatetable.getFirstChild();
NodeList nodes_title=temp_element.getChildNodes();
c1=colhead.next;
boolean judge=false; //現在做的是判斷條件的列名在表中是否存在
while(c1!=null)
{
judge=false;
for(int j=0;j<nodes_title.getLength();j++)
{
if(nodes_title.item(j).getNodeName().equals(c1.name))
{
judge=true; //存在這個列名就正確
break;
}
}
if(judge==false) //如果列名不存在
{
Check.append("條件中列名"+c1.name+"在表中不存在\n");
return;
}
c1=c1.next;
}
NodeList allline=updatetable.getChildNodes();
/******************************
*<table>
* <titles>
* <colname1></colname>...
* <titles>
*<tr>
* <col1>...</col1><col2>...</col2> ...
*</tr>
*<tr>...</tr>
*</table>
*******************************/
i=1;
String cname=null; //每一行的列名
Node updateline=null; //要更新的行
judge=false;
for(;i<allline.getLength();i++) //找合適的行
{
NodeList colvalues=allline.item(i).getChildNodes(); //獲得的1個tr的每個列
c1=colhead.next;
while(c1!=null)
{
judge=false;
for(int j=0;j<colvalues.getLength();j++)
{
/*用delete中每一個條件在一行的所有列與列的值核對一邊
*即列名相等 而且 列值也相等
**/
if(colvalues.item(j).getNodeName().equals(c1.name) && colvalues.item(j).getFirstChild().getNodeValue().equals(c1.value))
{
judge=true;
break;
}
}
if(judge==false)
{
//這一行不成立,換一行
break;
}
c1=c1.next;
}
if(c1==null)//意味著這行通過檢查
{
updateline=allline.item(i);
}
}
Check.append("************************************\n");
if(updateline!=null) //這里updateline是<tr></tr>
{
//找到這一行,準備更新列
NodeList cols_update=updateline.getChildNodes();
int k=0;
for(k=0;k<cols_update.getLength();k++)
{
if(cols_update.item(k).getNodeName().equals(col_update))
{
cols_update.item(k).removeChild(cols_update.item(k).getFirstChild());
cols_update.item(k).appendChild(document.createTextNode(value_update));
break;
}
}
if(k!=cols_update.getLength())
{
Check.append("已經更新了該行\n");
}
else
{
Check.append("更新的列不存在\n");
}
}
else
{
Check.append("沒有滿足條件的行\n");
}
Show sh=new Show();
sh.show(root);
System.out.println("*************************************\n");
}catch(Exception ee){System.out.println(ee);}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -