Product.java
上传用户:zhihansy
上传日期:2014-12-04
资源大小:7241k
文件大小:4k
源码类别:

搜索引擎

开发平台:

Java

  1. package com.lucene;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.sql.Connection;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. /*
  9.  * @CopyRigth(R) 城市通
  10.  * @author 申华锋 E-mail:leonshine@qq.com
  11.  * @version 创建时间:Mar 9, 2008 12:12:45 AM
  12.  * @description 
  13.  */
  14. public class Product {
  15. private long productId;
  16. private String title;
  17. private String description;
  18. private String address;
  19. private String userName;
  20. private long userId;
  21. private String tag;
  22. private Date date;
  23. public String getAddress() {
  24. return address;
  25. }
  26. public void setAddress(String address) {
  27. this.address = address;
  28. }
  29. public String getDescription() {
  30. return description;
  31. }
  32. public void setDescription(String description) {
  33. this.description = description;
  34. }
  35. public long getProductId() {
  36. return productId;
  37. }
  38. public void setProductId(long productId) {
  39. this.productId = productId;
  40. }
  41. public String getTag() {
  42. return tag;
  43. }
  44. public void setTag(String tag) {
  45. this.tag = tag;
  46. }
  47. public String getTitle() {
  48. return title;
  49. }
  50. public void setTitle(String title) {
  51. this.title = title;
  52. }
  53. public long getUserId() {
  54. return userId;
  55. }
  56. public void setUserId(long userId) {
  57. this.userId = userId;
  58. }
  59. public String getUserName() {
  60. return userName;
  61. }
  62. public void setUserName(String userName) {
  63. this.userName = userName;
  64. }
  65. public String ToString(){
  66. return "这是个照片的实体";
  67. }
  68. public Date getDate() {
  69. return date;
  70. }
  71. public void setDate(Date date) {
  72. this.date = date;
  73. }
  74. //查询数据库,返回Product对象数组
  75. public static Product[] loadProducts() throws Exception {
  76. ArrayList<Product> list = new ArrayList<Product>();
  77. PreparedStatement pstm = null;
  78. ResultSet rs = null;
  79. String sql = "select product_id,title,address,descr,user_id,user_name,upload_time,tag_name from product";
  80. try {
  81. pstm = DBConnection.getConnection().prepareStatement(sql);
  82. rs = pstm.executeQuery();
  83. while (rs.next()) {
  84. Product photo = new Product();
  85. photo.setProductId(rs.getLong(1));
  86. photo.setTitle(rs.getString(2));
  87. photo.setAddress(rs.getString(3));
  88. photo.setDescription(rs.getString(4));
  89. photo.setUserId(rs.getLong(5));
  90. photo.setUserName(rs.getString(6));
  91. photo.setDate(rs.getTimestamp(7));
  92. photo.setTag(rs.getString(8));
  93. list.add(photo);
  94. }
  95. } catch (SQLException e) {
  96. e.printStackTrace();
  97. } finally {
  98. if (rs != null) {
  99. rs.close();
  100. }
  101. if (pstm != null) {
  102. pstm.close();
  103. }
  104. }
  105. return (Product[]) list.toArray(new Product[list.size()]);
  106. }
  107. //根据传入id获取一个Product
  108. public static Product queryProductbyId(long id)throws Exception{
  109. Product photo = new Product();
  110. PreparedStatement pstm = null;
  111. ResultSet rs = null;
  112. String sql = "select product_id,title,address,descr,user_id,user_name,upload_time,tag_name from product where product_id ='"+id+"'";
  113. try {
  114. pstm = DBConnection.getConnection().prepareStatement(sql);
  115. rs = pstm.executeQuery();
  116. while (rs.next()) {
  117. photo.setProductId(rs.getLong("product_id"));
  118. photo.setTitle(rs.getString("title"));
  119. photo.setAddress(rs.getString("address"));
  120. photo.setDescription(rs.getString("descr"));
  121. photo.setUserId(rs.getLong("user_id"));
  122. photo.setUserName(rs.getString("user_name"));
  123. photo.setDate(rs.getTimestamp("upload_time"));
  124. photo.setTag(rs.getString("tag_name"));
  125. }
  126. } catch (SQLException e) {
  127. e.printStackTrace();
  128. } finally {
  129. if (rs != null) {
  130. rs.close();
  131. }
  132. if (pstm != null) {
  133. pstm.close();
  134. }
  135. }
  136. return photo;
  137. }