首页>代码>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;
	}
}
最近下载更多
打死王二狗  LV1 2024年12月16日
张泽帅  LV6 2024年10月21日
亦叶灰  LV1 2024年10月18日
yunduan123  LV1 2024年10月14日
柳咪华沙  LV7 2024年6月28日
wangsheng818  LV4 2024年4月4日
大道至简  LV13 2023年11月3日
jiemomo  LV12 2023年10月19日
cybcyb  LV1 2023年9月5日
dowell_liu  LV1 2023年8月2日
最近浏览更多
付洋麟  LV5 4月23日
233123 2024年12月20日
暂无贡献等级
xinshou11111x 2024年12月19日
暂无贡献等级
打死王二狗  LV1 2024年12月13日
asdxvfvfs 2024年12月3日
暂无贡献等级
3312djdad  LV2 2024年11月8日
亦叶灰  LV1 2024年10月17日
张泽帅  LV6 2024年10月17日
yunduan123  LV1 2024年10月7日
799743530  LV11 2024年7月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友