人员管理程序(共11页).doc
精选优质文档-倾情为你奉上(1)package org.lxh.useradmin.dao;import java.util.List;import org.lxh.useradmin.vo.User;public interface IUserDAO /* * 表示数据库的增加操作 * * param user * return * throws Exception */public boolean doCreate(User user) throws Exception;public boolean doUpdate(User user) throws Exception;/* * 表示删除操作,按编号删除 * * param id * return * throws Exception */public boolean doDelete(int id) throws Exception;/* * 表示数据库的查询操作 * param id * return * throws Exception */public User findById(int id) throws Exception;/* * 查询的时候将返回一组对象 * param keyWord * return * throws Exception */public List<User> findAll(String keyWord) throws Exception;(2)package org.lxh.useradmin.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import org.lxh.useradmin.dao.IUserDAO;import org.lxh.useradmin.dbc.DataBaseConnection;import org.lxh.useradmin.vo.User;public class IUserDAOImpl implements IUserDAO private DataBaseConnection dbc = null;private Connection conn = null;public IUserDAOImpl() this.dbc = new DataBaseConnection();this.conn = this.dbc.getConnection();Overridepublic 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(); / 所有的内容从user类中取出pstmt.setString(2, user.getSex(); / 所有的内容从user类中取出pstmt.setDate(3, new java.sql.Date(user.getBirthday().getTime();if (pstmt.executeUpdate() > 0) / 至少已经更新了一行flag = true; catch (Exception e) throw e; finally / 不管如何抛出,最终肯定是要进行数据库的关闭操作的if (pstmt != null) try pstmt.close(); catch (Exception e1) this.dbc.close();return flag;Overridepublic 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); / 所有的内容从user类中取出if (pstmt.executeUpdate() > 0) / 至少已经更新了一行flag = true; catch (Exception e) throw e; finally / 不管如何抛出,最终肯定是要进行数据库的关闭操作的if (pstmt != null) try pstmt.close(); catch (Exception e1) this.dbc.close();return flag;Overridepublic boolean doUpdate(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(); / 所有的内容从user类中取出pstmt.setString(2, user.getSex(); / 所有的内容从user类中取出pstmt.setDate(3, new java.sql.Date(user.getBirthday().getTime();pstmt.setInt(4, user.getId();if (pstmt.executeUpdate() > 0) / 至少已经更新了一行flag = true; catch (Exception e) throw e; finally / 不管如何抛出,最终肯定是要进行数据库的关闭操作的if (pstmt != null) try pstmt.close(); catch (Exception e1) this.dbc.close();return flag;Overridepublic List<User> findAll(String keyWord) throws Exception List<User> all = new ArrayList<User>();PreparedStatement pstmt = null;String sql = "SELECT id,name,sex,birthday FROM user WHERE 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 + "%");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) throw e; finally / 不管如何抛出,最终肯定是要进行数据库的关闭操作的if (pstmt != null) try pstmt.close(); catch (Exception e1) this.dbc.close();return all;Overridepublic User findById(int id) throws Exception User user = null ;PreparedStatement pstmt = null;String sql = "SELECT id,name,sex,birthday FROM user WHERE id=?"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) throw e; finally / 不管如何抛出,最终肯定是要进行数据库的关闭操作的if (pstmt != null) try pstmt.close(); catch (Exception e1) this.dbc.close();return user;(3)package org.lxh.useradmin.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/mldn" ;private static final String DBUSER = "root" ;private static final String DBPASS = "mysqladmin" ;private 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();(4)package org.lxh.useradmin.factory;import org.lxh.useradmin.dao.IUserDAO;import org.lxh.useradmin.dao.impl.IUserDAOImpl;public class DAOFactory public static IUserDAO getIUserDAOInstance()return new IUserDAOImpl() ;(5)package org.lxh.useradmin.test;import java.util.Iterator;import java.util.List;import org.lxh.useradmin.factory.DAOFactory;import org.lxh.useradmin.vo.User;public class TestAll public static void main(String args) throws Exception List<User> allUser = DAOFactory.getIUserDAOInstance().findAll("") ;Iterator<User> iter = allUser.iterator() ;while(iter.hasNext()User user = iter.next() ;System.out.println(user);package org.lxh.useradmin.test;import org.lxh.useradmin.factory.DAOFactory;public class TestDelete public static void main(String args) throws Exception DAOFactory.getIUserDAOInstance().doDelete(2);package org.lxh.useradmin.test;import org.lxh.useradmin.factory.DAOFactory;import org.lxh.useradmin.vo.User;public class TestId public static void main(String args) throws Exception User user = DAOFactory.getIUserDAOInstance().findById(1) ;System.out.println(user);package org.lxh.useradmin.test;import java.util.Date;import org.lxh.useradmin.factory.DAOFactory;import org.lxh.useradmin.vo.User;public class TestInsert public static void main(String args) throws Exception User user = new User();user.setName("李兴华");user.setSex("男");user.setBirthday(new Date();DAOFactory.getIUserDAOInstance().doCreate(user);package org.lxh.useradmin.test;import java.util.Date;import org.lxh.useradmin.factory.DAOFactory;import org.lxh.useradmin.vo.User;public class TestUpdate public static void main(String args) throws Exception User user = new User();user.setName("张心");user.setSex("女");user.setId(2) ;user.setBirthday(new Date();DAOFactory.getIUserDAOInstance().doUpdate(user);(6)DROP TABLE user ;CREATE TABLE user(idINTAUTO_INCREMENTPRIMARY KEY ,nameVARCHAR(50)NOT NULL ,sexVARCHAR(10)NOT NULL ,birthdayDATE) ;(7)package org.lxh.useradmin.vo;import java.util.Date;public class User private int id;public int getId() return id;public void setId(int id) this.id = id;public String getName() return name;public void setName(String name) this.name = name;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;private String name;private String sex;private Date birthday;Overridepublic String toString() return "编号:" + this.id + ";姓名:" + this.name + ";性别:" + this.sex+ ";生日:" + this.birthday;专心-专注-专业