parent
6414d1ef1f
commit
5634c56f9a
@ -0,0 +1,71 @@
|
||||
package com.msb.dao;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class BaseDao {
|
||||
public List baseQuery(Connection connection, Class clazz, String sql, Object ... args) {
|
||||
List list = new ArrayList();
|
||||
PreparedStatement statement = null;
|
||||
try {
|
||||
statement = connection.prepareStatement(sql);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
statement.setObject(i + 1, args[i]);
|
||||
}
|
||||
ResultSet rs = statement.executeQuery();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
field.setAccessible(true);
|
||||
}
|
||||
while (rs.next()) {
|
||||
Object obj = clazz.getDeclaredConstructor().newInstance();
|
||||
for (Field field : fields) {
|
||||
Object data = rs.getObject(field.getName());
|
||||
field.set(obj, data);
|
||||
}
|
||||
list.add(obj);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
statement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public int baseUpdate(Connection connection, String sql,Object ... args){
|
||||
PreparedStatement preparedStatement=null;
|
||||
int rows=0;
|
||||
|
||||
try{
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
//设置参数
|
||||
for (int i = 0; i <args.length ; i++) {
|
||||
preparedStatement.setObject(i+1, args[i]);
|
||||
}
|
||||
|
||||
//执行CURD
|
||||
rows =preparedStatement.executeUpdate();// 这里不需要再传入SQL语句
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if(null != preparedStatement){
|
||||
try {
|
||||
preparedStatement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
}
|
@ -1,7 +1,99 @@
|
||||
package com.msb.view;
|
||||
|
||||
import com.msb.bean.Dept;
|
||||
import com.msb.bean.Emp;
|
||||
import com.msb.dao.DeptDao;
|
||||
import com.msb.dao.EmpDao;
|
||||
import com.msb.dao.imp.DeptDaoImp;
|
||||
import com.msb.dao.imp.EmpDaoImp;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EmpManageSystem {
|
||||
private static Scanner sc =new Scanner(System.in);
|
||||
private static EmpDao empDao =new EmpDaoImp();
|
||||
private static DeptDao deptDao=new DeptDaoImp();
|
||||
private static SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
public static void main(String[] args) {
|
||||
while (true) {
|
||||
showMenu();
|
||||
System.out.println("请录入选项");
|
||||
int option =sc.nextInt();
|
||||
switch (option) {
|
||||
case 1:
|
||||
case1();
|
||||
break;
|
||||
case 2:
|
||||
case2();
|
||||
break;
|
||||
case 3:
|
||||
case3();
|
||||
break;
|
||||
case 5:
|
||||
case5();
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void showMenu(){
|
||||
System.out.println("************************************");
|
||||
System.out.println("* 1 查看所有员工信息");
|
||||
System.out.println("* 2 查看所有部门信息");
|
||||
System.out.println("* 3 根据工号删除员工信息");
|
||||
System.out.println("* 4 根据工号修改员工信息");
|
||||
System.out.println("* 5 增加员工信息");
|
||||
System.out.println("* 6 增加部门信息");
|
||||
System.out.println("* 7 退出");
|
||||
System.out.println("************************************");
|
||||
}
|
||||
|
||||
private static void case1(){
|
||||
List<Emp> emps = empDao.findAll();
|
||||
emps.forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void case2(){
|
||||
List<Dept> depts = deptDao.findAll();
|
||||
depts.forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void case3(){
|
||||
System.out.println("请输入要删除的员工编号");
|
||||
int empno=sc.nextInt();
|
||||
empDao.deleteByEmpno(empno);
|
||||
}
|
||||
|
||||
private static void case5(){
|
||||
System.out.println("请输入员工编号");
|
||||
int empno =sc.nextInt();
|
||||
System.out.println("请输入员工姓名");
|
||||
String ename =sc.next();
|
||||
System.out.println("请输入员工职位");
|
||||
String job =sc.next();
|
||||
System.out.println("请输入员工上级");
|
||||
int mgr =sc.nextInt();
|
||||
System.out.println("请输入员工入职日期,格式为yyyy-MM-dd");
|
||||
Date hiredate =null;
|
||||
try {
|
||||
hiredate = simpleDateFormat.parse(sc.next());
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("请输入员工工资");
|
||||
double sal =sc.nextDouble();
|
||||
System.out.println("请输入员工补助");
|
||||
double comm=sc.nextDouble();
|
||||
System.out.println("请输入员工部门号");
|
||||
int deptno =sc.nextInt();
|
||||
Emp emp=new Emp(empno, ename, job, mgr, hiredate, sal, comm,deptno);
|
||||
empDao.addEmp(emp);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue