📄 insert.java
字号:
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import java.awt.*;
class colvalue
{
String value=null;
colvalue next=null;
colvalue(String name)
{
value=name;
}
}
public class Insert
{
String tablename;
colvalue valuehead=new colvalue("head"),v1=valuehead,v2;
word list=null;
String temp; //词语的内容
int wordtype=-1; //词语的类型
public void inserttable(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.name.equals("into") )
{
Check.append("缺少into\n");
return;
}
/*已经通过into验证*/
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("("))
{
Check.append("缺少(\n");
return;
}
list=list.next;
/**************************************/
if( list==null )
{
Check.append("命令没有完结\n");
return;
}
/**************************************/
if(list.type!=2)
{
Check.append("插入值错误\n");
return;
}
while( (list.name!=")") )
{
if(list.type!=2)
{
Check.append("插入值错误\n");
return;
}
temp=list.name;
v2=new colvalue(temp);v1.next=v2;v1=v2;v2=v2.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);
v1=valuehead;
/*******************************************/
while(v1!=null)
{
System.out.println("colvalue: "+v1.value);
v1=v1.next;
}
/********************************************/
}
else
{
//命令;结束后还有字符,说明错误
Check.append(";后面还有字符或者)后面没有;\n");
return;
}
/*#######################insert############################*/
try
{
if(db==null)
{
Check.append("没有创建数据库\n");
return;
}
Document document=db.document;
Node root=document.getDocumentElement();//root是根元素
NodeList nodes=root.getChildNodes();
Node inserttable=null;
int i=0;
for(i=0;i<nodes.getLength();i++)
{
System.out.println(nodes.item(i).getNodeName()+"="+nodes.item(i).getAttributes().item(1).getNodeValue());
System.out.println((nodes.item(i).getAttributes().item(1).getNodeValue()).equals(tablename));
if((nodes.item(i).getAttributes().item(1).getNodeValue()).equals(tablename))
{
inserttable=nodes.item(i);
break;
}
}
if(i==nodes.getLength())
{
Check.append("没有"+tablename+"表\n");
return;
}
/**********找到了要的表,然后是读出列的个数***********/
int colnum_check=Integer.parseInt(inserttable.getAttributes().item(0).getNodeValue());
if(colnum_check!=colnum)
{
Check.append("你的值和列的数目不对应\n");
return;
}
Node temp_element=inserttable.getFirstChild();
NodeList nodes_title=temp_element.getChildNodes();
Element tablecol=null,tr=(Element)document.createElement("tr"); //插入这个表的各个列
v1=valuehead.next;
/******************************
*<table>
* <titles>
* <colname1></colname>...
* <titles>
*<tr>
* <col1>...</col1><col2>...</col2> ...
*</tr>
*<tr>...</tr>
*</table>
*******************************/
inserttable.appendChild(tr); //先插入一行
i=0;
String cname=null; //每一行的列名
while(v1!=null)
{
cname=nodes_title.item(i).getNodeName();
tablecol=(Element)document.createElement(cname); //创建一行中的一列
tr.appendChild(tablecol); //在一行中插入一个元素
tablecol.appendChild(document.createTextNode(v1.value));
//System.out.println("####### v1="+v1.value+" colname"+cname);
i++;
v1=v1.next;
}
Show sh=new Show();
sh.show(root);
//show(root);
System.out.println("*************************************\n");
}catch(Exception ee){System.out.println(ee);}
}
void show(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++)
{
show(nodes.item(i));
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -