首页>代码>java web学习入门的失物招领系统>/findu-master/src/com/findu/dao/TypeDAO.java
package com.findu.dao;

import com.findu.entity.Type;
import com.findu.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class TypeDAO {
	
	/**
	 * 通过id获得相应分类对象
	 * @param id
	 * @return
	 */
	public static Type findTypeById(int id) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();

			String sql = "select * from type where typeid=?";
			stat = conn.prepareStatement(sql);
			stat.setInt(1, id);

			rs = stat.executeQuery();

			if (rs.next()) {
				Type t = new Type();
				t.setTypeid(rs.getInt("typeid"));
				t.setTypename(rs.getString("typename"));
				Type localType1 = t;
				return localType1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.release(rs, stat, conn);
		}
		JDBCUtils.release(rs, stat, conn);

		return null;
	}
	
	/**
	 * 通过分类名称获得相应分类对象
	 * @param name
	 * @return
	 */
	public static Type findTypeByName(String name) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();

			String sql = "select * from type where typename=?";
			stat = conn.prepareStatement(sql);
			stat.setString(1, name);

			rs = stat.executeQuery();

			if (rs.next()) {
				Type t = new Type();
				t.setTypeid(rs.getInt("typeid"));
				t.setTypename(rs.getString("typename"));
				Type localType1 = t;
				return localType1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.release(rs, stat, conn);
		}
		JDBCUtils.release(rs, stat, conn);

		return null;
	}
	
	/**
	 * 通过分类名查找分类id
	 * @param name
	 * @return
	 */
	public int findTypeIdByName(String name) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();

			String sql = "select typeid from type where typename=?";
			stat = conn.prepareStatement(sql);
			stat.setString(1, name);

			rs = stat.executeQuery();

			if (rs.next()) {
				int i = rs.getInt("typeid");
				return i;
			}
			return -1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.release(rs, stat, conn);
		}
		return -1;
	}
	
	/**
	 * 通过分类id找分类名
	 * @param typeid
	 * @return
	 */
	public String findTypeNameById(int typeid) {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();

			String sql = "select typename from type where typeid=?";
			stat = conn.prepareStatement(sql);
			stat.setInt(1, typeid);

			rs = stat.executeQuery();

			if (rs.next()) {
				String str1 = rs.getString("typename");
				return str1;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.release(rs, stat, conn);
		}
		return null;
	}
	
	/**
	 * 获得所有分类
	 * @return
	 */
	public ArrayList<Type> getAllTypes() {
		Connection conn = null;
		PreparedStatement stat = null;
		ResultSet rs = null;

		ArrayList list = new ArrayList();
		try {
			conn = JDBCUtils.getConnection();

			String sql = "select * from type";
			stat = conn.prepareStatement(sql);

			rs = stat.executeQuery();

			while (rs.next()) {
				Type t = new Type();
				t.setTypeid(rs.getInt("typeid"));
				t.setTypename(rs.getString("typename"));

				list.add(t);
			}
			ArrayList localArrayList1 = list;
			return localArrayList1;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.release(rs, stat, conn);
		}
		return null;
	}
}
最近下载更多
wangsheng818  LV4 4月4日
大道至简  LV13 2023年11月3日
jiemomo  LV12 2023年10月19日
cybcyb  LV1 2023年9月5日
dowell_liu  LV1 2023年8月2日
xhx行不行  LV1 2023年6月12日
小顾顾顾顾顾  LV4 2023年6月2日
娜娜子zz  LV2 2023年5月16日
gaoxuyang  LV8 2023年3月23日
lilonghui  LV3 2023年3月12日
最近浏览更多
wangsheng818  LV4 4月4日
tll123456  LV1 3月15日
sweet1 2月29日
暂无贡献等级
Octal1028 2月12日
暂无贡献等级
lyhlyh  LV10 2月4日
暂无贡献等级
ceshi56898 2023年12月30日
暂无贡献等级
smysmy 2023年12月28日
暂无贡献等级
asddwh  LV12 2023年12月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友