trainclassdao.java

来自「J2ee开发的 人事管理系统 使用oracle数据库 myeclips平台开」· Java 代码 · 共 309 行

JAVA
309
字号
package com.galaxy.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.galaxy.util.PageHelp;
import com.galaxy.vo.TrainClassVO;

import com.galaxy.base.DaoInterface;
import com.galaxy.db.ConnectDB;

public class TrainClassDAO extends ConnectDB implements DaoInterface {

	public int addObject(Object ob) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int deleteObject(Object cond) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List queryByCondition(Object cond) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object readObject(Object cond) {
		// TODO Auto-generated method stub
		return null;
	}

	public int updateObject(Object ob) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int add(TrainClassVO tcVo) {
		super.openDBConnection();
		try {
			String sql = "insert into train_class values(galaxy.seq.nextval,?,?,'1')";
			PreparedStatement psm = super.dbConnection.prepareStatement(sql);
			psm.setString(1, tcVo.getTcName());
			psm.setString(2, tcVo.getTcTag());

			//tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录

			//psm.setString(3, tcVo.getTcExtend());

			int i = psm.executeUpdate();
			return i;

		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			// 关闭连接,释放数据库资源:
			super.closeDBConnection();
		}
	}

	public int update(TrainClassVO tcVo) {
		super.openDBConnection();
		try {

			//			tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录

			String sql = "update train_class set TC_ID=" + tcVo.getTcId()
					+ ",TC_NAME ='" + tcVo.getTcName() + "',TC_TAG='"
					+ tcVo.getTcTag() + "',TC_EXTEND='1' " + " where TC_ID="
					+ tcVo.getTcId() + "";
			int i = super.dbStatement.executeUpdate(sql); //执行sql语句
			return i;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			// 关闭连接,释放数据库资源:
			super.closeDBConnection();
		}
	}

	public int delete(int id) {
		super.openDBConnection();
		try {
			String sql = "update train_class set tc_extend='0' where tc_id in ("
					+ id + ")";
			int i = super.dbStatement.executeUpdate(sql); // 执行sql语句
			return i;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return -1;
		} catch (Exception E) {
			E.printStackTrace();
			return -2;
		} finally {
			// 关闭连接,释放数据库资源:
			super.closeDBConnection();
		}
	}

	//通过培训类别ID获得培训类别对象
	public TrainClassVO get(Long id) {
		super.openDBConnection();
		ResultSet rs = null;
		try {
			//			tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录
			String sql = "select * from train_class where tc_extend='1' and tc_id = "
					+ id + "";
			rs = super.dbStatement.executeQuery(sql); // 执行sql语句
			TrainClassVO tcVo = null;
			if (rs.next()) {
				tcVo = new TrainClassVO();
				tcVo.setTcId(id);
				tcVo.setTcName(rs.getString("TC_NAME"));
				tcVo.setTcTag(rs.getString("TC_TAG"));
				return tcVo;
			}
			return tcVo;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			super.closeDBConnection();
		}
	}

	//通过培训类别名称获得培训类别对象
	//这个函数是为了满足培训类别名称模糊查询,接受一个name参数,模糊查询出名称包含该字段的对象
	public TrainClassVO get(String name) {
		super.openDBConnection();
		ResultSet rs = null;
		try {
			//			tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录
			String sql = "select * from train_class where tc_extend='1' and tc_name = '"
					+ name + "'";
			rs = super.dbStatement.executeQuery(sql); // 执行sql语句
			TrainClassVO tcVo = null;

			//这里只返回一个tcVo,而不是一个tcVo的LIST
			//例如:传入参数为c,但是数据库名称有c,c++,c#.如果返回tcVo的LIST,则会显示出培训种类为c,c++,c#的所有记录
			//而如果只返回一个tcVo,则只会精确定位到c的所有记录。
			if (rs.next()) {
				tcVo = new TrainClassVO();
				tcVo.setTcId(Long.valueOf(rs.getString("TC_ID")));
				tcVo.setTcName(rs.getString("TC_NAME"));
				tcVo.setTcTag(rs.getString("TC_TAG"));
				return tcVo;
			}
			return tcVo;
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();
			return null;
		} catch (Exception E) {
			E.printStackTrace();
			return null;
		} finally {
			// 关闭连接,释放数据库资源:
			super.closeDBConnection();
		}
	}

	public List getFindAll() {
		List trainClassList = new ArrayList();
		//		tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
		//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
		//显示列表时,只显示可用(tr_extend='1')的记录
		String psql = "select * from train_class where tc_extend='1'";
		super.openDBConnection();
		try {
			super.dbResultSet = super.dbStatement.executeQuery(psql);
			while (super.dbResultSet.next()) {
				TrainClassVO tcVo = new TrainClassVO();
				tcVo.setTcId(dbResultSet.getLong("tc_id"));
				tcVo.setTcName(dbResultSet.getString("tc_name"));
				tcVo.setTcTag(dbResultSet.getString("tc_tag"));

				trainClassList.add(tcVo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		super.closeDBConnection();
		return trainClassList;
	}

	//	获得培训类别信息上下列表翻页---------参数信息为 sql语句中的部分,每页显示记录数,当前页码
	public PageHelp getTrainClassList(String condition, int pageSize,
			int currentPage) {

		int listSize = 0;
		List showlist = new ArrayList();//存放要显示到页面上的部分结果
		PageHelp pageHelp = new PageHelp();
		String sql = "";
		try { //统计记录总数
			super.openDBConnection();
			ResultSet rs = null;

			//			tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录
			sql = "select count(*) listSize from train_class where tc_extend='1' ";
			if (!"".equals(condition) || !"null".equals(condition))
				sql = "select count(*) listSize from train_class where tc_extend='1' "
						+ condition;
			sql = sql + " order by TC_ID asc";

			pageHelp.setSqlstr(sql);
			rs = super.dbStatement.executeQuery(sql);

			// 执行sql语句
			while (rs.next()) {
				listSize = rs.getInt("listSize");
			}
		} catch (SQLException SqlE) {
			SqlE.printStackTrace();

		} catch (Exception E) {
			E.printStackTrace();

		} finally {
			// 关闭连接,释放数据库资源:
			//db_conn.CloseDB();	
			super.closeDBConnection();

		}

		try { //找到要显示的记录
			super.openDBConnection();
			ResultSet rs = null;
			int startNum = (currentPage - 1) * pageSize + 1;//由于数据库中没有第0条记录所以要进行+1修正
			int endNum = currentPage * pageSize + 1;

			//			tc_extend字段用于标识该条记录是否可用。为1则可用。为0不可用。
			//在添加时,初始设为1,即可用。修改时,不能修改该字段。只有在删除时,将该字段设置为0。即不可用。
			//显示列表时,只显示可用(tr_extend='1')的记录

			sql = "select * from (select a.* ,rownum rc from(select * from train_class where tc_extend='1' order by tc_id asc) a where rownum<"
					+ endNum + ") b where rc >=" + startNum + "";
			if (!"".equals(condition) && condition != null)
				sql = "select * from (select a.* ,rownum rc from(select * from train_class where tc_extend='1' "
						+ condition
						+ " order by tc_id asc) a where rownum<"
						+ endNum + ") b where rc >=" + startNum + "";
			sql = sql + " order by TC_ID asc";
			//System.out.println("sqllist------"+sql);			
			pageHelp.setSqlstr(sql);
			super.dbResultSet = super.dbStatement.executeQuery(sql);
			rs = super.dbResultSet;

			// 执行sql语句
			while (rs.next()) {
				TrainClassVO tcVo = new TrainClassVO();
				tcVo.setTcId(rs.getLong("TC_ID"));
				tcVo.setTcName(rs.getString("TC_NAME"));
				tcVo.setTcTag(rs.getString("TC_TAG"));

				showlist.add(tcVo);
			}

		} catch (SQLException SqlE) {
			SqlE.printStackTrace();

		} catch (Exception E) {
			E.printStackTrace();

		} finally {
			// 关闭连接,释放数据库资源:
			//db_conn.CloseDB();	
			super.closeDBConnection();

		}

		// 设置页面有关分页的显示信息	
		pageHelp.setCondition(condition);
		pageHelp.setCurrentpage(currentPage); //要显示的是第几页
		pageHelp.setPagesize(pageSize); //每页显示几条记录
		pageHelp.setRecordcount(listSize); //按当前条件查询结果的全部记录数(总条数)
		pageHelp.getPagecount(); //按照“页数=记录总数/每页显示条数”得到显示页数
		pageHelp.setSqlstr(sql); //将当前的查询条件装入gageHelp对象中
		pageHelp.setPagebar("/training/TR_TrainClassServlet");//设置上一页,下一页,首页,末页的显示条
		pageHelp.setObjectlist(showlist);//将list对象存储起来
		return pageHelp;
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?