www.pudn.com > lucene+mysql+eclipe.rar > Product.java


package com.lucene; 
import java.util.ArrayList; 
import java.util.Date; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
/* 
 * @CopyRigth(R) 城市通 
 * @author 申华锋 E-mail:leonshine@qq.com 
 * @version 创建时间:Mar 9, 2008 12:12:45 AM 
 * @description  
 */ 
public class Product { 
	private long productId; 
 
	private String title; 
 
	private String description; 
 
	private String address; 
 
	private String userName; 
 
	private long userId; 
 
	private String tag; 
 
	private Date date; 
 
	public String getAddress() { 
		return address; 
	} 
 
	public void setAddress(String address) { 
		this.address = address; 
	} 
 
	public String getDescription() { 
		return description; 
	} 
 
	public void setDescription(String description) { 
		this.description = description; 
	} 
 
	public long getProductId() { 
		return productId; 
	} 
 
	public void setProductId(long productId) { 
		this.productId = productId; 
	} 
 
	public String getTag() { 
		return tag; 
	} 
 
	public void setTag(String tag) { 
		this.tag = tag; 
	} 
 
	public String getTitle() { 
		return title; 
	} 
 
	public void setTitle(String title) { 
		this.title = title; 
	} 
 
	public long getUserId() { 
		return userId; 
	} 
 
	public void setUserId(long userId) { 
		this.userId = userId; 
	} 
 
	public String getUserName() { 
		return userName; 
	} 
 
	public void setUserName(String userName) { 
		this.userName = userName; 
	} 
	public String ToString(){ 
		 
		return "这是个照片的实体"; 
	} 
 
	public Date getDate() { 
		return date; 
	} 
 
	public void setDate(Date date) { 
		this.date = date; 
	} 
	 
	//查询数据库,返回Product对象数组 
	public static Product[] loadProducts() throws Exception { 
		ArrayList list = new ArrayList(); 
		PreparedStatement pstm = null; 
		ResultSet rs = null; 
		String sql = "select product_id,title,address,descr,user_id,user_name,upload_time,tag_name from product"; 
		try { 
			pstm = DBConnection.getConnection().prepareStatement(sql); 
			rs = pstm.executeQuery(); 
			while (rs.next()) { 
				Product photo = new Product(); 
				photo.setProductId(rs.getLong(1)); 
				photo.setTitle(rs.getString(2)); 
				photo.setAddress(rs.getString(3)); 
				photo.setDescription(rs.getString(4)); 
				photo.setUserId(rs.getLong(5)); 
				photo.setUserName(rs.getString(6)); 
				photo.setDate(rs.getTimestamp(7)); 
				photo.setTag(rs.getString(8)); 
				list.add(photo); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (rs != null) { 
				rs.close(); 
			} 
			if (pstm != null) { 
				pstm.close(); 
			} 
		} 
		return (Product[]) list.toArray(new Product[list.size()]); 
	} 
	 
	//根据传入id获取一个Product 
	public static Product queryProductbyId(long id)throws Exception{ 
		Product photo = new Product(); 
		PreparedStatement pstm = null; 
		ResultSet rs = null; 
		String sql = "select product_id,title,address,descr,user_id,user_name,upload_time,tag_name from product where product_id ='"+id+"'"; 
		try { 
			pstm = DBConnection.getConnection().prepareStatement(sql); 
			rs = pstm.executeQuery(); 
			while (rs.next()) { 
				 
				photo.setProductId(rs.getLong("product_id")); 
				photo.setTitle(rs.getString("title")); 
				photo.setAddress(rs.getString("address")); 
				photo.setDescription(rs.getString("descr")); 
				photo.setUserId(rs.getLong("user_id")); 
				photo.setUserName(rs.getString("user_name")); 
				photo.setDate(rs.getTimestamp("upload_time")); 
				photo.setTag(rs.getString("tag_name")); 
			} 
		} catch (SQLException e) { 
			e.printStackTrace(); 
		} finally { 
			if (rs != null) { 
				rs.close(); 
			} 
			if (pstm != null) { 
				pstm.close(); 
			} 
		} 
		return photo; 
	}  
}