⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 select.java

📁 基于xml的数据库管理系统
💻 JAVA
字号:
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;                //词语的内容
	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("命令没有完结\n");
	 		return;
		 }
	 	 /**************************************/
		 
	 	 
		 if( list.type!=1 )
		 {
		 	Check.append("列名"+list.name+"错误\n");
		 	return;
		 }
		 
		 while( !list.name.equals("from") )
		 {	
		 	if(list.type!=1)
		    {
    		 	Check.append("列名错误\n");
    		 	return;
	    	}
	    	
		 	temp=list.name;
		 	s2=new Colselectlist(temp);s1.next=s2;s1=s2;s2=s2.next;
		 	colnum_select++;                                        //列的数目
		 	list=list.next;
		 	
		 	/**************************************/
		 	if( list==null )
		 	{
		 		Check.append("命令没有完结\n");
		 		return;
		 	}
		 	/**************************************/
		 	
		 	if(list.name.equals("from"))
		 	{
		 		break;
		 	}
		 	
		 	if(!list.name.equals(","))
		    {
		     	Check.append("缺少,\n");
		     	return;
	     	}
		   	
	     	list=list.next;
		 }
		 
		 /*已经通过from验证*/
		 
		 list=list.next;
		 
		 /**************************************/
		 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("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("<"))
			{
				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("命令没有完结\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();
    	    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;
    	    }
    	    
    	    /**********找到了要的表,然后是读出列的个数***********/
    	    int colnum_check=Integer.parseInt(selecttable.getAttributes().item(0).getNodeValue());
    	    
    	    if(colnum_check<colnum || colnum_check<colnum_select)
    	    {
    	    	Check.append("你的选择的列的数目有问题\n");
    	    	return;
    	    }
    	      
    	    
    	    Node temp_element=selecttable.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;
    	    }
    	    
    	    s1=shead.next;
    	    judge=false; //现在做的是判断选取的列在表中是否存在
    	    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;  //存在这个列名就正确
    	    			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("********************** 选择的结果 ************************\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个tr的每个列
    	    	c1=colhead.next;
    	    	
    	    	int selectline=-1;
    	    	while(c1!=null)
    	    	{
    	    		judge=false;
    	    		for(int j=0;j<colvalues.getLength();j++)
    	    		{
    	    			/*用select每一个条件在一行的所有列与列的值核对一边
    	    			 *即列名相等 而且 列值也相等
    	    			 **/
    	    			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);}
		 
	}
	
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -