项目1 - 人事管理系统

1.做个人事管理系统

第一个类主类

package wode;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * Created by ttc on 18-1-23.
 */
public class JDBCDemoFinal {
    public static int delectEmpById(int id) throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("DELETE FROM EMP WHERE sid=?");
        preparedStatement.setInt(1,id);
        int rows=preparedStatement.executeUpdate();
        JDBCUtils.close(preparedStatement,connection);
        return rows;
    }
    public static int update(Emp emp) throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("update emp set sname=?,passwordnum=?,email=?,permission=?,birthday=?WHERE sid=?");
        preparedStatement.setString(1,emp.getSname());
        preparedStatement.setInt(2,emp.getPasswordnum());
        preparedStatement.setString(3,emp.getEmail());
        preparedStatement.setString(4,emp.getPermission());
        Date date=emp.getBirthday();
        long time=date.getTime();
        java.sql.Timestamp sqlDate = new java.sql.Timestamp(time);
        preparedStatement.setTimestamp(5,sqlDate);
        preparedStatement.setInt(6,emp.getSid());
        int rows=preparedStatement.executeUpdate();
        JDBCUtils.close(preparedStatement,connection);
        return rows;
    }

    public static int AddEmp(Emp emp) throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("INSERT INTO emp (sname,passwordnum,email,permission,birthday)VALUES (?,?,?,?,?)");
        preparedStatement.setString(1,emp.getSname());
        preparedStatement.setInt(2,emp.getPasswordnum());
        preparedStatement.setString(3,emp.getEmail());
        preparedStatement.setString(4,emp.getPermission());
        Date date=emp.getBirthday();
        long time2=date.getTime();
        java.sql.Timestamp sqlDate = new java.sql.Timestamp(time2);
        preparedStatement.setTimestamp(5,sqlDate);
        int rows=preparedStatement.executeUpdate();
        JDBCUtils.close(preparedStatement,connection);
        return rows;
    }
    public static Emp getEmpById(int id) throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp WHERE sid=?");
        preparedStatement.setInt(1,id);
        ResultSet resultSet=preparedStatement.executeQuery();
        Emp emp=null;
        if (resultSet.next()==true)
        {
            int sid=resultSet.getInt("sid");
            String name=resultSet.getString("sname");
            int password=resultSet.getInt("passwordnum");
            String email=resultSet.getString("email");
            String permission=resultSet.getString("permission");
            Date date=resultSet.getTimestamp("birthday");
            emp=new Emp();
            emp.setSid(sid);
            emp.setSname(name);
            emp.setPasswordnum(password);
            emp.setEmail(email);
            emp.setPermission(permission);
            emp.setBirthday(date);
        }
        JDBCUtils.close(resultSet,preparedStatement,connection);
        return emp;
    }
    public static Emp getEmpBySname(String yname) throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp WHERE sname like ?");
        preparedStatement.setString(1,"%"+yname+"%");
        ResultSet resultSet=preparedStatement.executeQuery();
        Emp emp=null;
        if (resultSet.next()==true)
        {
            int sid=resultSet.getInt("sid");
            String name=resultSet.getString("sname");
            int password=resultSet.getInt("passwordnum");
            String email=resultSet.getString("email");
            String permission=resultSet.getString("permission");
            Date date=resultSet.getTimestamp("birthday");
            emp=new Emp();
            emp.setSid(sid);
            emp.setSname(name);
            emp.setPasswordnum(password);
            emp.setEmail(email);
            emp.setPermission(permission);
            emp.setBirthday(date);
        }
        JDBCUtils.close(resultSet,preparedStatement,connection);
        return emp;
    }
    public static List<Emp> getAllEmpBySname() throws SQLException {
        Connection connection=JDBCUtils.getConn();
        PreparedStatement preparedStatement=connection.prepareStatement("SELECT *FROM Emp");
        List<Emp>empList=new ArrayList<>();
        ResultSet resultSet=preparedStatement.executeQuery();
         while (resultSet.next()==true)
                {
                int sid=resultSet.getInt("sid");
                String name=resultSet.getString("sname");
                int password=resultSet.getInt("passwordnum");
                String email=resultSet.getString("email");
                String permission=resultSet.getString("permission");
                Date date=resultSet.getTimestamp("birthday");
               Emp emp=new Emp();
                emp.setSid(sid);
                emp.setSname(name);
                emp.setPasswordnum(password);
                emp.setPermission(permission);
                emp.setEmail(email);
                emp.setBirthday(date);
                empList.add(emp);
            }
        JDBCUtils.close(resultSet,preparedStatement,connection);
        return empList;
    }
    public static void main(String[] args) throws SQLException, ParseException {
//       Emp emp=new Emp();
//      emp.setSname("zhangsan");
//      emp.setPasswordnum(123);
//      emp.setEmail("zhangsan@163.com");
//       emp.setPermission("管理员");
//        String date1="1980-05-13";
//        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        Date date=simpleDateFormat.parse(date1);
//        emp.setBirthday(date);
//        AddEmp(emp);

        System.out.println("欢迎使用neusoft的用户管理系统");
        System.out.println("=============================");
        System.out.println("用户登录-----------------1");
        System.out.println("用户注册-----------------2");
        System.out.println("退出程序-----------------3");
        Scanner scanner=new Scanner(System.in);
        while (true)
        {
            int command=scanner.nextInt();
            if (command==2)
            {
                System.out.println("用户注册界面");
                System.out.println("=============================");
                System.out.println("请输入你的用户名");
                String name=scanner.next();
                System.out.println("请输入你的密码");
                int num=scanner.nextInt();
                System.out.println("请输入你的邮箱");
                String email=scanner.next();
                System.out.println("请输入你的生日");
                String birthday=scanner.next();
                Emp emp=new Emp();
              emp.setSname(name);
              emp.setPasswordnum(num);
              emp.setEmail(email);
              emp.setPermission("普通用户");
                String s=birthday;
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                Date date=simpleDateFormat.parse(s);
                emp.setBirthday(date);
              List<Emp>list=getAllEmpBySname();
              List<String>namelist=new ArrayList<>();
             for (Emp emp2:list)
             {
                 namelist.add(emp2.getSname());
             }

                 if (namelist.contains(name))
                 {
                     System.out.println("注册失败,已有用户名");
                 }
                 else
                 {
                     AddEmp(emp);
                     System.out.println("注册成功");
                 }
            }
            if (command==1)
            {
                System.out.println("请输入你的用户名");
                String name=scanner.next();
                System.out.println("请输入你的密码");
                int password=scanner.nextInt();
                List<Emp>list=getAllEmpBySname();
                for (Emp emp:list)
                {
                    if (emp.getSname().equals(name)&&emp.getPasswordnum()==password)
                    {
                        System.out.println("登录成功。。。");
                        System.out.println("=============================");
                        System.out.println("欢迎登录主窗体");
                        System.out.println(name+"您好:"+"\t"+"您的权限是:"+emp.getPermission());
                        System.out.println("=============================");
                        if (emp.getPermission().equals("普通用户"))
                        {
                            while (true)
                            {
                                System.out.println("修改自己的信息------------------1");
                                System.out.println("查询自己的信息------------------2");
                                System.out.println("程序退出------------------------3");
                                int command1=scanner.nextInt();
                                if (command1==3)
                                {
                                    break;
                                }
                                if (command1==1)
                                {
                                    Emp emp1=getEmpBySname(name);
                                    if (emp1!=null)
                                    {
                                        System.out.println("您现在的信息是");
                                        System.out.println(emp1);
                                        System.out.println("=============================");
                                        System.out.println("请输入你要修改的姓名");
                                        String newname=scanner.next();
                                        System.out.println("请输入你要修改的密码");
                                        int newpassword=scanner.nextInt();
                                        System.out.println("请输入要修改的邮箱");
                                        String newemail=scanner.next();
                                        Emp emp2=new Emp();
                                        emp2.setSname(newname);
                                        emp2.setPasswordnum(newpassword);
                                        emp2.setEmail(newemail);
                                        emp2.setPermission(emp.getPermission());
                                        Date date=emp.getBirthday();
                                        emp2.setBirthday(date);
                                        emp2.setSid(emp.getSid());
                                        update(emp2);
                                        System.out.println("修改成功");
                                    }
                                    else
                                    {
                                        System.out.println("不存在");
                                    }
                                }
                                if (command1==2)
                                {
                                    Emp emp1=getEmpBySname(name);
                                    System.out.println("您现在的信息是");
                                    System.out.println(emp1);
                                }
                            }
                        }
                        if (emp.getPermission().equals("管理员"))
                        {
                            while (true)
                            {
                                System.out.println("添加用户-----------------1");
                                System.out.println("删除用户-----------------2");
                                System.out.println("修改用户-----------------3");
                                System.out.println("查询用户-----------------4");
                                System.out.println("程序退出-----------------5");
                                int command2=scanner.nextInt();
                                if (command2==5)
                                {
                                    break;
                                }
                                if (command2==4)
                                {
                                    System.out.println("查询全部用户----------1");
                                    System.out.println("根据ID查询用户--------2");
                                    System.out.println("根据姓名查询用户------3");
                                    System.out.println("请输入要做的操作");
                                    int command3=scanner.nextInt();
                                    if (command3==2)
                                    {
                                        System.out.println("请输入要查询的ID");
                                        int id=scanner.nextInt();
                                        Emp emp3=getEmpById(id);
                                        System.out.println(emp3);
                                    }
                                    if (command3==3)
                                    {
                                        System.out.println("请输入要查询的用户名(支持模糊查询)");
                                        String names=scanner.next();
                                        Emp emp4=getEmpBySname(names);
                                        System.out.println(emp4);
                                    }
                                    if (command3==1)
                                    {
                                        List<Emp>list1=getAllEmpBySname();
                                        for (Emp emp1:list1)
                                        {
                                            System.out.println(emp1);
                                        }
                                    }
                                }
                                if (command2==2)
                                {
                                    System.out.println("请输入要删除的用户的ID号码");
                                    int num=scanner.nextInt();
                                    delectEmpById(num);

                                    System.out.println("删除用户成功");
                                }
                                if (command2==1)
                                {
                                    System.out.println("请输入你的用户名");
                                    String name3=scanner.next();
                                    System.out.println("请输入你的密码");
                                    int num=scanner.nextInt();
                                    System.out.println("请输入你的邮箱");
                                    String email=scanner.next();
                                    System.out.println("请输入你的生日");
                                    String birthday=scanner.next();
                                    Emp emp1=new Emp();
                                    emp1.setSname(name3);
                                    emp1.setPasswordnum(num);
                                    emp1.setEmail(email);
                                    emp1.setPermission("普通用户");
                                    String s=birthday;
                                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
                                    Date date=simpleDateFormat.parse(s);
                                    emp1.setBirthday(date);
                                    AddEmp(emp1);
                                    System.out.println("添加用户成功");
                                }
                                if (command2==3)
                                {
                                    System.out.println("请输入要修改的用户的ID号码");
                                    int id=scanner.nextInt();
                                    System.out.println("请输入你要修改的姓名");
                                    String newname=scanner.next();
                                    System.out.println("请输入你要修改的密码");
                                    int newpassword=scanner.nextInt();
                                    System.out.println("请输入要修改的邮箱");
                                    String newemail=scanner.next();
                                    System.out.println("请输入要修改的用户的权限(管路员/普通用户)");
                                    String word=scanner.next();
                                    Emp emp1=new Emp();
                                    emp1.setSid(id);
                                    emp1.setSname(newname);
                                    emp1.setPasswordnum(newpassword);
                                    emp1.setEmail(newemail);
                                    Emp emp2=getEmpById(id);
                                    emp1.setBirthday(emp2.getBirthday());
                                    emp1.setPermission(word);
                                    update(emp1);
                                    System.out.println("修改成功");
                                }
                            }
                        }
                    }
                }
            }
        }
   }
}

第二个副类

package wode;

import java.util.Date;

/**
 * Created by ttc on 18-1-23.
 */
public class Emp {
    private int sid;
    private String sname;
    private int passwordnum ;
    private String email;
    private String permission;
    private Date birthday;

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public int getPasswordnum() {
        return passwordnum;
    }

    public void setPasswordnum(int passwordnum) {
        this.passwordnum = passwordnum;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", passwordnum=" + passwordnum +
                ", email='" + email + '\'' +
                ", permission='" + permission + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

第三个类

package wode;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * Created by ttc on 18-1-23.
 */
public class JDBCUtils {
    private static Connection conn=null;
    private static String url;
    private static String username;
    private static String password;
    static{
        InputStream in =JDBCUtils .class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        url=properties.getProperty("url");
        username=properties.getProperty("username");
        password=properties.getProperty("password");
        try {
            conn= DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    private JDBCUtils(){}

    public static Connection getConn()
    {
        try {
            conn = DriverManager.getConnection(url,username,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }
    public static void close(Statement statement, Connection connection)
    {
        if(statement != null)
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection)
    {
        if(resultSet != null)
        {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(statement != null)
        {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(connection != null)
        {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,576评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,515评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,017评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,626评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,625评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,255评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,825评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,729评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,271评论 1 320
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,363评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,498评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,183评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,867评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,338评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,458评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,906评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,507评论 2 359

推荐阅读更多精彩内容