www.pudn.com > restrant.zip > ClusterDao.java, change:2014-03-08,size:2828b


package com.alex.restrantol.dao; 
 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
 
import java.util.List; 
 
import com.alex.restrantol.base.BaseDao; 
import com.alex.restrantol.entity.Cluster; 
 
public class ClusterDao { 
 
	Connection con; 
	PreparedStatement ps; 
	ResultSet rs; 
	 
	public List<Cluster> findClusterAll() { 
		List<Cluster> list = new ArrayList<Cluster>(); 
		Cluster cluster = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("select * from cluster"); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				cluster = new Cluster(); 
				cluster.setId(rs.getInt("id")); 
				cluster.setCategory(rs.getString("category")); 
				cluster.setTitle(rs.getString("title")); 
				cluster.setActivityTime(rs.getDate("activityTime")); 
				cluster.setMainPoster(rs.getString("mainPoster")); 
				cluster.setPublicityPoster(rs.getString("publicityPoster")); 
				cluster.setClusterPlace(rs.getString("clusterPlace")); 
				cluster.setClusterInfo(rs.getString("clusterInfo")); 
				cluster.setStatus(rs.getInt("status")); 
				cluster.setRecommended(rs.getInt("recommended")); 
				list.add(cluster); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return list; 
	} 
 
	public Cluster findClusterById(int clusterId) { 
		Cluster cluster = null; 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("select * from cluster where id=?"); 
			ps.setInt(1, clusterId); 
			rs = ps.executeQuery(); 
			while(rs.next()){ 
				 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return cluster; 
	} 
 
	public boolean addCluster(Cluster cluster) { 
		boolean flag = false; 
		try { 
			con = new BaseDao().getConn(); 
			String sql = "insert into cluster(category,username,title,activityTime,clusterPlace,clusterInfo,status,recommended) values(?,?,?,?,?,?,?,?)"; 
			ps = con.prepareStatement(sql); 
			ps.setString(1, cluster.getCategory()); 
			ps.setString(2, cluster.getUserName()); 
			ps.setString(3, cluster.getTitle()); 
			ps.setDate(4, new java.sql.Date(cluster.getActivityTime().getTime())); 
			ps.setString(5, cluster.getClusterPlace()); 
			ps.setString(6, cluster.getClusterInfo()); 
			ps.setInt(7, cluster.getStatus()); 
			ps.setInt(8, cluster.getRecommended()); 
			ps.executeUpdate(); 
			flag = true; 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
		return flag; 
	} 
 
	public void modifyClusterState(int clusterId, int state) { 
		try { 
			con = new BaseDao().getConn(); 
			ps = con.prepareStatement("update cluster set state=? where id=?"); 
			ps.setInt(1, state); 
			ps.setInt(2, clusterId); 
			ps.executeUpdate(); 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} 
	} 
}