• 初学Java8:通过JDBC实现简易人力资源管理系统


    1,人力资源管理系统,实现查询、添加、修改、删除的功能。同时设计登录界面,实现注册、登录,修改密码的功能。通过登录进入人力资源管理系统。

    2,能实现将输入的数据保存到数据库,并实现对数据的查询,修改,删除。

    3,在数据库设计两张表,分别是员工表t_staff(列包括id、姓名、性别、年龄),用户表t_user(列包括id、用户名、密码),

    3,异常处理,未实现数据有效性验证。(上次已实现,这次主要练习目的是学习JDBC,故未实现)

    4,MySQL数据库,客户端:SQLyog

     1 CREATE DATABASE HRMS_db DEFAULT CHARSET utf8;
     2 CREATE TABLE t_staff(
     3     id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
     4     NAME VARCHAR(5) NOT NULL,
     5     sex VARCHAR(1) NOT NULL,
     6     age INT NOT NULL    
     7 );
     8 
     9 SELECT * FROM t_staff;
    10 
    11 
    12 CREATE TABLE t_user(
    13     id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    14     NAME VARCHAR(10)  NOT NULL,
    15     pswd VARCHAR(10)  NOT NULL
    16 );
    17 
    18 SELECT * FROM t_user;

    5,数据库工具类-用于人力资源管理系统,此次是把数据存入数据库,将对数据库的操作定义成方法,可以简化之后的程序,提高效率。

      1 package hrms;
      2 
      3 import java.sql.*;
      4 
      5 /**
      6  * 数据库工具类-用于人力资源管理系统,把数据存入数据库
      7  * 
      8  * @author A_zhi 2016-9-8
      9  *
     10  */
     11 public class DBUtil {
     12     /**
     13      * 定义final变量
     14      */
     15     public static final String DRIVER = "com.mysql.jdbc.Driver";//驱动
     16     public static final String URL = "jdbc:mysql://localhost:3306/hrms_db?characterEncoding=utf-8";//地址
     17     public static final String USER = "root";//客户端SQLyog用户名为“root”
     18     public static final String PASSWORD = "";//客户端SQLyog无密码
     19     private static Connection con = null;
     20 
     21     /**
     22      * 建立Java-MySQL间的连接
     23      * 
     24      * @return con
     25      * @throws Exception
     26      */
     27     public static Connection getConnection() throws Exception {
     28         Class.forName(DRIVER);//加载Java-MySQL驱动
     29         con = DriverManager.getConnection(URL, USER, PASSWORD);//建立连接
     30         return con;//返回连接
     31     }
     32 
     33     /**
     34      * 关闭 Connection
     35      * 
     36      * @throws Exception
     37      */
     38     public static void closeConnection() throws Exception {
     39         if (con != null && !con.isClosed()) {
     40             con.close();//关闭连接
     41             con = null;
     42         }
     43     }
     44 
     45     /**
     46      * 执行普通SQL命令
     47      * 
     48      * @return 要执行的SQL语句
     49      * @throws Exception
     50      */
     51     public static int executeUpdate(String sql) throws Exception {
     52         con = getConnection();// 连接数据库
     53         Statement st = con.createStatement();// 创建SQL命令
     54         int r = st.executeUpdate(sql);// 执行SQL命令
     55         closeConnection();// 关闭
     56         return r;// 返回
     57     }
     58 
     59     /**
     60      * 执行预编译SQL命令,可执行带"?"参数的SQL语句(增加,删除,修改)
     61      * 
     62      * @param sql
     63      *            要执行的SQL命令
     64      * @param obj
     65      *            未知数目和类型的变量
     66      * @return 要执行的SQL语句
     67      * @throws Exception
     68      */
     69     public static int executeUpdate(String sql, Object... obj) throws Exception {
     70         con = getConnection();
     71         PreparedStatement pst = con.prepareStatement(sql);// 预编译SQL命令
     72         if (obj != null && obj.length > 0) {
     73             for (int i = 0; i < obj.length; i++) {
     74                 pst.setObject(i + 1, obj[i]);// 数据库从1开始,
     75             }
     76         }
     77         int r = pst.executeUpdate();
     78         closeConnection();
     79         return r;
     80     }
     81 
     82     /**
     83      * 账号验证  注册,登陆使用,输入用户名和密码存在返回true,否则返回false
     84      * @param name 用户名 pswd  密码
     85      * @return Boolean
     86      * @throws Exception
     87      */
     88     public static boolean queryLogin(String name,String pswd) throws Exception {
     89         String sql = "select name  from t_user where name=? and pswd=?";
     90         ResultSet rs = executeQuery(sql, name,pswd);
     91         if (rs.next())
     92             return true;
     93         else
     94             return false;
     95     }
     96 
     97     /**
     98      * 执行SQL查询命令
     99      * 
    100      * @param sql
    101      *            要执行的查询SQL命令
    102      * @param obj
    103      *            未知数目和类型的变量
    104      * @return 结果集ResultSet
    105      * @throws Exception
    106      */
    107     public static ResultSet executeQuery(String sql, Object... obj) throws Exception {
    108         con = getConnection();
    109         PreparedStatement pst = con.prepareStatement(sql);// 还是预编译
    110         if (obj != null && obj.length > 0) {
    111             for (int i = 0; i < obj.length; i++) {
    112                 pst.setObject(i + 1, obj[i]);
    113             }
    114         }
    115         ResultSet rs = pst.executeQuery();
    116         return rs;
    117     }
    118 }
    119 // 对于封装的查询方法,不能在方法中关闭Connection,否则无法在进行查询
    120 // 事先在方法外部定义Connection就是为了查询这个方法,其他方法中Connection可以定义在方法内
    121 // 采用脱离连接的行集可以实现关闭connection也能查询,以下附其关键语法
    122 // import javax.sql.rowset.CachedRowSet;
    123 // import com.sun.rowset.CachedRowSetImpl;
    124 // Connection con=DBUtil.getConnection();
    125 // PreparedStatement pst=con.prepareStatement(sql);127 // ResultSet rs=pst.executeQuery();
    128 // CachedRowSet crs=new CachedRowSetImpl();//创建行集
    129 // crs.populate(rs);//将结果集保存到行集
    130 // con.close();//关闭
    131 // crs.last();//再查询
    132 // System.out.println(crs.getString("name"));

    6,人力资源管理系统,把数据存入数据库,运用封装,此类为系统的主界面以及操作方法,只有登录之后才能进入。

      1 package hrms;
      2 
      3 import java.util.Scanner;
      4 import java.sql.*;
      5 
      6 /**
      7  * 人力资源管理系统,把数据存入数据库(运用封装)
      8  * 
      9  * 此类提供主界面及其数据操作
     10  * 
     11  * @author A_zhi
     12  * 
     13  *         2016-9-8
     14  * 
     15  *         数据库信息:
     16  * 
     17  *         CREATE DATABASE HRMS_db DEFAULT CHARSET utf8; CREATE TABLE t_staff(
     18  *         id INT PRIMARY KEY NOT NULL AUTO_INCREMENT , NAME VARCHAR(5) NOT
     19  *         NULL, sex VARCHAR(1) NOT NULL, age INT NOT NULL );SELECT * FROM
     20  *         t_staff;
     21  *
     22  */
     23 public class HrmsByJdbc {
     24     public static Scanner sc = new Scanner(System.in);
     25 
     26     /**
     27      * 主界面,来自之前的复制
     28      */
     29     public static void mainInterface() {
     30         while (true) {
     31             System.out.println("
    
    ");
     32             System.out.println("**********************************************");
     33             System.out.println("*            人力资源管理系统                 *");
     34             System.out.println("**********************************************");
     35             System.out.println("*             1、查看员工信息                 *");
     36             System.out.println("*             2、添加员工信息                 *");
     37             System.out.println("*             3、修改员工信息                 *");
     38             System.out.println("*             4、删除员工信息                 *");
     39             System.out.println("*             0、退出系统                     *");
     40             System.out.println("**********************************************");
     41             System.out.print("请选择:");
     42             int num = sc.nextInt();
     43             if (num == 0) {
     44                 System.out.println("
      Thanks For Your Use!");
     45                 System.exit(0);//退出系统,之前用的是break,但是它却返回到了登录界面,所以百度得到这个表达式
     46             } else {
     47                 switch (num) {
     48                 case 1:
     49                     query();// 查询
     50                     break;
     51                 case 2:
     52                     add();// 添加
     53                     break;
     54                 case 3:
     55                     update();// 修改
     56                     break;
     57                 case 4:
     58                     del();// 删除
     59                     break;
     60                 default:
     61                     System.out.println("没有这个选项,请重新输入...");
     62                 }
     63             }
     64         }
     65     }
     66 
     67     // 1、查询
     68     private static void query() {
     69         System.out.print("您要查询全部信息还是单个员工信息?
      a、全部,b、单个 :");
     70         String num1 = sc.next();
     71         String sql = null;
     72         try {
     73             switch (num1) {
     74             case "a":
     75                 sql = "select * from t_staff";
     76                 ResultSet rsa = DBUtil.executeQuery(sql);//调用工具类的方法
     77                 System.out.println("编号	姓名	性别	年龄");
     78                 while (rsa.next()) {
     79                     int id = rsa.getInt(1);
     80                     String name = rsa.getString(2);
     81                     String sex = rsa.getString(3);
     82                     int age = rsa.getInt(4);
     83                     System.out.println(id + "	" + name + "	" + sex + "	" + age);
     84                 }
     85                 break;
     86             case "b":
     87                 System.out.print("请输入您要查询的员工id:");
     88                 int idnum = sc.nextInt();
     89                 sql = "select * from t_staff where id=?";
     90                 ResultSet rsb = DBUtil.executeQuery(sql, idnum);
     91                 System.out.println("编号	姓名	性别	年龄");
     92                 while (rsb.next()) {
     93                     int id = rsb.getInt(1);
     94                     String name = rsb.getString(2);
     95                     String sex = rsb.getString(3);
     96                     int age = rsb.getInt(4);
     97                     System.out.println(id + "	" + name + "	" + sex + "	" + age);
     98                 }
     99                 break;
    100             default:
    101                 System.out.println("没有这个选项,请重新输入...");
    102                 break;
    103             }
    104 
    105         } catch (SQLException e) {
    106             System.out.println("数据库错误:" + e.getMessage());
    107             e.printStackTrace();
    108         } catch (Exception e) {
    109             System.out.println("其它错误" + e.getMessage());
    110             e.printStackTrace();
    111         } finally {
    112             try {
    113                 DBUtil.closeConnection();
    114             } catch (Exception e) {
    115                 System.out.println(e.getMessage());
    116             }
    117         }
    118     }
    119 
    120     // 2、添加
    121     private static void add() {
    122         System.out.println("	数据录入");
    123         System.out.print("姓名:");
    124         String name = sc.next();
    125         System.out.print("性别:");
    126         String sex = sc.next();
    127         System.out.print("年龄:");
    128         int age = sc.nextInt();
    129         String sql = "INSERT INTO t_staff(NAME,sex,age) VALUES(?,?,?)";
    130         try {
    131             DBUtil.executeUpdate(sql, name, sex, age);
    132             System.out.println("添加成功");
    133         } catch (Exception e) {
    134             System.out.println("错误:" + e.getMessage());
    135         } finally {
    136             try {
    137                 DBUtil.closeConnection();
    138             } catch (Exception e) {
    139                 e.printStackTrace();
    140             }
    141         }
    142     }
    143 
    144     // 3、修改
    145     private static void update() {
    146         String s1 = "select * from t_staff where id=?";
    147         String s2 = "update  t_staff set name=? where id=?";
    148         String s3 = "update  t_staff set sex=?  where id=?";
    149         String s4 = "update  t_staff set age=?  where id=?";
    150         System.out.print("请输入您要修改员工的id:");
    151         int idnum3 = sc.nextInt();
    152         try {
    153             ResultSet rsb = DBUtil.executeQuery(s1, idnum3);
    154             System.out.println("编号	姓名	性别	年龄");
    155             while (rsb.next()) {
    156                 int id = rsb.getInt(1);
    157                 String name = rsb.getString(2);
    158                 String sex = rsb.getString(3);
    159                 int age = rsb.getInt(4);
    160                 System.out.println(id + "	" + name + "	" + sex + "	" + age);
    161             }
    162             System.out.print("你是需要修改此人信息吗? y/n: ");
    163             String as = sc.next();
    164             if ("y".equals(as)) {
    165                 System.out.print("你要修改的是:a、姓名,b、性别,c、年龄 :");
    166                 String as1 = sc.next();
    167                 if ("a".equals(as1)) {
    168                     System.out.print("请输入姓名:");
    169                     String inname = sc.next();
    170                     DBUtil.executeUpdate(s2, inname, idnum3);
    171                 } else if ("b".equals(as1)) {
    172                     System.out.print("请输入性别:");
    173                     String sex = sc.next();
    174                     DBUtil.executeUpdate(s3, sex, idnum3);
    175                 } else if ("c".equals(as1)) {
    176                     System.out.print("请输入年龄:");
    177                     int age = sc.nextInt();
    178                     DBUtil.executeUpdate(s4, age, idnum3);
    179                 } else {
    180                     System.out.println("输入错误,请重新输入...");
    181                 }
    182             }
    183             System.out.println("修改成功!");
    184         } catch (Exception e) {
    185             e.printStackTrace();
    186         } finally {
    187             try {
    188                 DBUtil.closeConnection();
    189             } catch (Exception e) {
    190                 e.printStackTrace();
    191             }
    192         }
    193 
    194     }
    195 
    196     // 4、删除
    197     private static void del() {
    198         String s1 = "select * from t_staff where id=?";
    199         String s2 = "delete from t_staff where id=?";
    200         System.out.print("请输入您要删除员工的id:");
    201         int idnum4 = sc.nextInt();
    202         ResultSet rs4 = null;
    203         try {
    204             rs4 = DBUtil.executeQuery(s1, idnum4);
    205             System.out.println("编号	姓名	性别	年龄");
    206             while (rs4.next()) {
    207                 int id = rs4.getInt(1);
    208                 String name = rs4.getString(2);
    209                 String sex = rs4.getString(3);
    210                 int age = rs4.getInt(4);
    211                 System.out.println(id + "	" + name + "	" + sex + "	" + age);
    212             }
    213             System.out.print("您确定要删除此人信息吗? y/n:");
    214             String as = sc.next();
    215             if ("y".equals(as)) {
    216                 DBUtil.executeUpdate(s2, idnum4);
    217                 System.out.println("删除成功!");
    218             } else {
    219                 System.out.println("删除取消!");
    220             }
    221         } catch (Exception e) {
    222             e.getMessage();
    223         } finally {
    224             try {
    225                 DBUtil.closeConnection();
    226             } catch (Exception e) {
    227                 e.printStackTrace();
    228             }
    229         }
    230     }
    231 }

    7,登录类,提供账户注册、登录、修改密码的方法

      1 package hrms;
      2 
      3 import java.util.Scanner;
      4 
      5 /**
      6  * 人力资源管理系统 用户操作
      7  * 
      8  * 提供注册,登录,修改密码的功能
      9  * 
     10  * 数据库信息:
     11  * 
     12  * CREATE TABLE t_user( id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME
     13  * VARCHAR(10) NOT NULL, pswd VARCHAR(10) NOT NULL );
     14  * 
     15  * @author A_zhi
     16  * 
     17  *         2016-9-9
     18  */
     19 public class Login {
     20     private static Scanner sc = new Scanner(System.in);
     21 
     22     /**
     23      * 注册
     24      * 
     25      * @throws Exception
     26      */
     27     public static void register() throws Exception {
     28         System.out.println("
    ");
     29         System.out.println("*--------------------------------------------*");
     30         System.out.println("*++++++++++欢迎登录人力资源管理系统+++++++++++*");
     31         System.out.println("*--------------------------------------------*");
     32         String sql = "insert into t_user(name,pswd) values(?,?)";
     33         System.out.println("
    ");
     34         while (true) {
     35             System.out.print("请输入用户名:");
     36             String inname = sc.next();
     37             System.out.print("  请设置密码:");
     38             String inpswd = sc.next();
     39             boolean b = DBUtil.queryLogin(inname, inpswd);
     40             if (b) {
     41                 System.out.println("
    该用户名已存在,请重新输入...");
     42             } else {
     43                 DBUtil.executeUpdate(sql, inname, inpswd);
     44                 System.out.print("
    注册成功!欢迎登录!是否立即登陆?y/n :");
     45                 String as = sc.next();
     46                 if ("y".equals(as)) {
     47                     login();
     48                 }
     49                 break;
     50             }
     51         }
     52     }
     53 
     54     /**
     55      * 登录
     56      */
     57     public static void login() throws Exception {
     58         System.out.println("
    ");
     59         int count = 0;
     60         System.out.println("*--------------------------------------------*");
     61         System.out.println("*++++++++++欢迎登录人力资源管理系统+++++++++++*");
     62         System.out.println("*--------------------------------------------*");
     63         while (true) {
     64             System.out.println();
     65             System.out.print("请输入用户名:");
     66             String inname = sc.next();
     67             System.out.print("  请输入密码:");
     68             String inpswd = sc.next();
     69             boolean b = DBUtil.queryLogin(inname, inpswd);
     70             if (b) {
     71                 System.out.println("即将进入...");
     72                 HrmsByJdbc.mainInterface();
     73             } else {
     74                 count++;
     75                 System.out.println("账号与密码不匹配,请重新输入...
    ");
     76             }
     77             if (count == 3) {
     78                 System.out.println("您连续三次输入错误,已退出!");
     79                 break;
     80             }
     81         }
     82     }
     83 
     84     /**
     85      * 修改密码
     86      * 
     87      * @throws Exception
     88      */
     89     public static void updatePswd() throws Exception {
     90         System.out.println();
     91         System.out.print("请登录后修改密码");
     92         System.out.println("
    ");
     93         int count = 0;
     95         System.out.println("*--------------------------------------------*");
     96         System.out.println("*++++++++++欢迎登录人力资源管理系统+++++++++++*");
     97         System.out.println("*--------------------------------------------*");
     98         while (true) {
     99             System.out.println();
    100             System.out.print("请输入用户名:");
    101             String inname = sc.next();
    102             System.out.print("  请输入密码:");
    103             String inpswd = sc.next();
    104             boolean b = DBUtil.queryLogin(inname, inpswd);
    105             if (b) {
    106                 System.out.println("
    -----修改密码------
    ");
    107                 System.out.print("  请输入新的密码:");
    108                 String newpswd=sc.next();
    109                 String sql="update t_user set pswd=? where name=?";
    110                 DBUtil.executeUpdate(sql,newpswd,inname);
    111                 System.out.println("
    修改成功!请重新登录...");
    112                 login();
    113             } else {
    114                 count++;
    115                 System.out.println("账号与密码不匹配,请重新输入...
    ");
    116             }
    117             if (count == 3) {
    118                 System.out.println("您连续三次输入错误,已退出!");
    119                 break;
    120             }
    121         }
    122     }
    123 }

    8,注册登录的主界面,提供主方法,是程序执行的入口

     1 package hrms;
     2 
     3 import java.util.Scanner;
     4 
     5 /**
     6  * 人力资源管理系统
     7  * 
     8  * 注册登录界面,程序执行入口
     9  * 
    10  * @author A_zhi
    11  *
    12  *         2016-9-11
    13  */
    14 public class TextLogin {
    15 
    16     public static void main(String[] args) throws Exception {
    17         Scanner sc = new Scanner(System.in);
    18         while (true) {
    19             System.out.println("
    ");
    20             System.out.println("*--------------------------------------------*");
    21             System.out.println("*++++++++++欢迎登录人力资源管理系统+++++++++++*");
    22             System.out.println("*--------------------------------------------*");
    23             System.out.println("*++++++++++++++++1,注册++++++++++++++++++++++*");
    24             System.out.println();
    25             System.out.println("*++++++++++++++++2,登陆++++++++++++++++++++++*");
    26             System.out.println();
    27             System.out.println("*++++++++++++++++3,修改密码++++++++++++++++++*");
    28             System.out.println();
    29             System.out.println("*++++++++++++++++0,退出++++++++++++++++++++++*");
    30             System.out.print("请选择:");
    31             int num = sc.nextInt();
    32             if (num == 0) {
    33                 System.out.println("
      Thanks For Your Use!");
    34                 break;
    35             } else {
    36                 switch (num) {
    37                 case 1:
    38                     Login.register();
    39                     break;
    40                 case 2:
    41                     Login.login();
    42                     break;
    43                 case 3:
    44                     Login.updatePswd();;
    45                     break;
    46                 default:
    47                     System.out.println("没有这个选项,请重新输入...");
    48                 }
    49             }
    50         }
    51         sc.close();
    52     }
    53 }

      至今学习Java已有一月有余,去上课前两天,我在家自己看看视频,当时在DOS输出了一个hello  world我都很高兴,一个多月过去了,程序从几行变成了上百行,这段时间很辛苦,很累,但是收获还是有的,虽然我不知道学了一个多月,能到现在的水平是够了还是不够,但是我问心无愧,我努力了。Java知识太多了,这段时间每天讲的都很多,有的甚至来不及消化,就又开始了新的内容,每天早上7点起床去学Java,下午7点回来,吃了饭再看看,还是感觉时间不够。接下来的时间还需要更加努力。

      那天晚上在一个Java学习群,有个自学了几天的朋友发了一个for循环的语句,但是是错的,他说他看着视频就能写,不看视频就出错,就是记不住,我回应道,没有什么代码是你去写十遍记不住的,如果有那就再写十遍。我以后也会尽量再多写对练的。

      加油!坚持!

                                                              A_zhi

                                                              2016-9-11

  • 相关阅读:
    变态的IE
    视频豪横时代,应用如何快速构建视频点播能力?
    阿里云峰会 | 阿里云CDN六大边缘安全能力,全力助推政企数字化转型
    从 2018 年 Nacos 开源说起
    完美日记:实现高弹性高稳定电商架构
    Dubbo 迈出云原生重要一步 应用级服务发现解析
    如何提升微服务的幸福感
    怀里橘猫柴犬,掌上代码江湖——对话阿里云 MVP郭旭东
    云原生时代消息中间件的演进路线
    solr中特殊字符的处理
  • 原文地址:https://www.cnblogs.com/a-zhi/p/5861574.html
Copyright © 2020-2023  润新知