MIS信息管理系统实战开发之单独使用文件实现保存
开发背景
ID、姓名、年龄为公共信息,而学生有成绩,工人有工资
定义一个抽象类Person(ID、姓名、年龄),学生是其子类,有成绩,工人是其子类有工资
ID如何定义呢?
ID最好可以自己生成,最好的方式是采用下面的编码方式:
· 标记 + 时间戳 + 三位随机数
· 例如:2009年3月22 20:10:10.345
· 学生的标记为s,工人的标记为w
· 生成的ID号: 学生 --> s20090322201010345023
工人 --> w20090322201010345023
因为现在的程序要满足文件和数据库的操作标准,所以此处应该定义出一个公共的标准 —— 接口
查询信息的时候可以进行排序操作,可以使用Comparable接口完成。
ID最好可以自己生成,最好的方式是采用下面的编码方式:
· 标记 + 时间戳 + 三位随机数
· 例如:2009年3月22 20:10:10.345
· 学生的标记为s,工人的标记为w
· 生成的ID号: 学生 --> s20090322201010345023
工人 --> w20090322201010345023
因为现在的程序要满足文件和数据库的操作标准,所以此处应该定义出一个公共的标准 —— 接口
查询信息的时候可以进行排序操作,可以使用Comparable接口完成。
整个代码中牵扯到数据层的操作
· 数据层就是指真实的数据操作 --> CRUD。
· 最终结果操作的肯定是一个人(人为工人和学生)
应该进行分开,一个是全部的学生管理,一个是全部的工人管理。
数据层操作标准定义完成之后,有两种选择,一种是直接使用子类实现,但是以后的修改不是很方便,
所以此处最好使用代理设计的思路完成,做一个中间层。
代码关系:
Main --> Menu --> PersonOperate --> DAO
· 数据层就是指真实的数据操作 --> CRUD。
· 最终结果操作的肯定是一个人(人为工人和学生)
应该进行分开,一个是全部的学生管理,一个是全部的工人管理。
数据层操作标准定义完成之后,有两种选择,一种是直接使用子类实现,但是以后的修改不是很方便,
所以此处最好使用代理设计的思路完成,做一个中间层。
代码关系:
Main --> Menu --> PersonOperate --> DAO
因为程序即要求使用文件保存,又要求使用数据库保存,所以此处可以设计出一个工厂,通过此工厂进行DAO的操作子类实例取得。
###################Michael分割线#####################
PersonDAO.java
package org.michael.demo.dao;
import java.util.Set;
import org.michael.demo.vo.Person;
// 定义具体的数据的操作方法
public interface PersonDAO {
/**
* 插入数据的操作
*
* @param person
* 插入的是一个人员信息
* @return 操作成功与否的提示
* @throws Exception
* 如果有错误,则把错误抛给调用处处理
*/
public boolean doCreate(Person person) throws Exception;
/**
* 更新数据操作
*
* @param person
* 更新的具体信息
* @return 更新成功与否的提示
* @throws Exception
* 如果有错误,则把错误抛出
*/
public boolean doUpdate(Person person) throws Exception;
/**
* 按id删除信息
*
* @param id
* 人员的编号
* @return 删除与否的提示
* @throws Exception
* 如果有错误,则在调用处处理
*/
public boolean doDelete(String id) throws Exception;
/**
* 因为查询是多个,所以要返回Set集合
*
* @return 全部的查询结果,一个Set中包含了多个Person对象
* @throws Exception
*/
public Set<Person> findAll() throws Exception;
/**
* 按id进行查询
*
* @param id
* 人员的编号
* @return 具体的人员信息
* @throws Exception
*/
public Person findById(String id) throws Exception;
/**
* 按关键字进行查询
*
* @param keyWord
* 输入的关键字
* @return 返回一组信息
* @throws Exception
*/
public Set<Person> findByLike(String keyWord) throws Exception;
}
import java.util.Set;
import org.michael.demo.vo.Person;
// 定义具体的数据的操作方法
public interface PersonDAO {
/**
* 插入数据的操作
*
* @param person
* 插入的是一个人员信息
* @return 操作成功与否的提示
* @throws Exception
* 如果有错误,则把错误抛给调用处处理
*/
public boolean doCreate(Person person) throws Exception;
/**
* 更新数据操作
*
* @param person
* 更新的具体信息
* @return 更新成功与否的提示
* @throws Exception
* 如果有错误,则把错误抛出
*/
public boolean doUpdate(Person person) throws Exception;
/**
* 按id删除信息
*
* @param id
* 人员的编号
* @return 删除与否的提示
* @throws Exception
* 如果有错误,则在调用处处理
*/
public boolean doDelete(String id) throws Exception;
/**
* 因为查询是多个,所以要返回Set集合
*
* @return 全部的查询结果,一个Set中包含了多个Person对象
* @throws Exception
*/
public Set<Person> findAll() throws Exception;
/**
* 按id进行查询
*
* @param id
* 人员的编号
* @return 具体的人员信息
* @throws Exception
*/
public Person findById(String id) throws Exception;
/**
* 按关键字进行查询
*
* @param keyWord
* 输入的关键字
* @return 返回一组信息
* @throws Exception
*/
public Set<Person> findByLike(String keyWord) throws Exception;
}
PersonDAOImplFile.java
package org.michael.demo.dao.impl;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.io.FileOperate;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.vo.Worker;
public class PersonDAOImplFile implements PersonDAO {
// 所有的内容肯定保存在一个集合之中,因为一个集合可以直接向文件中保存
// 此集合的内容最好由文件读取进来,因为文件本身中要保存对象,
// 但是此程序牵扯到第一次运行的情况
private Set<Person> allPerson;
private FileOperate fo = null;
public static String fileName = null ;
// 在构造方法中为其实例化
@SuppressWarnings( "unchecked")
public PersonDAOImplFile() {
this.fo = new FileOperate(fileName);
try {
this.allPerson = (Set) fo.load();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean doCreate(Person person) throws Exception {
// 就是插入操作
// 在原有的基础上进行增加操作
boolean flag = false;
try {
this.allPerson.add(person);
this.fo.save( this.allPerson);
flag = true;
} catch (Exception e) {
throw e;
}
return flag;
}
public boolean doDelete(String id) throws Exception {
boolean flag = false;
// 如果要删除之前必须先进行查找操作
try {
this.allPerson.remove( this.findById(id));
this.fo.save( this.allPerson);
flag = true;
} catch (Exception e) {
throw e;
}
return flag;
}
public boolean doUpdate(Person person) throws Exception {
// 无论怎样更新,里面的id是不能改变的
boolean flag = false;
try {
Person p = this.findById(person.getId());
if (person instanceof Student) {
Student oldO = (Student) p;
Student newO = (Student) person;
oldO.setId(newO.getId());
oldO.setName(newO.getName());
oldO.setAge(newO.getAge());
oldO.setScore(newO.getScore());
}
if (person instanceof Worker) {
Worker oldO = (Worker) p;
Worker newO = (Worker) person;
oldO.setId(newO.getId());
oldO.setName(newO.getName());
oldO.setAge(newO.getAge());
oldO.setSalary(newO.getSalary());
}
// 进行更新操作
this.allPerson.add(p);
this.fo.save( this.allPerson);
} catch (Exception e) {
throw e;
}
return flag;
}
public Set<Person> findAll() throws Exception {
return this.allPerson;
}
public Person findById(String id) throws Exception {
Person per = null;
try {
Iterator<Person> iter = this.allPerson.iterator();
while (iter.hasNext()) {
Person p = iter.next();
if (p.getId().equals(id)) {
// id匹配成功,则取出此对象
per = p;
break;
}
}
} catch (Exception e) {
throw e;
}
return per;
}
public Set<Person> findByLike(String keyWord) throws Exception {
Set<Person> search = new TreeSet<Person>();
Iterator<Person> iter = this.allPerson.iterator();
while (iter.hasNext()) {
// 按姓名进行模糊查询
Person p = iter.next();
if (p.getName().indexOf(keyWord) != -1) {
// 查到内容向search集合中存放
search.add(p);
}
}
return search;
}
}
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.io.FileOperate;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.vo.Worker;
public class PersonDAOImplFile implements PersonDAO {
// 所有的内容肯定保存在一个集合之中,因为一个集合可以直接向文件中保存
// 此集合的内容最好由文件读取进来,因为文件本身中要保存对象,
// 但是此程序牵扯到第一次运行的情况
private Set<Person> allPerson;
private FileOperate fo = null;
public static String fileName = null ;
// 在构造方法中为其实例化
@SuppressWarnings( "unchecked")
public PersonDAOImplFile() {
this.fo = new FileOperate(fileName);
try {
this.allPerson = (Set) fo.load();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean doCreate(Person person) throws Exception {
// 就是插入操作
// 在原有的基础上进行增加操作
boolean flag = false;
try {
this.allPerson.add(person);
this.fo.save( this.allPerson);
flag = true;
} catch (Exception e) {
throw e;
}
return flag;
}
public boolean doDelete(String id) throws Exception {
boolean flag = false;
// 如果要删除之前必须先进行查找操作
try {
this.allPerson.remove( this.findById(id));
this.fo.save( this.allPerson);
flag = true;
} catch (Exception e) {
throw e;
}
return flag;
}
public boolean doUpdate(Person person) throws Exception {
// 无论怎样更新,里面的id是不能改变的
boolean flag = false;
try {
Person p = this.findById(person.getId());
if (person instanceof Student) {
Student oldO = (Student) p;
Student newO = (Student) person;
oldO.setId(newO.getId());
oldO.setName(newO.getName());
oldO.setAge(newO.getAge());
oldO.setScore(newO.getScore());
}
if (person instanceof Worker) {
Worker oldO = (Worker) p;
Worker newO = (Worker) person;
oldO.setId(newO.getId());
oldO.setName(newO.getName());
oldO.setAge(newO.getAge());
oldO.setSalary(newO.getSalary());
}
// 进行更新操作
this.allPerson.add(p);
this.fo.save( this.allPerson);
} catch (Exception e) {
throw e;
}
return flag;
}
public Set<Person> findAll() throws Exception {
return this.allPerson;
}
public Person findById(String id) throws Exception {
Person per = null;
try {
Iterator<Person> iter = this.allPerson.iterator();
while (iter.hasNext()) {
Person p = iter.next();
if (p.getId().equals(id)) {
// id匹配成功,则取出此对象
per = p;
break;
}
}
} catch (Exception e) {
throw e;
}
return per;
}
public Set<Person> findByLike(String keyWord) throws Exception {
Set<Person> search = new TreeSet<Person>();
Iterator<Person> iter = this.allPerson.iterator();
while (iter.hasNext()) {
// 按姓名进行模糊查询
Person p = iter.next();
if (p.getName().indexOf(keyWord) != -1) {
// 查到内容向search集合中存放
search.add(p);
}
}
return search;
}
}
package org.michael.demo.factory;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.proxy.PersonDAOProxyFile;
public class DAOFactory {
public static PersonDAO getPersonDAOInstance() {
return null;
}
public static PersonDAO getPersonDAOInstance(String path) {
return new PersonDAOProxyFile(path);
}
}
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.proxy.PersonDAOProxyFile;
public class DAOFactory {
public static PersonDAO getPersonDAOInstance() {
return null;
}
public static PersonDAO getPersonDAOInstance(String path) {
return new PersonDAOProxyFile(path);
}
}
FileOperate.java
package org.michael.demo.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.TreeSet;
import org.michael.demo.vo.Person;
public class FileOperate {
private File file = null;
// 在够方法处必须指定保存文件的名字
public FileOperate(String path) {
this.file = new File(path);
if (! this.file.exists()) {
// 如果内容不存在,则认为是第一次操作,需要初始化
try {
this.save( new TreeSet<Person>()) ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 把对象保存在文件之中
public void save(Object obj) throws Exception {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream( new FileOutputStream( this.file));
// 写入对象
out.writeObject(obj);
} catch (Exception e) {
throw e;
} finally {
try {
out.close();
} catch (Exception e) {
}
}
}
// 把对象从文件之中读进来
public Object load() throws Exception {
Object obj = null;
ObjectInputStream input = null;
try {
input = new ObjectInputStream( new FileInputStream( this.file));
obj = input.readObject();
} catch (Exception e) {
throw e;
} finally {
try {
input.close();
} catch (Exception e) {
}
}
return obj;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.TreeSet;
import org.michael.demo.vo.Person;
public class FileOperate {
private File file = null;
// 在够方法处必须指定保存文件的名字
public FileOperate(String path) {
this.file = new File(path);
if (! this.file.exists()) {
// 如果内容不存在,则认为是第一次操作,需要初始化
try {
this.save( new TreeSet<Person>()) ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 把对象保存在文件之中
public void save(Object obj) throws Exception {
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream( new FileOutputStream( this.file));
// 写入对象
out.writeObject(obj);
} catch (Exception e) {
throw e;
} finally {
try {
out.close();
} catch (Exception e) {
}
}
}
// 把对象从文件之中读进来
public Object load() throws Exception {
Object obj = null;
ObjectInputStream input = null;
try {
input = new ObjectInputStream( new FileInputStream( this.file));
obj = input.readObject();
} catch (Exception e) {
throw e;
} finally {
try {
input.close();
} catch (Exception e) {
}
}
return obj;
}
}
Main.java
package org.michael.demo.main;
import org.michael.demo.menu.Menu;
public class Main {
public static void main(String[] args) {
new Menu();
}
}
import org.michael.demo.menu.Menu;
public class Main {
public static void main(String[] args) {
new Menu();
}
}
Menu.java
package org.michael.demo.menu;
import org.michael.demo.operate.PersonOperate;
import org.michael.demo.operate.StudentOperate;
import org.michael.demo.operate.WorkerOperate;
import org.michael.demo.util.InputData;
public class Menu {
private InputData input = null;
private PersonOperate po = null;
private boolean sflag = true;
private boolean wflag = true;
// 菜单要有增加信息、修改信息等的功能,增加信息的时候要选择是增加学生还是工人
public Menu() {
this.input = new InputData();
while ( true) {
this.showMain();
}
}
// 显示主菜单
public void showMain() {
System.out.println( " 学校信息管理程序 ");
System.out.println( " 1、学生信息管理 ");
System.out.println( " 2、工人信息管理 ");
System.out.println( " 3、退出系统 ");
// 要进行选择
System.out.println( "\n\n 请选择所要的操作:");
switch (input.getInt()) {
case 1: {
this.sflag = true;
while (sflag) {
this.showStudent();
}
break;
}
case 2: {
this.wflag = true;
while (wflag) {
this.showWorker();
}
break;
}
case 3: {
System.out.println( "系统退出,拜拜~~~") ;
System.exit(1);
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
// 显示学生操作的菜单
public void showStudent() {
this.po = new StudentOperate();
System.out.println( " 学生信息管理 ");
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.println( "\n\n请选择具体的操作:");
switch ( this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.sflag = false;
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
// 显示工人操作的菜单
public void showWorker() {
this.po = new WorkerOperate();
System.out.println( " 工人信息管理 ");
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.println( "\n\n请选择具体的操作:");
switch ( this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.wflag = false;
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
}
import org.michael.demo.operate.PersonOperate;
import org.michael.demo.operate.StudentOperate;
import org.michael.demo.operate.WorkerOperate;
import org.michael.demo.util.InputData;
public class Menu {
private InputData input = null;
private PersonOperate po = null;
private boolean sflag = true;
private boolean wflag = true;
// 菜单要有增加信息、修改信息等的功能,增加信息的时候要选择是增加学生还是工人
public Menu() {
this.input = new InputData();
while ( true) {
this.showMain();
}
}
// 显示主菜单
public void showMain() {
System.out.println( " 学校信息管理程序 ");
System.out.println( " 1、学生信息管理 ");
System.out.println( " 2、工人信息管理 ");
System.out.println( " 3、退出系统 ");
// 要进行选择
System.out.println( "\n\n 请选择所要的操作:");
switch (input.getInt()) {
case 1: {
this.sflag = true;
while (sflag) {
this.showStudent();
}
break;
}
case 2: {
this.wflag = true;
while (wflag) {
this.showWorker();
}
break;
}
case 3: {
System.out.println( "系统退出,拜拜~~~") ;
System.exit(1);
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
// 显示学生操作的菜单
public void showStudent() {
this.po = new StudentOperate();
System.out.println( " 学生信息管理 ");
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.println( "\n\n请选择具体的操作:");
switch ( this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.sflag = false;
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
// 显示工人操作的菜单
public void showWorker() {
this.po = new WorkerOperate();
System.out.println( " 工人信息管理 ");
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.println( "\n\n请选择具体的操作:");
switch ( this.input.getInt()) {
case 1: {
this.po.add();
break;
}
case 2: {
this.po.findAll();
break;
}
case 3: {
this.po.findByLike();
break;
}
case 4: {
this.po.delete();
break;
}
case 5: {
this.po.update();
break;
}
case 6: {
this.wflag = false;
break;
}
default: {
System.out.println( " 选择错误,请重新选择!");
break;
}
}
}
}
PersonOperate.java
package org.michael.demo.operate;
public interface PersonOperate {
public void add();
public void delete();
public void update();
public void findAll();
public void findByLike();
}
public interface PersonOperate {
public void add();
public void delete();
public void update();
public void findAll();
public void findByLike();
}
StudentOperate.java
package org.michael.demo.operate;
import java.util.Iterator;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.factory.*;
public class StudentOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public StudentOperate() {
this.dao = DAOFactory.getPersonDAOInstance( "student.ser");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp( "s").getTimeStampRandom();
System.out.print( "输入学生姓名:");
String name = this.input.getString();
System.out.print( "输入学生年龄:");
int age = this.input.getInt();
System.out.print( "输入学生成绩:");
float score = this.input.getFloat();
Student s = new Student(id, name, age, score);
try {
this.dao.doCreate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete() {
String id = null;
System.out.print( "请输入要删除的学生ID:");
id = input.getString();
try {
this.dao.doDelete(id);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update() {
// 更新之前需要先查询出来
String id = null;
System.out.print( "请输入要修改的学生ID:");
id = input.getString();
Student s = null;
try {
s = (Student) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print( "输入学生姓名(原姓名:" + s.getName() + "):");
String name = this.input.getString();
System.out.print( "输入学生年龄(原年龄:" + s.getAge() + "):");
int age = this.input.getInt();
System.out.print( "输入学生成绩(原成绩:" + s.getScore() + "):");
float score = this.input.getFloat();
s.setName(name);
s.setAge(age);
s.setScore(score);
try {
this.dao.doUpdate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator<Person> iter = null;
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t成绩");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
public void findByLike() {
String keyWord = null ;
System.out.print( "请输入查询关键字:") ;
keyWord = this.input.getString() ;
Iterator<Person> iter = null;
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t成绩");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
import java.util.Iterator;
import org.michael.demo.dao.PersonDAO;
import org.michael.demo.util.InputData;
import org.michael.demo.util.TimeStamp;
import org.michael.demo.vo.Person;
import org.michael.demo.vo.Student;
import org.michael.demo.factory.*;
public class StudentOperate implements PersonOperate {
private PersonDAO dao = null;
private InputData input = null;
public StudentOperate() {
this.dao = DAOFactory.getPersonDAOInstance( "student.ser");
this.input = new InputData();
}
public void add() {
String id = new TimeStamp( "s").getTimeStampRandom();
System.out.print( "输入学生姓名:");
String name = this.input.getString();
System.out.print( "输入学生年龄:");
int age = this.input.getInt();
System.out.print( "输入学生成绩:");
float score = this.input.getFloat();
Student s = new Student(id, name, age, score);
try {
this.dao.doCreate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete() {
String id = null;
System.out.print( "请输入要删除的学生ID:");
id = input.getString();
try {
this.dao.doDelete(id);
} catch (Exception e) {
e.printStackTrace();
}
}
public void update() {
// 更新之前需要先查询出来
String id = null;
System.out.print( "请输入要修改的学生ID:");
id = input.getString();
Student s = null;
try {
s = (Student) this.dao.findById(id);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.print( "输入学生姓名(原姓名:" + s.getName() + "):");
String name = this.input.getString();
System.out.print( "输入学生年龄(原年龄:" + s.getAge() + "):");
int age = this.input.getInt();
System.out.print( "输入学生成绩(原成绩:" + s.getScore() + "):");
float score = this.input.getFloat();
s.setName(name);
s.setAge(age);
s.setScore(score);
try {
this.dao.doUpdate(s);
} catch (Exception e) {
e.printStackTrace();
}
}
public void findAll() {
Iterator<Person> iter = null;
try {
iter = this.dao.findAll().iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t成绩");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
public void findByLike() {
String keyWord = null ;
System.out.print( "请输入查询关键字:") ;
keyWord = this.input.getString() ;
Iterator<Person> iter = null;
try {
iter = this.dao.findByLike(keyWord).iterator();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println( "ID\t\t\t姓名\t年龄\t成绩");
while (iter.hasNext()) {
System.out.println(iter.next());
}
}
}
###################Michael分割线#####################
###################Michael分割线#####################
###################Michael分割线#####################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/142460
,如需转载请自行联系原作者