當前位置:編程學習大全網 - 編程語言 - 用JAVA編寫壹個APPLET,實現用戶輸入數據並寫入數據庫和單個字段的查詢

用JAVA編寫壹個APPLET,實現用戶輸入數據並寫入數據庫和單個字段的查詢

不知道是不是妳想要的

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

}

}

  • 上一篇:什麽是禦女車
  • 下一篇:蘋果手機怎麽看成人教育學習視頻怎麽樣?
  • copyright 2024編程學習大全網