服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

hashtable简易数据库程序


import java.io.*; 
import java.util.*; 
class str14 

inputstreamreader stdin=new inputstreamreader(system.in); 
bufferedreader bufin=new bufferedreader(stdin); 

public static void main(string args[]) throws ioexception 

new str14();//run the application 


public str14() throws ioexception//constructor 

hashtable htable=new hashtable(20,0.75f); 
database(htable); 


public str14(hashtable htable) throws ioexception //override the constructor 

database(htable); 


public void database(hashtable htable) throws ioexception 

int count=htable.size();//get the ammount of the data in htable 
int value,id,num; 
string key,enter,data; 
enumeration keys=htable.keys();//get all the key in htable 
enumeration elements=htable.elements();//get all the elements in htable 
system.out.println("
hashtable 简易数据库程序
"); 
system.out.println("(1)输入数据"); 
system.out.println("(2)请除所有数据"); 
system.out.println("(3)显示单笔数据"); 
system.out.println("(4)删除单笔数据"); 
system.out.println("(5)显示所有数据"); 
system.out.println("(6)结束程序"); 
system.out.print("请输入您的选择:"); 
value=select();//call select(),return the function number 
switch(value)//the function 

case 1: 
system.out.print("
请输入一笔数据:");//need data input 
data=bufin.readline(); 
count++; 
key=string.valueof(count); 
htable.put(key,data);//store it to the htable 
system.out.print("
输入完成,按任意键继续..."); 
enter=bufin.readline(); 
new str14(htable);//reenter 
break; 
case 2://clear all data from the htable 
htable.clear(); 
system.out.print("
已删除了所有数据... 按任意键继续..."); 
enter=bufin.readline(); 
new str14(htable);//reenter 
break; 
case 3: 
system.out.print("
请输入要显示的数据编号:"); 
id=getid(count);//call getid() 
key=string.valueof(id); 
object select=htable.get(key);//fetch the data from the htable 
data=select.tostring(); 
system.out.print("
编号"+" "+"内容"); 
system.out.println("
 "+key+" "+data);//display the data 
system.out.print("
按任意键继续..."); 
enter=bufin.readline(); 
new str14(htable);//reenter 
break; 
case 4: 
system.out.print("请输入要删除的数据编号:"); 
id=getid(count); 
key=string.valueof(id); 
htable.remove(key);//remove data 
count--; 
num=count; 
system.out.print("已删除了所选择的数据...按任意见继续..."); 
hashtable htable1=new hashtable(20,0.75f);//create new htable named htable1 
elements=htable.elements();//fetch all the data from htable 
while(elements.hasmoreelements()) 

key=string.valueof(num);//a new value 
data=(string)elements.nextelement();//fetch content of the data 
htable1.put(key,data);//store it to htable1 
num--; 

htable.clear(); 
enter=bufin.readline(); 
new str14(htable1); 
break; 
case 5: 
string[] sortkey=new string[count];//create a new sort array 
string[] sortdata=new string[count]; 
num=count; 
elements=htable.elements(); 
keys=htable.keys(); 
while(elements.hasmoreelements()) 

key=(string)keys.nextelement();//fetch value of key 
data=(string)elements.nextelement();//fetch value of data 
sortkey[num-1]=key;//store the value of key to sortkey array 
sortdata[num-1]=data;//store the value of data to sortdata array 
num--; 

system.out.println("
编号"+" "+"内容"); 
for(int i=0;i<count;i++)//display all content after sorting 
system.out.println(" "+sortkey[i]+" "+sortdata[i]); 
system.out.print("
目前共有"+count+"笔数据"); 
system.out.print("

按任意键继续..."); 
enter=bufin.readline(); 
new str14(htable); 
break; 
default: 



public int select() throws ioexception//method of getting a function selector 

string input; 
int value=0; 
input=bufin.readline();//read a input from keyboard 
try 

value=integer.parseint(input);//convert a string to a int value 
}catch(numberformatexception e)//can’t vonverted 

system.out.print("请输入选项1~6:"); 
value=select(); 

if(value>6||value<1)//if exceed then print a message and reenter 

system.out.print("请输入选项1~6:"); 
value=select(); 

return value;//return a value 


public int getid(int count)throws ioexception//a method of return the number of data 

string input; 
int value=0; 
input=bufin.readline();//read a user input string from keyboard 
try 

value=integer.parseint(input);//convert the string to a int 
}catch(numberformatexception e)//if can’t convert to a integer then reenter 

system.out.print("请输入数据编号:"); 
value=getid(count); 

if(value>count)//the input value is out of bound 

system.out.print("无此编号的数据,请重新输入:"); 
getid(count); 

return value;//return a value 

扫描关注微信公众号