使用JAVA SE 实现的人员管理系统.doc_第1页
使用JAVA SE 实现的人员管理系统.doc_第2页
使用JAVA SE 实现的人员管理系统.doc_第3页
使用JAVA SE 实现的人员管理系统.doc_第4页
使用JAVA SE 实现的人员管理系统.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于 S E 的 人员管理系统以下 是一个人员管理系统的完整代码。包括人员编号、姓名、性别、雇佣日期等的增删改查操作。使用到的技术是 java IO .DAO .Factory. 和集合等Java SE 的知识。 对于巩固Java 基础,提高Java技术有很大帮助。使用的数据库是MySql。以下的代码是 完整的代码。是直接从项目中拷贝出来的。 创建好数据库并 加载数据库驱动后,将程序部署在Eclipse中就可以 运行了。数据库脚本是:create table user(id INT AUTO_INCREMENT PRIMARY KEY,name VARCHAR(50) NOT NULL,sex VARCHAR(10) NOT NULL,birthday DATE ); 一、VO包。package vo; import java.util.Date; public class User private String name;private int id;private String sex;private Date birthday;public String getName() return name;public void setName(String name) = name;public int getId() return id;public void setId(int id) this.id = id;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public Date getBirthday() return birthday;public void setBirthday(Date birthday) this.birthday = birthday;Overridepublic String toString() return 编号: + id +姓名: + name + ,性别: + sex+ ,生日: + birthday ;二、数据库连接的dbc包。package dbc; import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException; public class DataBaseConnection private static final String DBDRIVER = org.gjt.mm.mysql.Driver;private static final String DBURL =jdbc:mysql:/localhost:3306/user;private static final String DBUSER =root;private static final String DBPASS= 123;Connection conn= null;/声明一个数据库 操作 对象,并加载驱动程序、实例化。public DataBaseConnection()try Class.forName(DBDRIVER); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace();try conn= DriverManager.getConnection(DBURL, DBUSER, DBPASS); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public Connection getConnection() / 返回这个 数据库连接对象return this.conn;public void close() /关闭数据库的操作。if(this.conn!=null)try this.conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();三、dao包。 package dao; import java.util.List; import vo.User; public interface IUserDAO /*创建一个 用户对象。 * * * */public boolean doCreate(User user) throws Exception;/* 更新一个 对象;* */public boolean doUpdata(User user) throws Exception;/* 根据 id 号删除一个信息。* param id * return*/public boolean doDelete(int id) throws Exception;/* 根据id 号查询一个用户的信息。* param id* return*/public User findById(int id) throws Exception;/* 根据关键字,查询用户信息。* param keyWord* return*/public List findAll(String keyWord) throws Exception; 四、dao的代理包。 package dao.impl; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List; import vo.User;import dao.IUserDAO; / 接口的实现类。public class IUserDAOImpl implements IUserDAO private Connection conn = null; public IUserDAOImpl(Connection conn) this.conn =conn ; public boolean doCreate(User user) throws Exception boolean flag = false;PreparedStatement pstmt = null;String sql = insert into user(name,sex,birthday) values(?,?,?);try pstmt = this.conn.prepareStatement(sql);pstmt.setString(1, user.getName();pstmt.setString(2, user.getSex();pstmt.setDate(3, new java.sql.Date(user.getBirthday().getTime();if (pstmt.executeUpdate() 0) flag = true; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); finally if (pstmt != null) try pstmt.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();/this.dbc.close(); return flag; public boolean doUpdata(User user) throws Exception boolean flag = false;PreparedStatement pstmt = null;String sql = update user set name=?,sex= ?,birthday=? where id = ?;try pstmt = this.conn.prepareStatement(sql);pstmt.setString(1, user.getName();pstmt.setString(2, user.getSex();pstmt.setDate(3, new java.sql.Date(user.getBirthday().getTime();pstmt.setInt(4, user.getId();if (pstmt.executeUpdate() 0) flag = true; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); finally if (pstmt != null) try pstmt.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();/this.dbc.close(); return flag; public boolean doDelete(int id) throws Exception boolean flag = false;PreparedStatement pstmt = null;String sql = delete from user where id =?;try pstmt = this.conn.prepareStatement(sql);pstmt.setInt(1, id);if (pstmt.executeUpdate() 0) flag = true; catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); finally if (pstmt != null) try pstmt.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();/this.dbc.close(); return flag; public User findById(int id) throws Exception User user = null;PreparedStatement pstmt = null;String sql = select id,name,sex,birthday from user where id like ?;try pstmt = this.conn.prepareStatement(sql);pstmt.setInt(1, id);ResultSet rs= pstmt.executeQuery();if(rs.next()user = new User();user.setId(rs.getInt(1);user.setName(rs.getString(2);user.setSex(rs.getString(3);user.setBirthday(rs.getDate(4);rs.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); finally if (pstmt != null) try pstmt.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();/this.dbc.close(); return user; public List findAll(String keyWord) throws Exception List all= new ArrayList();PreparedStatement pstmt = null;String sql = select id,name,sex,birthday from user where id like ? or name like ? or sex like ? or birthday like ?;try pstmt = this.conn.prepareStatement(sql);pstmt.setString(1, %+keyWord+%);pstmt.setString(2, %+keyWord+%);pstmt.setString(3, %+keyWord+%);pstmt.setString(4, %+keyWord+%);ResultSet rs= pstmt.executeQuery();while(rs.next()User user = new User();user.setId(rs.getInt(1);user.setName(rs.getString(2);user.setSex(rs.getString(3);user.setBirthday(rs.getDate(4);all.add(user);rs.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace(); finally if (pstmt != null) try pstmt.close(); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();/this.dbc.close(); return all; 五、dao的真实代理。package xy; import java.util.List; import vo.User;import dao.IUserDAO;import dao.impl.IUserDAOImpl;import dbc.DataBaseConnection; public class IUserDAOProxy implements IUserDAO private DataBaseConnection dbc = null;private IUserDAO dao= null;public IUserDAOProxy()this.dbc= new DataBaseConnection(); this.dao = new IUserDAOImpl(this.dbc.getConnection();public boolean doCreate(User user) throws Exception boolean flag =false;try this.dao.doCreate(user); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallythis.dbc.close();return flag; public boolean doUpdata(User user) throws Exception boolean flag =false;try this.dao.doUpdata(user); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallythis.dbc.close();return flag; public boolean doDelete(int id) throws Exception boolean flag =false;try this.dao.doDelete(id); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallythis.dbc.close();return flag; public User findById(int id) throws Exception User user = null;try user =this.dao.findById(id); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallythis.dbc.close();return user; public List findAll(String keyWord) throws Exception List allUser =null;try allUser=this.dao.findAll(keyWord); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();finallythis.dbc.close();return allUser; 六、工厂包。package factory; import dao.IUserDAO;import xy.IUserDAOProxy; public class DAOFactory public static IUserDAO getIUserDAOInstance()return new IUserDAOProxy();七、菜单包。package menu; import operate.UserOperate;import util.InputData; public class Menu public Menu()while(true)this.show();public void show()System.out.println(=FZQ人员管理系统=);System.out.println( 1、增加用户);System.out.println( 2、修改用户);System.out.println( 3、删除用户);System.out.println( 4、查找单个用户);System.out.println( 5、查询全部用户);System.out.print( 6、退出系统);InputData input = new InputData();int ch = input.getInt(n请选择的选项, 输入的信息不正确!);switch(ch)case 1:UserOperate.insert();break;case 2:UserOperate.updata();break;case 3:UserOperate.delete();break;case 4:UserOperate.findById();break;case 5:UserOperate.findAll();break;case 6:System.out.print(系统已退出!);System.exit(1);break;default :System.out.print(请选择正确的选项!);break;八、IO输入包。package operate; import java.util.ArrayList;import java.util.Iterator;import java.util.List; import util.InputData;import vo.User;import factory.DAOFactory; public class UserOperate public static void insert()User user =new User();InputData input = new InputData();user.setName(input.getString(请输入姓名:tt);user.setSex(input.getString(请输入性别:tt);user.setBirthday(input.getDate(请输入生日:tt, 输入的日期格式必须是yyyy-MM-dd);try DAOFactory.getIUserDAOInstance().doCreate(user); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(数据添加成功!);public static void updata()InputData input = new InputData();int id = input.getInt(请输入要修改的用户编号:tt, 编号必须是数字!);User user = null;try user = DAOFactory.getIUserDAOInstance().findById(id); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();if(user != null)user.setName(input.getString(请输入姓名(原姓名为:+user.getName()+):tt);user.setSex(input.getString(请输入性别(原性别为:+user.getSex()+):tt);user.setBirthday(input.getDate(请输入生日(原生日为:+user.getBirthday()+):tt, 输入的日期格式必须是yyyy-MM-dd);try DAOFactory.getIUserDAOInstance().doUpdata(user); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(数据修改成功!);elseSystem.out.println(要查找的用户不存在);public static void delete()InputData input = new InputData();int id = input.getInt(请输入要删除的编号:tt, 输入的编号必须是数字);try DAOFactory.getIUserDAOInstance().doDelete(id); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(数据删除成功!);public static void findById()InputData input = new InputData();User user= null;int id = input.getInt(请输入要查询的编号:tt, 编号必须是数字!);try user = DAOFactory.getIUserDAOInstance().findById(id); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();if(user!=null)System.out.println(user);elseSystem.out.println(要查询的用户不存在!);public static void findAll()InputData input = new InputData();List allUser = new ArrayList();String keyWord = input.getString(请输入要查询的关键字:tt);try allUser = DAOFactory.getIUserDAOInstance().findAll(keyWord); catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();Iterator iter = allUser.iterator();while(iter.hasNext()User user = iter.next();System.out.println(user); 九、IO输出。package util; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date; public class InputData private BufferedReader buf = null;public InputData()this.buf = new BufferedReader(new InputStreamReader(System.in);public int getInt(String info,String err)int i = 0;boolean flag= true;while(flag)String str = this.getString(info);if(str.matches(d+)i = Integer.parseInt(str);flag = false;elseSystem.out.println(err);return i;public String getString(String info)String str= null;System.out.println(info);try str = this.buf.readLine(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();return str;public Date getDate(String info,String err)Date date = null;boolean flag = true;while(flag)String str= this.getString(info);if(str.matches(d4-d2-d2)try date = new SimpleDateFormat(yyyy-MM-dd).parse(str);flag = false; catch (ParseException e) / TODO Auto-generated catch blocke.printStackTrace();elseSystem.out.println(err);return date;十、以下是 测试包。 1、测试按编号删除package test; import factory.DAOFactory; public class TestDelete public static void main(String args) throws Exception / TODO Auto-generated method stubDAOFactory.getIUserDAOInstance().doDelete(2); 2、测试 创建人员信息。package test; import java.util.Date; import factory.DAOFactory; import vo.User; public class TestDoCreat public static void main(String args) / TODO Auto-generated method stubUser user = new User();user.setName(李四);user.setSex(男);user.setBirthday(new Date();try DAOFactory.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论