NewsAttachmentDAOImpl.java
上传用户:junmaots
上传日期:2022-07-09
资源大小:2450k
文件大小:7k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. /*
  2.  * Created on 2005-11-10
  3.  *
  4.  * TODO To change the template for this generated file go to
  5.  * Window - Preferences - Java - Code Style - Code Templates
  6.  */
  7. package com.mycompany.news.dao.impl;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import com.mycompany.database.Database;
  15. import com.mycompany.news.dao.NewsAttachmentDAO;
  16. import com.mycompany.news.dto.News;
  17. import com.mycompany.news.dto.NewsAttachment;
  18. import com.mycompany.tools.DTOPopulator;
  19. import com.opensymphony.util.BeanUtils;
  20. /**
  21.  * @author Administrator
  22.  *
  23.  * TODO To change the template for this generated type comment go to
  24.  * Window - Preferences - Java - Code Style - Code Templates
  25.  */
  26. public class NewsAttachmentDAOImpl implements NewsAttachmentDAO{
  27. Connection connection = null;
  28. /**
  29.  * @return Returns the connection.
  30.  */
  31. public Connection getConnection() {
  32. return connection;
  33. }
  34. /**
  35.  * @param connection The connection to set.
  36.  */
  37. public void setConnection(Connection connection) {
  38. this.connection = connection;
  39. }
  40. public void addNewsAttachment(NewsAttachment newsAttachment) throws SQLException{
  41. String sql="insert into News_Attachment(news_id,attachment_name,attachment_content) values(?,?,?)";
  42. PreparedStatement ps = connection.prepareStatement(sql);
  43. int i=1;
  44. ps.setLong(i++,newsAttachment.getNewsId().longValue());
  45. ps.setString(i++,newsAttachment.getAttachmentName());
  46. ps.setBytes(i++,newsAttachment.getAttachmentContent());
  47. ps.executeUpdate();
  48. ps.close();
  49. }
  50. public void updateNewsAttachment(NewsAttachment newsAttachment)throws Exception{
  51. List NewsAttachments = new ArrayList();
  52. NewsAttachments.add("attachment_Name");
  53. NewsAttachments.add("attachment_Order");
  54. NewsAttachments.add("attachment_Status");
  55. StringBuffer sqlString=new StringBuffer();
  56. sqlString.append("update NewsAttachment set ");
  57. for(int i=0;i<NewsAttachments.size();i++){
  58. if(NewsAttachments.size()-1==i)
  59. sqlString.append(NewsAttachments.get(i)+"=? ");
  60. else
  61. sqlString.append(NewsAttachments.get(i)+"=?, ");
  62. }
  63. sqlString.append(" where Attachment_id=?");
  64. System.out.println(sqlString);
  65. PreparedStatement ps = connection.prepareStatement(sqlString.toString());
  66. for(int i=0;i<NewsAttachments.size();i++){
  67. ps.setObject(i+1,BeanUtils.getValue(newsAttachment,((String)NewsAttachments.get(i)).replaceAll("_","")));
  68. }
  69. ps.setLong(NewsAttachments.size()+1,newsAttachment.getAttachmentId().longValue());
  70. ps.executeUpdate();
  71. ps.close();
  72. }
  73. public void deleteNewsAttachment(NewsAttachment newsAttachment)throws Exception{
  74. String sqlString="delete from  News_Attachment where Attachment_id=?";
  75. PreparedStatement ps = connection.prepareStatement(sqlString);
  76. ps.setLong(1,newsAttachment.getAttachmentId().longValue());
  77. ps.executeUpdate();
  78. ps.close();
  79. }
  80. public List listAllNewsAttachments() throws Exception{
  81. PreparedStatement ps = connection.prepareStatement("select * from News_Attachment");
  82. ResultSet rs = ps.executeQuery();
  83. List list = DTOPopulator.populate(rs,NewsAttachment.class);
  84. for(int i=0;i<list.size();i++){
  85. NewsAttachment c = (NewsAttachment) list.get(i);
  86. }
  87. rs.close();
  88. ps.close();
  89. return list;
  90. }
  91. public List listNewsAttachments(NewsAttachment newsAttachmentCondition)throws Exception{
  92. boolean newsCondition=false;
  93. StringBuffer stringBuffer = new StringBuffer();
  94. stringBuffer.append("select * from News_Attachment where 1=1");
  95. if(newsAttachmentCondition.getNewsId()!=null){
  96. stringBuffer.append(" and news_id=?");
  97. newsCondition=true;
  98. }
  99. PreparedStatement ps = connection.prepareStatement(stringBuffer.toString());
  100. int i=1;
  101. if(newsCondition){
  102. ps.setLong(i++,newsAttachmentCondition.getNewsId().longValue());
  103. }
  104. ResultSet rs = ps.executeQuery();
  105. List list = new ArrayList();
  106. while(rs.next()){
  107. NewsAttachment attachment = new NewsAttachment();
  108. attachment.setAttachmentId(new Long(rs.getLong("attachment_id")));
  109. attachment.setAttachmentName(rs.getString("attachment_name"));
  110. // attachment.setAttContent(rs.getBytes("attachment_conntent"));
  111. attachment.setNewsId(newsAttachmentCondition.getNewsId());
  112. list.add(attachment);
  113. }
  114. return list;
  115. }
  116. public NewsAttachment getByID(long id)throws Exception{
  117. PreparedStatement ps = connection.prepareStatement("select * from news_attachment where attachment_id=?");
  118. ps.setLong(1,id);
  119. ResultSet rs = ps.executeQuery();
  120. // while(rs.next()){
  121. // NewsAttachment att = new NewsAttachment();
  122. // att.setAttachmentName(rs.getString("attachment_name"));
  123. // att.setAttachmentId(new Long(id));
  124. // att.setAttachmentContent(rs.getBytes("attachment_content"));
  125. // }
  126. return (NewsAttachment) DTOPopulator.populate(rs,NewsAttachment.class).get(0);
  127. }
  128. public static void main(String[] args) {
  129. NewsAttachmentDAO dao = new NewsAttachmentDAOImpl();
  130. Connection conn =null;
  131. try {
  132. conn = Database.getConnection();
  133. dao.setConnection(conn);
  134. String s = "fffaaaaffffddddabcdefg";
  135. StringBuffer sb = new StringBuffer();
  136. for(int i=0;i<100000;i++){
  137. sb.append(s);
  138. }
  139. NewsAttachment newsAttachment=new NewsAttachment();
  140. newsAttachment.setAttachmentName("测试附件");
  141. newsAttachment.setAttachmentContent(sb.toString().getBytes());
  142. newsAttachment.setNewsId(new Long(22));
  143. dao.addNewsAttachment(newsAttachment);
  144. // dao.updateNewsAttachment(newsAttachment);
  145. Database.commit();
  146. // dao.listAllNewsAttachments();
  147. } catch (Exception e) {
  148. // TODO Auto-generated catch block
  149. e.printStackTrace();
  150. }finally{
  151. Database.releaseConnection(conn);
  152. }
  153. }
  154. /* (non-Javadoc)
  155.  * @see com.mycompany.NewsAttachment.dao.NewsAttachmentDAO#getRecommendedNewsAttachment(int)
  156.  */
  157. public List getRecommendedNewsAttachment(long columnid) throws Exception {
  158. // TODO Auto-generated method stub
  159. String sqlStr=" select ni.* from News_Attachment_Info ni left join NewsAttachment_attribute na on ni.NewsAttachment_id=na.NewsAttachment_id where na.NewsAttachment_attr_name=? and na.NewsAttachment_attr_value=? and ni.column_id=?";
  160. PreparedStatement ps = connection.prepareStatement(sqlStr);
  161. ps.setString(1,"is_recommend");
  162. ps.setString(2,"true");
  163. ps.setLong(3,columnid);
  164. ResultSet rs = ps.executeQuery();
  165. return DTOPopulator.populate(rs,NewsAttachment.class);
  166. }
  167. /* (non-Javadoc)
  168.  * @see com.mycompany.news.dao.NewsAttachmentDAO#deleteAttrByNews(com.mycompany.news.dto.News)
  169.  */
  170. public void deleteAttrByNews(News news) throws Exception {
  171. // TODO Auto-generated method stub
  172. String sqlString="delete from News_Attachment where news_id=?";
  173. PreparedStatement ps = connection.prepareStatement(sqlString);
  174. ps.setLong(1,news.getNewsId().longValue());
  175. ps.executeUpdate();
  176. ps.close();
  177. }
  178. }