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

📄 createtable.java

📁 基于xml的数据库管理系统
💻 JAVA
字号:
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.awt.*;

class cols
{
	String colname=null;
	cols next=null;
	cols(String name)
	{
		colname=name;
	}
}
public class Createtable
{
	String tablename;
	cols colhead=new cols("head"),c1=colhead,c2;
	word list=null;
	
	String temp;                //词语的内容
	int wordtype=-1;            //词语的类型
	
	
	public void  createtable(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("("))
		 {
		 	Check.append("缺少(\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;
		 	c2=new cols(temp);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==";") || (list.next==null)  ) //若没有读到)直接读到了;或者命令结束则报错
		 	{
		 		Check.append("缺少)\n");
		 		return;
		 	}
		 	
		   	if(!list.name.equals(","))
		    {
		     	Check.append("缺少,\n");
		     	return;
	     	}
	     	list=list.next;
		 }
		 
		 /*已经读取到)*/
		 list=list.next;
		 
		 /**************************************/
		 if( list==null )
		 {
			Check.append("命令没有完结\n");
	 		return;
		 }
	 	 /**************************************/
		 
		 if( (list.name.equals(";")) && (list.next==null)  )
		 {
		 	//命令结束,可以建立表
		 	System.out.println(tablename);
		 	c1=colhead;
		 	
		 	/*******************************************/
		 	while(c1!=null)
		 	{
		 		System.out.println("colname: "+c1.colname);
		 		c1=c1.next;
		 	}
		 	/********************************************/
		 }
		 else
		 {
		 	//命令;结束后还有字符,说明错误
		 	Check.append(";后面还有字符或者)后面没有;\n");
		    return;
		 }
		 
		 /*#######################建立xml table############################*/
		 try
    	 { 
    	    if(db==null)
    	    {
    	    	Check.append("没有创建数据库\n");
	    		return;
    	    }
    	    Document document=db.document;
    	    Node root=document.getDocumentElement();//root是根元素
    	    
    	    NodeList nodes=root.getChildNodes();
	    	for(int i=0;i<nodes.getLength();i++)
	    	{
	    		if(nodes.item(i).getAttributes().item(0).getNodeValue().equals(tablename))
	    		{
	    			Check.append("你已经建立了相同的表,请不要重复建立\n");
	    			return;
	    		}
	    	}
    	    
    	    Element temp_element=(Element)document.createElement("table"),ele;
    	    temp_element.setAttribute("name",tablename);
    	    temp_element.setAttribute("colnum",String.valueOf(colnum));
    	    root.appendChild(temp_element);
    	    
    	    ele=(Element)document.createElement("titles"); //ele是列名的一张表,因为空表要保存列名
    	    temp_element.appendChild(ele);
    	    
    	    c1=colhead.next;  //准备把新建的所有列放到第一个(titles中作为列名的一张表)
    	    while(c1!=null)
    	    {
    	    	ele.appendChild(document.createElement(c1.colname));
    	    	c1=c1.next;
    	    }
    	    
    	    Show sh=new Show();
    	    sh.show(root);
    	    //show(root);
    	    
    	    Check.append("你已经成功建立了"+tablename+"表\n");
     	 }catch(Exception ee){System.out.println(ee);}
		 
	}
	
	void show1(Node eee)
	{ 
	    System.out.print(eee.getNodeName()+" :");
	    if(eee.hasAttributes())
	    {
	    	NamedNodeMap atts=eee.getAttributes();
	    	for(int j=0;j<atts.getLength();j++)
	    	{
	    		Node att=atts.item(j);
	    		System.out.print(" +-- "+att.getNodeName()+"/"+att.getNodeValue());
	    	}
	    }
	    System.out.println("  ");
	    if(eee.hasChildNodes())
	    {
	    	NodeList nodes=eee.getChildNodes();
	    	for(int i=0;i<nodes.getLength();i++)
	    	{
	    		show1(nodes.item(i));
	    	}
	    }
	}
	
}

⌨️ 快捷键说明

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