JAVA JDBC实现的职工管理系统(Console版)(期中java课程设计)

                     

一.说在前面的话

        不知不觉就到了大三的期中了,java也基本算是入了门。这不,老师布置了java的课程设计。(废话......)

涉及的java主要知识点:java基础知识,接口,JDBC。

三个package:ui,dao,vo(自行脑补包名,由于专业的特殊性包名不能随意暴露,您懂得)。分别代表了UI层、数据库操作层(包括链接和断开)、职工信息(也就是Employee类)。

二.上代码

1.职工信息类(package:com.XXX.vo。包含Employee.class)
package com.XXX.vo;

public class Employee {
	private int eId;    //职工id号
	private String eName;//职工姓名
	private String eIdno;//职工身份证号
	private long eTel;   //职工电话
	private String eDept;//职工部门
	private String eJob; //职工职位
	private String eMail;//职工email
	public int getEId() {
		return eId;
	}
	public void setEId(int id) {
		eId = id;
	}
	public String getEName() {
		return eName;
	}
	public void setEName(String name) {
		eName = name;
	}
	public String getEIdno() {
		return eIdno;
	}
	public void setEIdno(String idno) {
		eIdno = idno;
	}
	public long getETel() {
		return eTel;
	}
	public void setETel(int tel) {
		eTel = tel;
	}
	public String getEDept() {
		return eDept;
	}
	public void setEDept(String dept) {
		eDept = dept;
	}
	public String getEJob() {
		return eJob;
	}
	public void setEJob(String job) {
		eJob = job;
	}
	public String getEMail() {
		return eMail;
	}
	public void setEMail(String mail) {
		eMail = mail;
	}
	
	/**
	 * 职工类的构造函数
	 * @param eId
	 * @param eName
	 * @param eIdno
	 * @param eTel
	 * @param eDept
	 * @param eJob
	 * @param eMail
	 */
	public Employee(int eId, String eName, String eIdno, long eTel, String eDept,
			String eJob, String eMail) {
		super();
		this.eId = eId;
		this.eName = eName;
		this.eIdno = eIdno;
		this.eTel = eTel;
		this.eDept = eDept;
		this.eJob = eJob;
		this.eMail = eMail;
	}
	/**
	 * 职工类只有一个参数的构造函数
	 * @param eId
	 */
	public Employee(int eId)//eId的构造函数
	{
		this.eId = eId;
	}
}

3.UI层(package:com.XXX.ui。包含:MainUI.java  EmpUI.java)

MainUI.java

package com.XXX.ui;

import java.util.Scanner;

public class MainUI {
	public void menu() {
		System.out.println("职工基础信息管理(employee-crud)");
		System.out.println("");
		System.out.println("请输入1-5进行操作");
		System.out.println("1.添加新员工");
		System.out.println("2.检索员工信息");
		System.out.println("3.更新员工信息");
		System.out.println("4.删除员工信息");
		System.out.println("5.退出系统");
		System.out.println("请输入您的选择:");
	}
	/*
	 * 入口main函数
	 */
	public static void main(String[] args) {

		while (true) {    
			new MainUI().menu();
			try {               
				Scanner scan = new Scanner(System.in);
				int i;
				i = scan.nextInt();
				switch (i) {
				case 1:
					new EmpUI().insertUi();
					break;
				case 2:
					new EmpUI().selectUi();
					break;
				case 3:
					new EmpUI().updateUi();
					break;
				case 4:
					new EmpUI().deleteUi();
					break;
				case 5:
					System.out.println("本系统已退出");
					System.exit(0);
					break;
				default:
					System.out.println("请输入数字1-5");
					break;
				}
			} catch (java.util.InputMismatchException e) {
				System.out.println("请输入数字");
			}

		}
	}
}


EmpUI.java

package com.XXX.ui;

import java.util.Scanner;
import com.XXX.dao.EmployeeDaoImp;
import com.XXX.vo.Employee;

public class EmpUI {
	/**
	 * 更新员工信息的操作界面
	 */
	public void updateUi() {
		System.out.println("员工信息更新界面");

		// 从键盘输入要更新的员工编号,以及更新后的其他六项信息
		System.out.println("输入要更新的员工编号:");
		Scanner scan = new Scanner(System.in);
		int eid = scan.nextInt();
		boolean bool = new EmployeeDaoImp().check(eid);// 判断是否存在此eid,若存在,则执行更新,否则提示警告
		if (bool) {
			System.out.println("输入要更新的员工姓名:");
			String ename = scan.next();
			System.out.println("输入要更新的员工身份证号:");
			String eidno = scan.next();
			if (!(new EmployeeDaoImp().checkIdno(eidno))) {
				System.out.println("输入要更新的员工电话:");
				long etel = scan.nextLong();
				System.out.println("输入要更新的员工部门:");
				String edept = scan.next();
				System.out.println("输入要更新的员工职位:");
				String ejob = scan.next();
				System.out.println("输入要更新的员工email:");
				String email = scan.next();
				Employee emp = new Employee(eid, ename, eidno, etel, edept, ejob,email);
				boolean bool2 = new EmployeeDaoImp().updateEmp(emp);
				if (bool2) {
					System.out.println("更新成功");
				} else {
					System.out.println("更新失败");
				}
			}else{
				System.out.println("输入身份证号与已知员工省份证号重复!");
			}
			
		} else {
			System.out.println("此员工不存在");
		}
	}

	/**
	 * 删除员工的操作界面
	 */
	public void deleteUi() {
		System.out.println("输入要删除的员工编号:");
		Scanner scan = new Scanner(System.in);
		int eid = scan.nextInt();
		boolean bool = new EmployeeDaoImp().check(eid);
		if (bool) {
			boolean bool2 = new EmployeeDaoImp().deleteEmp(eid);
			if (bool2) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} else {
			System.out.println("此员工不存在");
		}
	}

	/**
	 * 添加员工的界面
	 */
	public void insertUi() {

		System.out.println("输入要插入的员工编号:");
		Scanner scan = new Scanner(System.in);
		int eid = scan.nextInt();
		boolean bool = new EmployeeDaoImp().check(eid);
		if (bool) {
			System.out.println("此员工编号已经存在");
		} else {
			System.out.println("输入要插入的员工姓名:");
			String ename = scan.next();
			System.out.println("输入要插入的员工身份证号:");
			String eidno = scan.next();
			if (!(new EmployeeDaoImp().checkIdno(eidno))){
				System.out.println("输入要插入的员工电话:");
				long etel = scan.nextLong();
				System.out.println("输入要插入的员工部门:");
				String edept = scan.next();
				System.out.println("输入要插入的员工职位:");
				String ejob = scan.next();
				System.out.println("输入要插入的员工email:");
				String email = scan.next();

				Employee emp = new Employee(eid, ename, eidno, etel, edept, ejob,email);

				boolean bool2 = new EmployeeDaoImp().insertEmp(emp);
				if (bool2) {
					System.out.println("插入成功");
				} else {
					System.out.println("插入失败");
				}
			}else{
				System.out.println("插入的身份证号与已知的身份证号重复!");
			}
			
		}
	}

	/**
	 * 检索员工信息的界面
	 */
	public void selectUi() {
		System.out.println("1.显示全部职工信息");
		System.out.println("2.根据id查询职工信息");
		Scanner scan1 = new Scanner(System.in);
		int i = scan1.nextInt();
		switch (i) {
		case 1:
			boolean bool1 =  new EmployeeDaoImp().showAll();
			if (!bool1) {
				System.out.println("显示全部信息失败!");
			}
			break;
		case 2:
			System.out.println("请输入要查询的员工号");
			Scanner scan = new Scanner(System.in);
			int eid = scan.nextInt();
			boolean bool = new EmployeeDaoImp().check(eid);
			if (bool) {
				new EmployeeDaoImp().selectEmp(eid);
				
			} else {
				System.out.println("该员工不存在");
			}
		default:
			break;
		}	
	}
	
}
从MainUI里面的switch里面跳转到EmpUI里面的增删改查的UI界面。

3.数据库操作层(package:com.XXX.dao 包含:DBConnectonUtil.java EmployeeImp.java EmployeeImpDao.java)

DBConnectionUtil.java

package com.XXX.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DBConnectionUtil {
	public Connection getConnection(){
		Connection connection = null;
		String username = "scott";
		String password = "tiger";
        String url = "jdbc:oracle:thin:@localhost:1521:XXX";//XXX自行替换为自己的
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection = DriverManager.getConnection(url,username,password);
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	public void release( ResultSet resultSet,PreparedStatement preparedStatement,Connection connection){
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if (preparedStatement != null) {
			try {
				preparedStatement.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

EmployeeImp.java

package com.XXX.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import com.inspur.vo.Employee;
/*
 * 实现IEmployeeDao接口
 * 实现IEmployeeDao接口已有7个函数接口,新增两个private函数:drawTable(),pad();
 */
public class EmployeeDaoImp implements IEmployeeDao{
	private static final int COLUMN_WIDTH = 17;

	@Override
	public boolean updateEmp(Employee employee) {

		PreparedStatement preparedStatement = null;
		Connection connection = new DBConnectionUtil().getConnection();
		String sql = "update employee set ename=?,eidno=?,etel=?,edept=?,ejob=?,email=? where eid=?";
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,employee.getEName());
			preparedStatement.setString(2, employee.getEIdno());
			preparedStatement.setLong(3, employee.getETel());
			preparedStatement.setString(4, employee.getEDept());
			preparedStatement.setString(5,employee.getEJob());
			preparedStatement.setString(6, employee.getEMail());
			preparedStatement.setInt(7, employee.getEId());
			int flag = preparedStatement.executeUpdate();
			if (flag != 0)
				return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(null, preparedStatement, connection);
		}
		return false;
	}
	/*
	 * 删除学生信息(非 Javadoc)
	 * @see com.inspur.dao.IEmployeeDao#deleteEmp(int)
	 */
	@Override
	public boolean deleteEmp(int eid) {
		PreparedStatement preparedStatement = null;
		Connection connection = new DBConnectionUtil().getConnection();
		String sql = "delete from employee where eid=?";
		try{
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1,eid);
			int flag = preparedStatement.executeUpdate();
			if (flag != 0)
				return true;
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(null, preparedStatement, connection);
		}
		return false;
	}
	/*
	 * 插入学生信息(非 Javadoc)
	 * @see com.inspur.dao.IEmployeeDao#insertEmp(com.inspur.pojo.Employee)
	 */
	@Override
	public boolean insertEmp(Employee employee) {

		PreparedStatement preparedStatement = null;
		Connection connection = new DBConnectionUtil().getConnection();
		String sql = "insert into employee (eid,ename,eidno,etel,edept,ejob,email)values(?,?,?,?,?,?,?)";
		try{
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, employee.getEId());
			preparedStatement.setString(2, employee.getEName());
			preparedStatement.setString(3, employee.getEIdno());
			preparedStatement.setLong(4, employee.getETel());
			preparedStatement.setString(5, employee.getEDept());
			preparedStatement.setString(6, employee.getEJob());
			preparedStatement.setString(7,employee.getEMail());
			int flag = preparedStatement.executeUpdate();
			if (flag != 0)
				return true;
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(null, preparedStatement, connection);
		}
		return false;
	}
	/*
	 * 查询学生信息(非 Javadoc)
	 * @see com.inspur.dao.IEmployeeDao#selectEmp(int)
	 */
	@Override
	public Employee selectEmp(int eid) {

		Employee employee = null;
		PreparedStatement preparedStatement = null;
		ResultSet result = null;
		Connection connection = new DBConnectionUtil().getConnection();
		String sql = "select * from employee where eid=?";
		try {
			preparedStatement= connection.prepareStatement(sql);
			preparedStatement.setInt(1, eid);
			result = preparedStatement.executeQuery();
			drawTable(result);
			while (result.next()) {
				int eId=result.getInt(1);
				String ename=result.getString(2);
				String eidno=result.getString(3);
				long etel=result.getLong(4);
				String edept=result.getString(5);
				String ejob=result.getString(6);
				String email=result.getString(7);
				employee=new Employee(eId,ename,eidno,etel,edept,ejob,email);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(result, preparedStatement, connection);
		}
		
		return employee;
	}
	/*
	 * 核对eid看是否有重复(非 Javadoc)
	 * @see com.inspur.dao.IEmployeeDao#check(int)
	 */
	@Override
	public boolean check(int eid) {

		PreparedStatement preparedStatement = null;
		Connection connection = null;
		ResultSet result =null;
		String sql = "select * from employee where eid=?";
		try{
			connection = new DBConnectionUtil().getConnection();
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, eid);
			result = preparedStatement.executeQuery();
			while(result.next()){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(result, preparedStatement, connection);
		}
		return false;
	}
	@Override
	public boolean showAll() {

		Connection connection = null;
		java.sql.Statement statement = null;
		ResultSet result = null;
		String sql = "select * from employee";
		try {
			connection = new DBConnectionUtil().getConnection();
			statement = connection.createStatement();
			result = statement.executeQuery(sql);//直接执行sql语句
			drawTable(result);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			new DBConnectionUtil().release(result, null, connection);
		}
		return false;
	}
	
	/*
	 * 核对身份证信息是否与已有的员工身份证信息重复(非 Javadoc)
	 * @see com.inspur.dao.IEmployeeDao#checkIdno(java.lang.String)
	 */
	@Override
	public boolean checkIdno(String eidno) {
		PreparedStatement preparedStatement = null;
		Connection connection = new DBConnectionUtil().getConnection();
		String sql = "select * from employee where eidno=?";
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, eidno);
			ResultSet result = preparedStatement.executeQuery();
			while(result.next()){
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return false;
	}
	/*
	 * 根据传来的结果集绘制图表结构
	 */
	private void drawTable(ResultSet result){//函数参数为传来的结果集
		
		try{
			ResultSetMetaData resultSetMetaData = result.getMetaData();//读取元数据。
			int columnCount = resultSetMetaData.getColumnCount();
			int length = columnCount * COLUMN_WIDTH+6;//表格的宽度
			//绘制表格结构
			StringBuilder sb1 = new StringBuilder();
			for (int i = 0; i < length; i++) {
				sb1.append("=");
			}
			System.out.println();
			System.out.println(sb1.toString());
			for (int i = 0; i < columnCount; i++) {
				System.out.print(pad(resultSetMetaData.getColumnName(i + 1))+"|");
			}
			System.out.println();
			System.out.println(sb1.toString());
			while (result.next()) {
				String[] row = new String[columnCount];
				for (int i = 0; i < columnCount; i++) {
					row[i] = result.getString(i + 1);
					System.out.print(pad(row[i])+"|");
				}
				System.out.println();
			}
			System.out.println(sb1.toString());
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	/*
	 * 在s后面添加空格,总是使其列宽的长度为25.
	 */
	private String pad(String s){
		int padCount = COLUMN_WIDTH - s.length();
		StringBuilder sb = new StringBuilder();
		sb.append(s);
		for (int i = 0; i < padCount; i++) {
			sb.append(" ");
		}
		return sb.toString();
	}
}

其中我写了个drawTable(),用于根据结果集绘制表格结构,类似于在查询分析器里执行select语句得到的图表结构。里面有些许的算法,就是调用了我写的pad(),也就是先规定好每一列的宽度,然后减去读取元数据(比如获得表的某个列名)得到的长度,这个长度也就是在获得字符串后面加的“=”(空格)数,使得表格可以整齐的展现出来。其实主要目的是想用到从结果集ResultSet读取元数据的知识点。

EmployeeDao.java

<span style="font-family:Microsoft YaHei;font-size:18px;">package com.XXX.dao;
import com.XXX.vo.Employee;

/* 接口
 * 函数个数:7
 * boolean updateEmp()
 * boolean deleteEmp()
 * boolean insertEmp()
 * Employee selectEmp()
 * boolean check()
 */
public interface IEmployeeDao {
	public boolean updateEmp(Employee employee);//更新
	public boolean deleteEmp(int eid);//删除
	public boolean insertEmp(Employee employee);//插入
	public Employee selectEmp(int eid);//查询
	public boolean check(int eid);//核对id(员工id工号)
	public boolean showAll();//显示全部表
	public boolean checkIdno(String eidno);//核对idno(员工身份证号)
	
}
</span>

定义一个接口。

三.说在后面的话

1.先上几张图片




2.关于一些配置

数据库用的Oracle数据库,在Eclipse或者IDEA编写程序时,需引入Oracle数据库的jar包。


  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
好的,我可以为您解答。首先,我们需要确定这个仓库管理系统需要哪些功能,例如添加货物、删除货物、修改货物信息、查询货物信息等等。然后,我们就可以根据这些功能设计出相应的数据库表。 假设我们需要管理货物的名称、数量、价格等信息,我们可以设计一个名为“goods”的表,包含以下字段: - id:货物的唯一标识符,可以用自增长的整数类型 - name:货物名称,可以用字符串类型 - quantity:货物数量,可以用整数类型 - price:货物价格,可以用浮点数类型 接下来,我们可以使用JDBC技术来连接数据库,并实现相应的增删改查操作。以下是一个示例代码: ```java import java.sql.*; public class WarehouseManagementSystem { // JDBC驱动器名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/warehouse"; // 数据库凭据 static final String USER = "root"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 注册JDBC驱动器 Class.forName(JDBC_DRIVER); // 打开连接 System.out.println("连接到数据库..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 System.out.println("创建语句..."); stmt = conn.createStatement(); String sql = "SELECT id, name, quantity, price FROM goods"; ResultSet rs = stmt.executeQuery(sql); // 处理结果集 while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); int quantity = rs.getInt("quantity"); double price = rs.getDouble("price"); // 输出结果 System.out.print("ID: " + id); System.out.print(", Name: " + name); System.out.print(", Quantity: " + quantity); System.out.println(", Price: " + price); } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { // 处理JDBC错误 se.printStackTrace(); } catch (Exception e) { // 处理Class.forName错误 e.printStackTrace(); } finally { // 关闭资源 try { if (stmt != null) stmt.close(); } catch (SQLException se2) { } try { if (conn != null) conn.close(); } catch (SQLException se) { se.printStackTrace(); } } System.out.println("Goodbye!"); } } ``` 上面的代码使用JDBC驱动器来连接MySQL数据库,并执行了一个查询语句,将结果集打印出来。您可以根据自己的需求来修改其中的代码,实现添加、删除、修改和查询货物信息等功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值