java 控制台输出的系统要用的数据库的增删改查。
java控制台输出的系统要用的数据库的增删改查。哪个大神有啊,我快崩溃了。代码不用多500左右就行。...
java 控制台输出的系统要用的数据库的增删改查。哪个大神有啊,我快崩溃了。代码不用多500左右就行。
展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
2个回答
展开全部
package com.list;
/*
* Author: qcq
* Date:2015/7/20
* E-mail:qinchuanqing918@163.com
* */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Query {
class Person {
public String Id; /*should be sure this is unique*/
public String name;
public int age;
public boolean sex;/*true for man, false for girl*/
@Override
public String toString() {
return Id + " " + name + " " + age + " " + (sex? "M" : "F");
}
}
private Map<String, Person> people;
private String fileName;
public Query(String fileName){
people = new HashMap<String, Person>();
this.fileName = fileName;
if(!initial()){
System.out.println("The initial procedure failed may be because wrong"
+ "store file path, please checkit, and rerun this program.");
}
}
private boolean initial(){
boolean flag = false;
try {
File file = new File(fileName);
if (!file.exists()){
System.out.println("The file not exist, we will create it for you");
file.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while(null != line && !line.equals("")){
String[] splitLine = line.split(" +");
if (splitLine.length < 4){
System.out.println("The information not enough");
return flag;
}
Person temp = new Person();
temp.Id = splitLine[0];
temp.name = splitLine[1];
temp.age = Integer.valueOf(splitLine[2]);
temp.sex = splitLine[3].equals("M")? true:false;
people.put(temp.Id, temp);
line = reader.readLine();
}
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public boolean writeInFile(){
boolean flag = false;
try {
FileWriter writer = new FileWriter(fileName);
Set<String> key = people.keySet();
for (String keyIndex: key){
writer.write(people.get(keyIndex).toString() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**/
public boolean add(){
return false;
}
public boolean delete(){
return false;
}
public boolean query(){
return false;
}
public boolean update(){
return false;
}
public static void main(String[] args){
Query data = new Query("temp.txt");/*here should define the file name*/
Scanner in = new Scanner(System.in);
System.out.println("Here you can choice which option:\n"
+ "quit: for quit"
+ "a: for add"
+ "d: for delete"
+ "u: for update"
+ "q: for query");
String option = in.next();
label:
while(null != option && !option.equals("")){
switch (option){
case "quit" : break label;
case "a" :
/*here add the function*/
data.add();
break;
case "d" :
/*here add the function*/
data.delete();
break;
case "u":
/*here add the function*/
data.update();
break;
case "q" :
/*here add the function*/
data.query();
break;
default:
break label;
}
}
if (null != in){
in.close();
}
}
}
/*
* Author: qcq
* Date:2015/7/20
* E-mail:qinchuanqing918@163.com
* */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Query {
class Person {
public String Id; /*should be sure this is unique*/
public String name;
public int age;
public boolean sex;/*true for man, false for girl*/
@Override
public String toString() {
return Id + " " + name + " " + age + " " + (sex? "M" : "F");
}
}
private Map<String, Person> people;
private String fileName;
public Query(String fileName){
people = new HashMap<String, Person>();
this.fileName = fileName;
if(!initial()){
System.out.println("The initial procedure failed may be because wrong"
+ "store file path, please checkit, and rerun this program.");
}
}
private boolean initial(){
boolean flag = false;
try {
File file = new File(fileName);
if (!file.exists()){
System.out.println("The file not exist, we will create it for you");
file.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
while(null != line && !line.equals("")){
String[] splitLine = line.split(" +");
if (splitLine.length < 4){
System.out.println("The information not enough");
return flag;
}
Person temp = new Person();
temp.Id = splitLine[0];
temp.name = splitLine[1];
temp.age = Integer.valueOf(splitLine[2]);
temp.sex = splitLine[3].equals("M")? true:false;
people.put(temp.Id, temp);
line = reader.readLine();
}
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public boolean writeInFile(){
boolean flag = false;
try {
FileWriter writer = new FileWriter(fileName);
Set<String> key = people.keySet();
for (String keyIndex: key){
writer.write(people.get(keyIndex).toString() + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**/
public boolean add(){
return false;
}
public boolean delete(){
return false;
}
public boolean query(){
return false;
}
public boolean update(){
return false;
}
public static void main(String[] args){
Query data = new Query("temp.txt");/*here should define the file name*/
Scanner in = new Scanner(System.in);
System.out.println("Here you can choice which option:\n"
+ "quit: for quit"
+ "a: for add"
+ "d: for delete"
+ "u: for update"
+ "q: for query");
String option = in.next();
label:
while(null != option && !option.equals("")){
switch (option){
case "quit" : break label;
case "a" :
/*here add the function*/
data.add();
break;
case "d" :
/*here add the function*/
data.delete();
break;
case "u":
/*here add the function*/
data.update();
break;
case "q" :
/*here add the function*/
data.query();
break;
default:
break label;
}
}
if (null != in){
in.close();
}
}
}
追问
这是什么代码
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询