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

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

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

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

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

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

1 CREATE DATABASE HRMS_db DEFAULTCHARSET utf8;2 CREATE TABLEt_staff(3 id INT PRIMARY KEY NOT NULLAUTO_INCREMENT,4 NAME VARCHAR(5) NOT NULL,5 sex VARCHAR(1) NOT NULL,6 age INT NOT NULL

7 );8

9 SELECT * FROMt_staff;10

11

12 CREATE TABLEt_user(13 id INT PRIMARY KEY NOT NULLAUTO_INCREMENT,14 NAME VARCHAR(10) NOT NULL,15 pswd VARCHAR(10) NOT NULL

16 );17

18 SELECT * FROM t_user;

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

1 packagehrms;2

3 import java.sql.*;4

5 /**

6 * 数据库工具类-用于人力资源管理系统,把数据存入数据库7 *8 *@authorA_zhi 2016-9-89 *10 */

11 public classDBUtil {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 *@returncon25 *@throwsException26 */

27 public static Connection getConnection() throwsException {28 Class.forName(DRIVER);//加载Java-MySQL驱动29 con =DriverManager.getConnection(URL, USER, PASSWORD);//建立连接30 returncon;//返回连接31 }32

33 /**

34 * 关闭 Connection35 *36 *@throwsException37 */

38 public static void closeConnection() throwsException {39 if (con != null && !con.isClosed()) {40 con.close();//关闭连接41 con = null;42 }43 }44

45 /**

46 * 执行普通SQL命令47 *48 *@return要执行的SQL语句49 *@throwsException50 */

51 public static int executeUpdate(String sql) throwsException {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 *@paramsql63 * 要执行的SQL命令64 *@paramobj65 * 未知数目和类型的变量66 *@return要执行的SQL语句67 *@throwsException68 */

69 public static int executeUpdate(String sql, Object... obj) throwsException {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 returnr;80 }81

82 /**

83 * 账号验证 注册,登陆使用,输入用户名和密码存在返回true,否则返回false84 *@paramname 用户名 pswd 密码85 *@returnBoolean86 *@throwsException87 */

88 public static boolean queryLogin(String name,String pswd) throwsException {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 *@paramsql101 * 要执行的查询SQL命令102 *@paramobj103 * 未知数目和类型的变量104 *@return结果集ResultSet105 *@throwsException106 */

107 public static ResultSet executeQuery(String sql, Object... obj) throwsException {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 returnrs;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 packagehrms;2

3 importjava.util.Scanner;4 import java.sql.*;5

6 /**

7 * 人力资源管理系统,把数据存入数据库(运用封装)8 *9 * 此类提供主界面及其数据操作10 *11 *@authorA_zhi12 *13 * 2016-9-814 *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) NOT19 * NULL, sex VARCHAR(1) NOT NULL, age INT NOT NULL );SELECT * FROM20 * t_staff;21 *22 */

23 public classHrmsByJdbc {24 public static Scanner sc = newScanner(System.in);25

26 /**

27 * 主界面,来自之前的复制28 */

29 public static voidmainInterface() {30 while (true) {31 System.out.println("\n\n");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("\n 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 voidquery() {69 System.out.print("您要查询全部信息还是单个员工信息?\n 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("编号\t姓名\t性别\t年龄");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 + "\t" + name + "\t" + sex + "\t" +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("编号\t姓名\t性别\t年龄");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 + "\t" + name + "\t" + sex + "\t" +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 voidadd() {122 System.out.println("\t数据录入");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 voidupdate() {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("编号\t姓名\t性别\t年龄");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 + "\t" + name + "\t" + sex + "\t" +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 voiddel() {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("编号\t姓名\t性别\t年龄");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 + "\t" + name + "\t" + sex + "\t" +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 packagehrms;2

3 importjava.util.Scanner;4

5 /**

6 * 人力资源管理系统 用户操作7 *8 * 提供注册,登录,修改密码的功能9 *10 * 数据库信息:11 *12 * CREATE TABLE t_user( id INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME13 * VARCHAR(10) NOT NULL, pswd VARCHAR(10) NOT NULL );14 *15 *@authorA_zhi16 *17 * 2016-9-918 */

19 public classLogin {20 private static Scanner sc = newScanner(System.in);21

22 /**

23 * 注册24 *25 *@throwsException26 */

27 public static void register() throwsException {28 System.out.println("\n");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("\n");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("\n该用户名已存在,请重新输入...");42 } else{43 DBUtil.executeUpdate(sql, inname, inpswd);44 System.out.print("\n注册成功!欢迎登录!是否立即登陆?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() throwsException {58 System.out.println("\n");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("账号与密码不匹配,请重新输入...\n");76 }77 if (count == 3) {78 System.out.println("您连续三次输入错误,已退出!");79 break;80 }81 }82 }83

84 /**

85 * 修改密码86 *87 *@throwsException88 */

89 public static void updatePswd() throwsException {90 System.out.println();91 System.out.print("请登录后修改密码");92 System.out.println("\n");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("\n-----修改密码------\n");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("\n修改成功!请重新登录...");112 login();113 } else{114 count++;115 System.out.println("账号与密码不匹配,请重新输入...\n");116 }117 if (count == 3) {118 System.out.println("您连续三次输入错误,已退出!");119 break;120 }121 }122 }123 }

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

1 packagehrms;2

3 importjava.util.Scanner;4

5 /**

6 * 人力资源管理系统7 *8 * 注册登录界面,程序执行入口9 *10 *@authorA_zhi11 *12 * 2016-9-1113 */

14 public classTextLogin {15

16 public static void main(String[] args) throwsException {17 Scanner sc = newScanner(System.in);18 while (true) {19 System.out.println("\n");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("\n 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值