网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  hashtable简易数据库程序     
  文章作者:未知  文章来源:水木森林  
  查看:99次  录入:管理员--2007-11-17  
 
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 

 
 
上一篇: 用filter实现ip过滤    下一篇: java 程序初始化过程详解
  相关文档
2005 jdj readers choice awards - best java book nominees: 11-17
java 和.net在安全功能的比较 11-16
玩转java的classpath(三)jwhich的工作过程 11-16
非打印字符 11-16
jboss-ql中的like、order by的实现 11-17
java io学习基础之读写文本文件 11-17
基础入门:关于java数组的深度思考 11-17
java开发环境的安装及设定 11-17
jdbc基础 11-17
结合struts和hibernate谈j2ee架构的数据表示[转载] 11-17
java语言高级:深入浅出面向对象的复用 11-16
java中与注释有关的语法 11-17
使用jaxb从一个对象模型中产生xml文档 11-17
netscape 修复java安全漏洞 11-17
java程序员必读:基础篇(7)编程中的一些共同的问题 11-17
分享用java3d做的3d粒子特效 11-17
rootfolder 属性 11-16
java嵌入式开发(1) 11-17
漫谈ejb——enterprise javabeans(一) 11-17
jboss的jdbc数据源配置步骤详解大全 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息