assettypedao.java

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

JAVA
153
字号
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.AssetType;

public class AssetTypeDAO extends BaseDAO {
	/**
	 * 创建资源类型
	 * 
	 * @param AssetType
	 */
	public void create(AssetType assetType) throws SQLException {
		String sql = "Insert Into AssetType(name, description) Values(?, ?)";

		// 设置参数值
		ps = conn.prepareStatement(sql);
		ps.setString(1, assetType.getName());
		ps.setString(2, assetType.getDescription());

		// 执行
		ps.executeUpdate();
	}

	/**
	 * 更新资源类型
	 * 
	 * @param AssetType
	 */
	public void update(AssetType assetType) throws SQLException {
		String sql = "Update AssetType set name=?, description=? where id=? ";

		// 设置参数值
		ps = conn.prepareStatement(sql);
		ps.setString(1, assetType.getName());
		ps.setString(2, assetType.getDescription());
		ps.setInt(3, assetType.getId());

		// 执行
		ps.executeUpdate();
	}

	/**
	 * 删除资源类型
	 * 
	 * @param AssetTypeId
	 * 
	 * @return 删除的资源类型数目
	 */
	public int delete(Integer assetTypeId) throws SQLException {
		String sql = "Delete from AssetType where id=? ";

		// 设置参数值
		ps = conn.prepareStatement(sql);
		ps.setInt(1, assetTypeId);

		// 执行
		int total = ps.executeUpdate();
		return total;
	}

	/**
	 * 根据主键检索资源类型
	 * 
	 * @param AssetTypeId
	 * 
	 * @return AssetType
	 */
	public AssetType findById(Integer assetTypeId) throws SQLException {
		String sql = "Select * from AssetType where id=? ";

		// 设置参数值并执行查询
		ps = conn.prepareStatement(sql);
		ps.setInt(1, assetTypeId);
		ResultSet rs = ps.executeQuery();

		AssetType assetType = null;
		if (rs.next()) {
			assetType = new AssetType();
			assetType.setId(rs.getInt("id"));
			assetType.setName(rs.getString("name"));
			assetType.setDescription(rs.getString("description"));
		}

		return assetType;
	}

	/**
	 * 检索资源类型
	 * 
	 * @param AssetType
	 * @return List<AssetType>
	 */
	public List<AssetType> findAll() throws SQLException {

		String sql = "Select * from AssetType ";

		// 执行查询
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();

		// 将查询结果组装至AssetType实体对象
		AssetType assetType = null;
		List<AssetType> list = new ArrayList<AssetType>();
		while (rs.next()) {
			assetType = new AssetType();
			assetType.setId(rs.getInt("id"));
			assetType.setName(rs.getString("name"));
			assetType.setDescription(rs.getString("description"));
			list.add(assetType);
		}

		return list;
	}

	/**
	 * 检索资源类型
	 * 
	 * @param AssetType
	 * @return List<AssetType>
	 */
	public List<AssetType> list(AssetType assetType) throws SQLException {
		String sql = "Select * from AssetType where 1=1 ";

		// 根据资产类型名称检索
		if (assetType.getName() != null && !assetType.getName().equals("")) {

			sql = sql + " and name like '%" + assetType.getName() + "%'";
		}

		// 执行查询
		ps = conn.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();

		// 将查询结果组装至AssetType实体对象
		AssetType assetType1 = null;
		List<AssetType> list = new ArrayList<AssetType>();
		while (rs.next()) {
			
			assetType1 = new AssetType();
			
			assetType1.setId(rs.getInt("id"));
			assetType1.setName(rs.getString("name"));
			assetType1.setDescription(rs.getString("description"));
			
			list.add(assetType1);
		}
		return list;
	}
}

⌨️ 快捷键说明

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