⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sjk.txt

📁 初步数据库的操作,适用于初学者
💻 TXT
字号:
import java.io.*;

public class Simulate 
{   
	public static void main(String[] args)throws Exception
	{                                                             // the command list
	   System.out.println("****************************************");
           System.out.println("*     Command List(please choose)      *");
           System.out.println("****************************************");
           System.out.println("*    select table:select the table     *");
           System.out.println("****************************************");
	   System.out.println("*    select line:select the line       *");
           System.out.println("****************************************");
           System.out.println("*       deltable:delete a table        *");
           System.out.println("****************************************");
           System.out.println("*        delline:delete a line         *");
           System.out.println("****************************************");
	   System.out.println("*     insert table:insert a table      *");
           System.out.println("****************************************");

	   
   while(true){                                                    
	       System.out.println("please select the Command(exit to exit)");
	       BufferedReader order = new BufferedReader (new InputStreamReader(System.in));
		   String str = order.readLine();
		   String dictoryname = "E:\\dictionary.txt";       //the absolute path of dictionary
		   File dictory = new File(dictoryname); 
		   DataInputStream datain = new DataInputStream
						(new FileInputStream(dictory));     //inputstream
		   int dictory_row = 0;
		   int dictory_column = 0;
		   String[][] dictory_Array = new String[100][10];
		   String inf = "";
		   while((inf = datain.readLine()) != null) {       //input the data in the dictionary to a 二维数组
				String	temp[] = inf.split("\t");
					for(int i = 0; i < temp.length; i++){
						dictory_Array[dictory_row][i] = temp[i];
					}
					dictory_row++;
					dictory_column = temp.length;
		   }
          if(str.equals("exit")) { break; }
		  else if(str.equals("select table")) {            //  do the select table operation
				 System.out.println("please input the tablename:");
				 BufferedReader tablename = new BufferedReader (new InputStreamReader(System.in));
				 String table_name = tablename.readLine();
				 String filename = new String("E:\\"+table_name+"."+"txt");
				 File select_file = new File(filename);    //find the table
				 DataInputStream in = new DataInputStream
								(new FileInputStream(select_file));
				 int file_row = 0;
				 int file_column = 0;
				 String[][] file_Array = new String[100][10];
				 String string = "";
				 while((string = in.readLine()) != null) { //input the data in the table to a 二维数组
							String tep[] = string.split("\t");
							for(int i1 = 0; i1 < tep.length; i1++){
								file_Array[file_row][i1] = tep[i1];
							}
							file_row++;
							file_column = tep.length;
				 }
				 for(int a = 0; a < file_row;a++) {       //output the selected information
						for(int b = 0; b < file_column;b++) 
							System.out.print(file_Array[a][b]+"\t");
						System.out.println();	
				 }
			
			}

			else if(str.equals("select line")) { 
				 System.out.println("please input the tablename:");
				 BufferedReader tablename = new BufferedReader (new InputStreamReader(System.in));
				 String table_name = tablename.readLine();
				 String filename = new String("E:\\"+table_name+"."+"txt");
				 File select_file = new File(filename); 
				 DataInputStream in = new DataInputStream
								(new FileInputStream(select_file));
				 int file_row = 0;
				 int file_column = 0;
				 String[][] file_Array = new String[100][10];
				 String string = "";
				 while((string = in.readLine()) != null) {  //input the data in the table to a 二维数组
							String tep[] = string.split("\t");
							for(int i1 = 0; i1 < tep.length; i1++){
								file_Array[file_row][i1] = tep[i1];
							}
							file_row++;
							file_column = tep.length;
				 }
				 String[] column_name = new String[100];
				 for(int i2 = 0; i2 < dictory_row; i2++) {  //input the attribute of a table to the array
							if(table_name.equals(dictory_Array[i2][0])) {
									column_name[i2] = dictory_Array[i2][1];
							}
				 }
				 System.out.println("please input the item you choose: ");
				 System.out.println("(care)choose the number not the string!!");
				 for(int i3 = 1; i3 < column_name.length&&column_name[i3]!=null; i3++)
							System.out.println(i3+"."+column_name[i3]);
				 BufferedReader numberin = new BufferedReader (new InputStreamReader(System.in));
				 String number = numberin.readLine();
				 int num = Integer.parseInt(number);
				 if(num < column_name.length){
							 System.out.println("please input the "+column_name[num]);
							 BufferedReader choosein = new BufferedReader (new InputStreamReader(System.in));
							 String data = choosein.readLine();                //input the attribute to choose 
							 for(int t = 0;t < file_row; t++) {
									if(data.equals(file_Array[t][num-1])) {
										for(int a = 0; a < file_column;a++)
										   System.out.print(file_Array[0][a]+"\t");//output the attributes name
										System.out.println("");
										for(int b = 0; b < file_column;b++)
										   System.out.print(file_Array[t][b]+"\t");//output the selected line
									}
							 }

				 }
		 
			}

			else if(str.equals("deltable")) {
				 System.out.println("please input the tablename:");
				 BufferedReader tablename = new BufferedReader (new InputStreamReader(System.in));
				 String table_name = tablename.readLine();
				 String filename = new String("E:\\"+table_name+"."+"txt");
				 File select_file = new File(filename);
				 select_file.delete();
				 int   p=0;                                 
				 String   fstr;                                                        
				 String[]   fff = new String[100];    
				 FileReader   fr1   =new   FileReader(dictoryname);                     
				 BufferedReader   bfr1=new   BufferedReader(fr1);   
				 while((fstr=bfr1.readLine())!=null){    //read every line to the array
				  fff[p]=fstr;   
				  p++;   
				 } 
				 FileWriter   fw1   =new   FileWriter(dictoryname,false);   
				 for(int a = 0; a < dictory_row; a++){   //write every line to the file except the selected line
						 if((fff[a].length()>0)&&((dictory_Array[a][0].compareTo(table_name))!=0))   
								fw1.write(fff[a]+"\r\n");   
				 } 
				 fr1.close();
				 fw1.close();

			}

			else if(str.equals("delline")) {
				System.out.println("please input the tablename:");
				 BufferedReader tablename = new BufferedReader (new InputStreamReader(System.in));
				 String table_name = tablename.readLine();
				 String filename = new String("E:\\"+table_name+"."+"txt");
				 File select_file = new File(filename); 
				 DataInputStream in = new DataInputStream
								(new FileInputStream(select_file));
				 int file_row = 0;
				 int file_column = 0;
				 String[][] file_Array = new String[100][10];
				 String string = "";
				 while((string = in.readLine()) != null) {
							String tep[] = string.split("\t");
							for(int i1 = 0; i1 < tep.length; i1++){
								file_Array[file_row][i1] = tep[i1];
							}
							file_row++;
							file_column = tep.length;
				 }
				 String[] column_name = new String[100];
				 for(int i2 = 0; i2 < dictory_row; i2++) {
							if(table_name.equals(dictory_Array[i2][0])) {
									column_name[i2] = dictory_Array[i2][1];
							}
				 }
				 System.out.println("please input the item you choose: ");
				 System.out.println("(care)choose the number not the string!!");
				 for(int i3 = 1; i3 < column_name.length&&column_name[i3]!=null; i3++)
							System.out.println(i3+"."+column_name[i3]);
				 BufferedReader numberin = new BufferedReader (new InputStreamReader(System.in));
				 String number = numberin.readLine();
				 int num = Integer.parseInt(number);
				 if(num < column_name.length){                                //select the attribute
							 System.out.println("please input the "+column_name[num]);
							 BufferedReader choosein = new BufferedReader (new InputStreamReader(System.in));
							 String data = choosein.readLine();
							 for(int t = 0;t < file_row; t++) {
									if(data.equals(file_Array[t][num-1])) {  //delete the selected line 
													 int   p=0;   
													 String   fstr;   
													 String[]   fff = new String[100];    
													 FileReader   fr1   =new   FileReader(filename);   
													 BufferedReader   bfr1=new   BufferedReader(fr1);   
													 while((fstr=bfr1.readLine())!=null){   
													  fff[p]=fstr;   
													  p++;   
													 } 
													 FileWriter   fw1   =new   FileWriter(filename,false);
													 for(int a = 0; a < dictory_row; a++)
														 if(a!=t)
															 fw1.write(fff[a]+"\r\n");   
													 fr1.close();
													 fw1.close();
									}
							 }
				 }
			}
			else if(str.equals("insert table")) {
				System.out.println("please insert the table name");
				BufferedReader inserin = new BufferedReader (new InputStreamReader(System.in));
				String table_name = inserin.readLine();
				String filename = new String("E:\\"+table_name+"."+"txt");
				System.out.println("please insert the attribute type length(用Tab键隔开,每一个attribute请隔行输入)");
				System.out.println("input exit1 to finish the input");
				while(true) {                                  //you can input as many attributes you can
						BufferedReader attriin = new BufferedReader (new InputStreamReader(System.in));
						String attributes = attriin.readLine();
						if(attributes.equals("exit1"))  break;
						String[] attribute = attributes.split("\t");
						String line = new String(table_name+"\t"+attribute[0]+"\t"+attribute[1]+"\t"+attribute[2]);
						FileWriter   fw=new   FileWriter(dictoryname, true);     
						BufferedWriter   bw=new   BufferedWriter(fw);  //write the attributes and table to the dictionary
						  bw.write(line);  
						  bw.newLine();
						  bw.flush();     
						  bw.close();   
						  fw.close();
						File newtable = new File(filename);
						newtable.createNewFile();
						String line1 = new String(attribute[0]);
						FileWriter   fw1=new   FileWriter(filename, true);     
						BufferedWriter   bw1=new   BufferedWriter(fw1); //write attributes name to the new file 
						  bw1.write(line1);  
						  bw1.write("\t");
						  bw1.flush();     
						  bw1.close();     
						  fw1.close();
				}
			}
			else { System.out.println("it is not a commandation!!"); }
     }
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -