“`
package com.lq.emp.main;
import com.lq.emp.service.EmpService;
//程序入口
public class AppMain {
public static void main(String[] args) {
while(true) {
new EmpService().start();
}
}
}
package com.lq.emp.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Scanner;
import com.lq.emp.dao.EmpDAO;
import com.lq.emp.domain.emp;
//提供系统所需功能。
public class EmpService {
private Scanner sc=new Scanner(System.in);
private EmpDAO edao=new EmpDAO();
//add()方法实现添加员工信息的功能
private void add() {
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("入职日期:");
String hiredate=sc.next();
System.out.println("工资:");
int sal=sc.nextInt();
System.out.println("奖金:");
int comm=sc.nextInt();
System.out.println("部门编号:");
int deptno=sc.nextInt();
emp e=new emp();
e.setComm(comm);
e.setDeptno(deptno);
e.setEmpno(empno);
e.setEname(ename);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
try {
e.setHiredate(sdf.parse(hiredate));
} catch (ParseException e1) {
e1.printStackTrace();
}
e.setJob(job);
e.setMgr(mgr);
e.setSal(sal);
//调用dao完成数据库操作
boolean b=edao.add(e);
//反馈操作结果给用户
if(b) {
System.out.println("添加员工成功!!");
}else {
System.out.println("添加员工失败!!");
}
}
![原数据库数据](https://img-blog.csdn.net/20180417213039236?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2QxNTAzNjI0MDIwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![往数据库添加数据](https://img-blog.csdn.net/20180417213333469?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2QxNTAzNjI0MDIwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![添加后的数据库](https://img-blog.csdn.net/20180417213428613?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2QxNTAzNjI0MDIwMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
//实现通过员工编号来修改员工信息
private void alter() {
int b1=0;
System.out.println("请输入你想要修改员工的编号:");
int empno=sc.nextInt();
System.out.println("请输入你想修改的内容:");
String x=sc.next();
//在数据库的表中,有的列的值是数字,有的列的值是字符串,所以通过判断所要修改的列的值的类型,提供不同修改的方法。
if("ename" .equals(x) || "job".equals(x)||"hiredate".equals(x)) {
System.out.println("请输入修改后的值:");
String y=sc.next();
b1= new EmpDAO().alter(empno,x,y);
}else if("empno".equals(x)||"mgr".equals(x)||"sal".equals(x)||"comm".equals(x)||"deptno".equals(x)) {
System.out.println("请输入修改后的值:");
int y=sc.nextInt();
b1= new EmpDAO().alter(empno,x,y);
}else {
System.out.println("所选内容不存在!!");
}
if(b1==1) {
System.out.println("修改成功!!");
}else {
System.out.println("修改失败!!");
}
}
//实现删除员工信息的功能。
private void delete() {
System.out.println(“请输入你想要删除员工的编号:”);
int empno =sc.nextInt();
boolean b= new EmpDAO().delete(empno);
if(b) {
System.out.println(“删除员工成功!!”);
}else {
System.out.println(“删除员工失败!!”);
}
}
//该方法用来调用菜单和选择操作的方法。
public void start() {
menu();
int op=sc.nextInt();
doOp(op);
}
//该方法是通过获取用户所需使用功能的编号来实现对应的功能
private void doOp(int op) {
switch (op) {
case 1:
add();
break;
case 2:
alter();
break;
case 3:
delete();
break;
case 4:
System.out.println("请输入所要查询员工的编号:");
int empno=sc.nextInt();
List l1=new EmpDAO().querry(empno);
if (l1.isEmpty()) {
System.out.println("所查询员工不存在!!!");
}else {
for (emp emp : l1) {
System.out.println("编号:"+emp.getEmpno()+"。姓名:"+emp.getEname()+"。工作:"+emp.getJob()+"。经理编号:"+emp.getMgr()+"。入职日期:"+emp.getHiredate()+"。工资:"
+emp.getSal()+"。部门编号:"+emp.getDeptno());
}
}
break ;
case 5:
List l=new EmpDAO().queryALL();
for (emp emp : l) {
System.out.println("编号:"+emp.getEmpno()+"。姓名:"+emp.getEname()+"。工作:"+emp.getJob()+"。经理编号:"+emp.getMgr()+"。入职日期:"+emp.getHiredate()+"。工资:"
+emp.getSal()+"。部门编号:"+emp.getDeptno());
}
break;
case 6:
System.out.println("退出成功!!!");
System.exit(0);
default:
System.out.println("系统没有提供此功能!!!");
break;
}
}
//打印菜单
private void menu() {
System.out.println(“**************************************”);
System.out.println(“\t\t\t欢迎使用本系统!!!”);
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.退出系统”);
}
}
package com.lq.emp.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.lq.emp.domain.emp;
import com.lq.emp.until.DBUtil;
//专门操作数据库的对象。
public final class EmpDAO {
private Connection conn;
private PreparedStatement pst;
private ResultSet rs;
//实现增加功能的具体代码
public boolean add(emp e) {
try {
conn=DBUtil.opeanConn();
pst=conn.prepareStatement(“insert into myemp values(?,?,?,?,?,?,?,?)”);
pst.setInt(1, e.getEmpno());
pst.setString(2, e.getEname());
pst.setString(3, e.getJob());
pst.setInt(4, e.getMgr());
pst.setDate(5, new Date(e.getHiredate().getTime()));//emp(util).getTime()—>long
pst.setInt(6, e.getSal());
pst.setInt(7, e.getComm());
pst.setInt(8, e.getDeptno());
pst.executeUpdate();
return true;
} catch (SQLException e1) {
e1.printStackTrace();
}finally {
DBUtil.close(pst, conn);
}
return false;
}
//通过判断不同的类型而调用不同的方法。
public int alter(int empno,String x,String y) {
int a=0;
try {
conn=DBUtil.opeanConn();
Statement stm=conn.createStatement();
a=stm.executeUpdate("Update myemp set "+x+"="+"'"+y+"'"+" where "+"empno="+empno);
return a;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn);
}
return a;
}
public int alter(int empno,String x,int y) {
int a=0;
try {
conn=DBUtil.opeanConn();
Statement stm=conn.createStatement();
a=stm.executeUpdate("Update myemp set "+x+"="+y+" where empno="+empno);
return a;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn);
}
return a;
}
//通过员工编号来删除员工信息。
public boolean delete(int empno) {
try {
int x=0;
conn=DBUtil.opeanConn();
Statement stm=conn.createStatement();
x=stm.executeUpdate("delete myemp where empno="+empno);
if (x==1) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn);
}
return false;
}
//通过员工编号来查询一个信息,并将结果以list 的形式返回。
public List querry(int empno) {
List elist1 =new ArrayList<>();
try {
conn=DBUtil.opeanConn();
pst=conn.prepareStatement("select * from myemp where empno="+empno);
rs=pst.executeQuery();
while(rs.next()) {
emp e=new emp();
e.setComm(rs.getInt("comm"));
e.setDeptno(rs.getInt("deptno"));
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setHiredate(rs.getDate("hiredate"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setSal(rs.getInt("sal"));
elist1.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(rs, pst, conn);
}
return elist1;
}
//查询表中所有内容
public List queryALL(){
List elist=new ArrayList<>();
try {
conn=DBUtil.opeanConn();
pst=conn.prepareStatement("select * from myemp");
rs=pst.executeQuery();
while(rs.next()) {
emp e=new emp();
e.setComm(rs.getInt("comm"));
e.setDeptno(rs.getInt("deptno"));
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setHiredate(rs.getDate("hiredate"));
e.setJob(rs.getString("job"));
e.setMgr(rs.getInt("mgr"));
e.setSal(rs.getInt("sal"));
elist.add(e);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(rs, pst, conn);
}
return elist;
}
}
“`package com.lq.emp.domain;
import java.util.Date;
public class emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
}
package com.lq.emp.until;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//工具类:实现连接数据库的功能
public class DBUtil {
static {
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection opeanConn() throws SQLException {
return DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”9867”);
}
public static void close(ResultSet rs,PreparedStatement pst,Connection conn) {
if(rs!=null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(pst!=null) {
try {
pst.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void close(PreparedStatement pst,Connection conn) {
close(null, pst, conn);
}
public static void close(Connection conn) {
close(null, null, conn);
}
}