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

📄 update.java

📁 基于xml的数据库管理系统
💻 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 + -