?? select.java
字號(hào):
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.awt.*;
class Colselectlist
{
String name=null;
Colselectlist next=null;
Colselectlist(String name)
{
this.name=name;
}
}
public class Select
{
String tablename;
Column colhead=new Column("head","none"),c1=colhead,c2;
Colselectlist shead=new Colselectlist("head"),s1=shead,s2;
word list=null;
int[] compare=new int[10];
String temp,temp_v; //詞語的內(nèi)容
int wordtype=-1,z=0; //詞語的類型
public void selectline(word wordlist,TextArea Check,database db)
{
System.out.println("*************選擇行****************");
list=wordlist;
list=list.next;
int colnum=0;
int colnum_select=0;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if( list.type!=1 )
{
Check.append("列名"+list.name+"錯(cuò)誤\n");
return;
}
while( !list.name.equals("from") )
{
if(list.type!=1)
{
Check.append("列名錯(cuò)誤\n");
return;
}
temp=list.name;
s2=new Colselectlist(temp);s1.next=s2;s1=s2;s2=s2.next;
colnum_select++; //列的數(shù)目
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if(list.name.equals("from"))
{
break;
}
if(!list.name.equals(","))
{
Check.append("缺少,\n");
return;
}
list=list.next;
}
/*已經(jīng)通過from驗(yàn)證*/
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if(list.type!=1)
{
Check.append("表名"+list.name+"錯(cuò)誤\n");
return;
}
tablename=list.name; //獲得表名
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if(!list.name.equals("where"))
{
Check.append("缺少where\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if(list.type!=1)
{
Check.append("輸入的列錯(cuò)誤\n");
return;
}
while( (list.name!=";") )
{
if(list.type!=1)
{
Check.append("列名錯(cuò)誤\n");
return;
}
temp=list.name;
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
System.out.println(temp+" "+list.name);
if(list.name.equals("<"))
{
compare[z]=-1;z++;
}
else if(list.name.equals("="))
{
compare[z]=0;z++;
}
else if(list.name.equals(">"))
{
compare[z]=1;z++;
}
else
{
Check.append("缺少=\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\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++;
//判斷列的數(shù)目
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
if(list.name.equals(";"))
{
break;
}
if(list.name.equals("and"))
{
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令沒有完結(jié)\n");
return;
}
/**************************************/
continue;
}
Check.append("命令"+list.name+"錯(cuò)誤\n");
}
/*已經(jīng)讀取到;*/
list=list.next;
/**************************************/
if( list!=null )
{
//命令;結(jié)束后還有字符,說明錯(cuò)誤
Check.append("符號(hào)';'后面還有字符\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("沒有創(chuàng)建數(shù)據(jù)庫\n");
return;
}
Document document=db.document;
Node root=document.getDocumentElement();//root是根元素
NodeList nodes=root.getChildNodes();
System.out.println("----------root: "+root.getNodeName());;
Node selecttable=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))
{
selecttable=nodes.item(i);
break;
}
}
if(i==nodes.getLength())
{
Check.append("沒有"+tablename+"表\n");
return;
}
/**********找到了要的表,然后是讀出列的個(gè)數(shù)***********/
int colnum_check=Integer.parseInt(selecttable.getAttributes().item(0).getNodeValue());
if(colnum_check<colnum || colnum_check<colnum_select)
{
Check.append("你的選擇的列的數(shù)目有問題\n");
return;
}
Node temp_element=selecttable.getFirstChild();
NodeList nodes_title=temp_element.getChildNodes();
c1=colhead.next;
boolean judge=false; //現(xiàn)在做的是判斷條件的列名在表中是否存在
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; //存在這個(gè)列名就正確
break;
}
}
if(judge==false) //如果列名不存在
{
Check.append("條件中列名"+c1.name+"在表中不存在\n");
return;
}
c1=c1.next;
}
s1=shead.next;
judge=false; //現(xiàn)在做的是判斷選取的列在表中是否存在
while(s1!=null)
{
judge=false;
for(int j=0;j<nodes_title.getLength();j++)
{
if(nodes_title.item(j).getNodeName().equals(s1.name))
{
judge=true; //存在這個(gè)列名就正確
break;
}
}
if(judge==false) //如果列名不存在
{
Check.append("你選擇的列"+s1.name+"在表中不存在\n");
return;
}
s1=s1.next;
}
NodeList allline=selecttable.getChildNodes();
/******************************
*<table>
* <titles>
* <colname1></colname>...
* <titles>
*<tr>
* <col1>...</col1><col2>...</col2> ...
*</tr>
*<tr>...</tr>
*</table>
*******************************/
i=1;
String cname=null; //每一行的列名
judge=false;
Check.append("********************** 選擇的結(jié)果 ************************\n");
s1=shead.next;
Check.append("----------------------------------------------------------\n");
while(s1!=null)
{
Check.append(s1.name+" ");
s1=s1.next;
}
Check.append("\n----------------------------------------------------------\n");
for(;i<allline.getLength();i++) //找合適的行
{
NodeList colvalues=allline.item(i).getChildNodes(); //獲得的1個(gè)tr的每個(gè)列
c1=colhead.next;
int selectline=-1;
while(c1!=null)
{
judge=false;
for(int j=0;j<colvalues.getLength();j++)
{
/*用select每一個(gè)條件在一行的所有列與列的值核對(duì)一邊
*即列名相等 而且 列值也相等
**/
if(colvalues.item(j).getNodeName().equals(c1.name))
{
if(compare[j]==-1)
{
if(colvalues.item(j).getFirstChild().getNodeValue().compareTo(c1.value)<0)
{
judge=true;
break;
}
}
else if(compare[j]==0)
{
if(colvalues.item(j).getFirstChild().getNodeValue().compareTo(c1.value)==0)
{
judge=true;
break;
}
}
else if(compare[j]==1)
{
if(colvalues.item(j).getFirstChild().getNodeValue().compareTo(c1.value)>0)
{
judge=true;
break;
}
}
}
}
if(judge==false)
{
//這一行不成立,換一行
break;
}
c1=c1.next;
}
//allline is tr list colvalues is td list
if(c1==null)//意味著這行通過檢查,要打印出來
{
s1=shead.next;
while(s1!=null)
{
for(int j=0;j<colvalues.getLength();j++)
{
if(colvalues.item(j).getNodeName().equals(s1.name) )
{
Check.append(colvalues.item(j).getFirstChild().getNodeValue()+" ");
}
}
s1=s1.next;
}
Check.append("\n");
}
}
Check.append("************************************\n");
Show sh=new Show();
sh.show(root);
System.out.println("*************************************\n");
}catch(Exception ee){System.out.println(ee);}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -