depreciationdao.java

来自「struts做得固定资产管理系统 带毕业论文完整版」· Java 代码 · 共 194 行

JAVA
194
字号
package com.qrsx.appcam.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qrsx.appcam.model.Depreciation;

public class DepreciationDAO extends BaseDAO {
	/**
	 * 创建:资产对应的算法
	 * 
	 * @param depreciation
	 * @throws SQLException
	 */
	public void create(Depreciation depreciation) throws SQLException {

		String sql = "Insert into depreciation (assetId,arithmetic) values(?,?)";
		// 设置参数
		ps = conn.prepareStatement(sql);
		ps.setInt(1, depreciation.getAssetId());
		ps.setString(2, depreciation.getArithmetic());
		// 执行插入操作
		ps.executeUpdate();
	}

	/**
	 * 更新操作
	 * 
	 * @param depreciation
	 * @throws SQLException
	 */
	public void update(Depreciation depreciation) throws SQLException {

		String sql = "Update depreciation set assetId=?,arithmetic=? where id=?";
		// 设置参数并执行更新操作
		ps = conn.prepareStatement(sql);
		ps.setInt(1, depreciation.getAssetId());
		ps.setString(2, depreciation.getArithmetic());
		ps.setInt(3, depreciation.getId());
		ps.executeUpdate();
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public int delete(Integer id) throws SQLException {

		String sql = "delete from depreciation where id=?";
		// 设置参数并执行操作
		ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		int count = ps.executeUpdate();
		return count;
	}

	/**
	 * 按照编号检索
	 * 
	 * @param id
	 * @return Depreciation
	 * @throws SQLException
	 */
	public Depreciation findById(Integer id) throws SQLException {

		String sql = "select *  from depreciation where id=?";
		// 设置参数
		ps = conn.prepareStatement(sql);
		ps.setInt(1, id);
		// 执行查询操作
		ResultSet rs = ps.executeQuery();
		Depreciation depreciation = null;
		AssetDAO dao = new AssetDAO();
		// 组装对象
		if (rs.next()) {
			depreciation = new Depreciation();
			depreciation.setId(rs.getInt("id"));
			depreciation.setAssetId(rs.getInt("assetId"));
			depreciation.setArithmetic(rs.getString("arithmetic"));
			depreciation.setAsset(dao.findById(rs.getInt("assetId")));
		}
		return depreciation;
	}

	/**
	 * 按照资产编号检索
	 * 
	 * @param id
	 * @return Depreciation
	 * @throws SQLException
	 */
	public Depreciation findById(String assetId) throws SQLException {

		String sql = "select *  from depreciation where AssetId=?";
		// 设置参数
		ps = conn.prepareStatement(sql);
		ps.setInt(1, Integer.valueOf(assetId));
		// 执行查询操作
		ResultSet rs = ps.executeQuery();
		Depreciation depreciation = null;
		AssetDAO dao = new AssetDAO();
		// 组装对象
		if (rs.next()) {
			depreciation = new Depreciation();
			depreciation.setId(rs.getInt("id"));
			depreciation.setAssetId(rs.getInt("assetId"));
			depreciation.setArithmetic(rs.getString("arithmetic"));
			depreciation.setAsset(dao.findById(rs.getInt("assetId")));
		}
		return depreciation;
	}


	/**
	 * 按照条件检索
	 * 
	 * @param depreciation
	 * @return
	 * @throws SQLException
	 */
	public List<Depreciation> list(Depreciation depreciation)
			throws SQLException {

		String sql = "select * from depreciation, asset  where depreciation.assetId=asset.id and 1=1 ";

		// 根据资产代码检索
		String code = depreciation.getAsset().getCode();
		if (code != null && !code.equals("")) {
			sql = sql + "  and asset.code like '%" + code + "%'  ";
		}
		// 根据借出日期检索
		String assetName = depreciation.getAsset().getName();

		if (assetName != null && !assetName.equals("")) {

			sql = sql + "  and asset.name like '%" + assetName + "%'  ";
		}
		//根据折旧算法检索
		String arithmetic = depreciation.getArithmetic();

		if (arithmetic != null && !arithmetic.equals("")) {

			sql = sql + "  and arithmetic like '%" + arithmetic + "%'  ";
		}


		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		Depreciation depreciation1 = null;
		List<Depreciation> list = new ArrayList<Depreciation>();
		AssetDAO dao = new AssetDAO();
		while (rs.next()) {
			depreciation1 = new Depreciation();
			depreciation1.setId(rs.getInt("id"));
			depreciation1.setAssetId(rs.getInt("assetId"));
			depreciation1.setArithmetic(rs.getString("arithmetic"));
			depreciation1.setAsset(dao.findById(rs.getInt("assetId")));
			list.add(depreciation1);
		}
		return list;
	}

	/**
	 * 检索所有信息
	 * 
	 * @return
	 * @throws SQLException
	 */
	public List<Depreciation> findAll() throws SQLException {

		String sql = "select * from Depreciation ";
		// 设置参数并执行查询操作
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		Depreciation depreciation = null;
		List<Depreciation> list = new ArrayList<Depreciation>();
		AssetDAO dao = new AssetDAO();
		while (rs.next()) {
			depreciation = new Depreciation();
			depreciation.setId(rs.getInt("id"));
			depreciation.setAssetId(rs.getInt("assetId"));
			depreciation.setArithmetic(rs.getString("arithmetic"));
			depreciation.setAsset(dao.findById(rs.getInt("assetId")));
			list.add(depreciation);
		}
		return list;
	}

}

⌨️ 快捷键说明

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