网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.如何掌握Java(J2SE篇).
.关于考SUN JAVA PROGRAMMER的几点.
.试试看把XML转成PDF的有效工具:.
.一个基于UDP数据广播的局域网络会.
.WebLogic 6.1使用技巧.
.改进后的英文字母打字游戏.
.Java语言TSP递归程序的优化.
.Java性能优化.
.无线通信技术CDPD vs GPRS大比拼.
.Jbuilder 中 Tomcat 数据源的配置.
.java连接SQL数据库.
.休闲程序空间:趣说java处理异常的.
.改写equal()的同时记得要改写has.
.发现Hibernate中文问题又一解决方.
.用Java实现的一个C/S完整聊天程序.
.Java多语言编码问题解析(2).
.SpringFramework入门实用攻略.
.1. Introduction.
.怎样用javascript计算两日期间相.
.Java学习之路全攻略——编程本质.

Hashtable简易数据库程序

发表日期:2008-1-5



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>6value<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 


上一篇:Java 程序初始化过程详解 人气:619
下一篇:用Filter实现IP过滤 人气:956
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐