只是练习而已,所以程序的好坏,以及设计的怎样都 没有什么关系,只是用来练兵吧了。精神上支持你,我也正在考虑这个问题。下面这个类我还没有写完,送给你吧(如果你要用Text SQL)别忘记写完后,发一份给我,让我也看看。[email protected] java.util.*; import com.newman.io.FileReadWrite; import com.newman.lang.MyString; import com.newman.lang.StringToken;/** * <p>Title: This follow is newman's writing</p> * <p>Description: I want better writing ,instead of best one!</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: Shu</p> * @author Newman * @version 1.0 */ /** * 查询,写入函数没有实现 * 觉得没有必要写这个类,实现sql查询太复杂了。 *//** * This is the core class. * Function: * 1.It can read the file and input the content into the memory in the way of hashtable. * Hashtable contains some vectors , just the columns. * So the user can query the content in the certain column . * 2.It also can save the changes back to the text. * So the user can modify the content . */ public class TextSQL { String m_szFileName;//database filename Hashtable m_Table;//database :store the vector String m_szSeparator;//string separator Vector m_vColumn;//the column vector String m_ENTER="\r\n"; public TextSQL(String filename, String separator) { this.m_szFileName =filename; this.m_Table =new Hashtable(); this.m_szSeparator=separator; this.m_vColumn =new Vector(); } /** * Load into memory */ public void loadInData() { if(MyString.Assert(this.m_szFileName)&&MyString.Assert(this.m_szSeparator)){ FileReadWrite frw=new FileReadWrite(this.m_szFileName); Enumeration enum=frw.getContentEnumeration(); boolean bflag=true;//create is true while(enum.hasMoreElements()){ StringToken st=new StringToken((String)enum.nextElement(),this.m_szSeparator); ArrayList list=new ArrayList(); list=st.workString();//get the data in line if(list==null)//the file is null return ; int ColumnCount=list.size(); if(bflag){//at the first time for(int i=0;i<ColumnCount;i++ ){ Vector v=new Vector(); this.m_Table .put(list.get(i),v); this.m_vColumn .add(list.get(i)) ;//read the column } bflag=false; } else{ Enumeration enum2=this.m_Table.elements(); int i=0; while(enum2.hasMoreElements()){ Vector v2=(Vector)enum2.nextElement(); v2.add(list.get(i)); i++; } } } } else{ System.out.println("The database filename hasn't been defined!"); return; } } /** * set the separator * @param separator */ public void setSeparator(String separator){ this.m_szSeparator =separator; }/** * realize the queries * @param column -the column * @param command -int,string,boolean * @return -result vector * * example: -int :age<50 turn to float -string:name='tom' , name like 'tom' -boolean:gender=true * * ruler:select *----------------- id name age sex 1 tom 13 true 2 jack 23 false 3 rose 44 true 4 peter 22 false 5 kate 12 true hashtable id vectorid 1,2,3,4,5 name vectorname tom,jack,rose,peter,kate age vectorage 13,23,44,22,12 sex vectorsex true,false,true,false true *---------------return * * * */ public Vector queryData(int column, String command){ return null; } public void storeData(){ String szdata=""; } /** * get a recorder by index * @param index the index of the vector * @return vetor(a recorder) */ private Vector getRecorder(int index){ Vector v=new Vector(); Enumeration enum=this.m_Table.elements() ; while(enum.hasMoreElements()){ Vector v2=(Vector)enum.nextElement() ; v.add(v2.elementAt(index)); } return v; } /** * get printing string * @param result -vector int vector,full records * @return */ private String getPrintString (Vector result){ Enumeration enum=this.m_vColumn.elements(); String recordtitle=""; while(enum.hasMoreElements() ){ recordtitle=recordtitle+(String)enum.nextElement() +this.m_szSeparator; } recordtitle=recordtitle+this.m_ENTER; String records="";//all records int count=this.getRecordCount(); for(int i=0;i<count;i++){ Vector v=this.getRecorder(i); records=records+this.getVectorString(v)+this.m_ENTER; } records=recordtitle+records; return records; } /** * get records count * @return count */ public int getRecordCount(){ Enumeration enum=this.m_Table.elements(); Vector v=new Vector(); if(enum.hasMoreElements()){ v=(Vector)enum.nextElement(); } return v.size() ; } /** *get string from vector(single vector,elements are object ,not vector) * @param vector * @return */ private String getVectorString (Vector vector){ Enumeration enum=vector.elements() ; String records=""; while(enum.hasMoreElements()){ records=records+(String)enum.nextElement()+this.m_szSeparator; } return records; } public String getType(){ return ""; } //--------------------------------debug start /** * show all the data in the result * @param result -vector int vector,full records */ private void printResult(Vector result){ String records=this.getPrintString(result); System.out.println(records); } /** * spy the data in the hashtable */ private void spyHashTable(){ Enumeration enum=this.m_Table.elements(); Vector vector=new Vector(); while(enum.hasMoreElements()){ Vector v=(Vector)enum.nextElement() ; vector.add(v); } this.printResult(vector) ; } /** * spy the data in the vector */ private void spyVector(Vector vector){ String records=this.getVectorString(vector); System.out.println(records); }//-------------------------------------debug end public static void main(String[] args) { TextSQL textSQL1 = new TextSQL("TextSQL.txt"," "); textSQL1.loadInData(); textSQL1.spyHashTable() ;//spy the hashtable Vector v=textSQL1.getRecorder(2); textSQL1.spyVector(v) ;//spy the vector textSQL1.spyVector(textSQL1.m_vColumn );//spyVector System.out.println("Records Count: "+textSQL1.getRecordCount());//get Record Count /* System.out.println(Float.parseFloat("35")); System.out.println(Float.parseFloat("35")>20); */ } }
更麻烦,哎,看来,这个程序你是做不成了:)
import com.newman.io.FileReadWrite;
import com.newman.lang.MyString;
import com.newman.lang.StringToken;/**
* <p>Title: This follow is newman's writing</p>
* <p>Description: I want better writing ,instead of best one!</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Shu</p>
* @author Newman
* @version 1.0
*/
/**
* 查询,写入函数没有实现
* 觉得没有必要写这个类,实现sql查询太复杂了。
*//**
* This is the core class.
* Function:
* 1.It can read the file and input the content into the memory in the way of hashtable.
* Hashtable contains some vectors , just the columns.
* So the user can query the content in the certain column .
* 2.It also can save the changes back to the text.
* So the user can modify the content .
*/
public class TextSQL { String m_szFileName;//database filename
Hashtable m_Table;//database :store the vector
String m_szSeparator;//string separator
Vector m_vColumn;//the column vector
String m_ENTER="\r\n"; public TextSQL(String filename, String separator) {
this.m_szFileName =filename;
this.m_Table =new Hashtable();
this.m_szSeparator=separator;
this.m_vColumn =new Vector();
} /**
* Load into memory
*/
public void loadInData() {
if(MyString.Assert(this.m_szFileName)&&MyString.Assert(this.m_szSeparator)){
FileReadWrite frw=new FileReadWrite(this.m_szFileName);
Enumeration enum=frw.getContentEnumeration();
boolean bflag=true;//create is true
while(enum.hasMoreElements()){
StringToken st=new StringToken((String)enum.nextElement(),this.m_szSeparator);
ArrayList list=new ArrayList();
list=st.workString();//get the data in line
if(list==null)//the file is null
return ;
int ColumnCount=list.size(); if(bflag){//at the first time
for(int i=0;i<ColumnCount;i++ ){
Vector v=new Vector();
this.m_Table .put(list.get(i),v);
this.m_vColumn .add(list.get(i)) ;//read the column
}
bflag=false;
}
else{
Enumeration enum2=this.m_Table.elements();
int i=0;
while(enum2.hasMoreElements()){
Vector v2=(Vector)enum2.nextElement();
v2.add(list.get(i));
i++;
}
}
}
}
else{
System.out.println("The database filename hasn't been defined!");
return;
}
} /**
* set the separator
* @param separator
*/
public void setSeparator(String separator){
this.m_szSeparator =separator;
}/**
* realize the queries
* @param column -the column
* @param command -int,string,boolean
* @return -result vector
*
* example:
-int :age<50 turn to float
-string:name='tom' , name like 'tom'
-boolean:gender=true
*
* ruler:select *-----------------
id name age sex
1 tom 13 true
2 jack 23 false
3 rose 44 true
4 peter 22 false
5 kate 12 true hashtable
id vectorid
1,2,3,4,5
name vectorname
tom,jack,rose,peter,kate
age vectorage
13,23,44,22,12
sex vectorsex
true,false,true,false true *---------------return
*
*
*
*/
public Vector queryData(int column, String command){
return null; } public void storeData(){
String szdata="";
} /**
* get a recorder by index
* @param index the index of the vector
* @return vetor(a recorder)
*/
private Vector getRecorder(int index){
Vector v=new Vector();
Enumeration enum=this.m_Table.elements() ;
while(enum.hasMoreElements()){
Vector v2=(Vector)enum.nextElement() ;
v.add(v2.elementAt(index));
}
return v;
} /**
* get printing string
* @param result -vector int vector,full records
* @return
*/
private String getPrintString (Vector result){
Enumeration enum=this.m_vColumn.elements();
String recordtitle="";
while(enum.hasMoreElements() ){
recordtitle=recordtitle+(String)enum.nextElement() +this.m_szSeparator;
}
recordtitle=recordtitle+this.m_ENTER; String records="";//all records
int count=this.getRecordCount(); for(int i=0;i<count;i++){
Vector v=this.getRecorder(i);
records=records+this.getVectorString(v)+this.m_ENTER;
}
records=recordtitle+records;
return records;
} /**
* get records count
* @return count
*/
public int getRecordCount(){
Enumeration enum=this.m_Table.elements();
Vector v=new Vector();
if(enum.hasMoreElements()){
v=(Vector)enum.nextElement();
}
return v.size() ;
} /**
*get string from vector(single vector,elements are object ,not vector)
* @param vector
* @return
*/
private String getVectorString (Vector vector){
Enumeration enum=vector.elements() ;
String records="";
while(enum.hasMoreElements()){
records=records+(String)enum.nextElement()+this.m_szSeparator;
}
return records;
} public String getType(){
return "";
}
//--------------------------------debug start
/**
* show all the data in the result
* @param result -vector int vector,full records
*/
private void printResult(Vector result){
String records=this.getPrintString(result);
System.out.println(records);
} /**
* spy the data in the hashtable
*/
private void spyHashTable(){
Enumeration enum=this.m_Table.elements();
Vector vector=new Vector();
while(enum.hasMoreElements()){
Vector v=(Vector)enum.nextElement() ;
vector.add(v);
}
this.printResult(vector) ;
} /**
* spy the data in the vector
*/
private void spyVector(Vector vector){
String records=this.getVectorString(vector);
System.out.println(records);
}//-------------------------------------debug end public static void main(String[] args) {
TextSQL textSQL1 = new TextSQL("TextSQL.txt"," ");
textSQL1.loadInData();
textSQL1.spyHashTable() ;//spy the hashtable Vector v=textSQL1.getRecorder(2);
textSQL1.spyVector(v) ;//spy the vector textSQL1.spyVector(textSQL1.m_vColumn );//spyVector System.out.println("Records Count: "+textSQL1.getRecordCount());//get Record Count
/*
System.out.println(Float.parseFloat("35"));
System.out.println(Float.parseFloat("35")>20);
*/ }
}
这个是DOM,上面那个是JDOM.上面那个内存比较耗。看看这个! DOMhttp://expert.csdn.net/Expert/topic/1425/1425570.xml?temp=.6834223
我的email是 [email protected]
其实我也知道等学完 jdbc和jsp了以后会很容易就做出来,但是我想通过这个模拟一下底层的具体实现,而且一直在用 Editplus的编译器,呵呵 是不是比较傻. -_-