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

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.ChannelDAO;
  16. import com.mycompany.news.dto.Channel;
  17. import com.mycompany.news.dto.Column;
  18. import com.mycompany.news.dto.News;
  19. import com.mycompany.tools.DTOPopulator;
  20. import com.opensymphony.util.BeanUtils;
  21. /**
  22.  * @author Administrator
  23.  *
  24.  * TODO To change the template for this generated type comment go to
  25.  * Window - Preferences - Java - Code Style - Code Templates
  26.  */
  27. public class ChannelDAOImpl implements ChannelDAO{
  28. Connection connection = null;
  29. /**
  30.  * @return Returns the connection.
  31.  */
  32. public Connection getConnection() {
  33. return connection;
  34. }
  35. /**
  36.  * @param connection The connection to set.
  37.  */
  38. public void setConnection(Connection connection) {
  39. this.connection = connection;
  40. }
  41. public void addChannel(Channel channel) throws SQLException{
  42. String sql="insert into Channel(channel_Name,channel_Order,channel_Status) values(?,?,?)";
  43. PreparedStatement ps = connection.prepareStatement(sql);
  44. int i=1;
  45. ps.setString(i++,channel.getChannelName());
  46. ps.setInt(i++,channel.getChannelOrder().intValue());
  47. ps.setInt(i++,channel.getChannelStatus().intValue());
  48. ps.executeUpdate();
  49. ps.close();
  50. }
  51. public void updateChannel(Channel channel)throws Exception{
  52. List columns = new ArrayList();
  53. columns.add("channel_Name");
  54. columns.add("channel_Order");
  55. columns.add("channel_Status");
  56. StringBuffer sqlString=new StringBuffer();
  57. sqlString.append("update Channel set ");
  58. for(int i=0;i<columns.size();i++){
  59. if(columns.size()-1==i)
  60. sqlString.append(columns.get(i)+"=? ");
  61. else
  62. sqlString.append(columns.get(i)+"=?, ");
  63. }
  64. sqlString.append(" where channel_id=?");
  65. System.out.println(sqlString);
  66. PreparedStatement ps = connection.prepareStatement(sqlString.toString());
  67. for(int i=0;i<columns.size();i++){
  68. ps.setObject(i+1,BeanUtils.getValue(channel,((String)columns.get(i)).replaceAll("_","")));
  69. }
  70. ps.setLong(columns.size()+1,channel.getChannelID().longValue());
  71. ps.executeUpdate();
  72. ps.close();
  73. }
  74. public void deleteChannel(Channel channel)throws Exception{
  75. String sqlString="update Channel set channel_status=? where channel_id=?";
  76. PreparedStatement ps = connection.prepareStatement(sqlString);
  77. ps.setInt(1,-1);
  78. System.out.println("channel.getChannelID() = "+channel.getChannelID());
  79. ps.setLong(2,channel.getChannelID().longValue());
  80. ps.executeUpdate();
  81. ps.close();
  82. }
  83. public List listAllChannels() throws Exception{
  84. PreparedStatement ps = connection.prepareStatement("select * from channel where channel_status>=0 order by channel_order");
  85. ResultSet rs = ps.executeQuery();
  86. List list = DTOPopulator.populate(rs,Channel.class);
  87. rs.close();
  88. ps.close();
  89. return list;
  90. }
  91. public List listChannels(Channel channelCondition)throws Exception{
  92. boolean nameCondition=false;
  93. boolean statusCondition=false;
  94. StringBuffer stringBuffer = new StringBuffer();
  95. stringBuffer.append("select * from channel where channel_status>=0");
  96. if(channelCondition.getChannelName()!=null&&!channelCondition.getChannelName().trim().equals("")){
  97. stringBuffer.append("and channel_name like ?");
  98. nameCondition = true;
  99. }
  100. if(channelCondition.getChannelStatus()!=null){
  101. stringBuffer.append("and channel_status =?");
  102. statusCondition= true;
  103. }
  104. PreparedStatement ps = connection.prepareStatement(channelCondition.toString());
  105. int i=1;
  106. if(nameCondition)
  107. ps.setString(i++,"%"+channelCondition.getChannelName().trim()+"%");
  108. if(statusCondition)
  109. ps.setInt(i++,channelCondition.getChannelStatus().intValue());
  110. ResultSet rs = ps.executeQuery();
  111. return DTOPopulator.populate(rs,Channel.class);
  112. }
  113. public Channel getByID(long id)throws Exception{
  114. PreparedStatement ps = connection.prepareStatement("select * from channel where channel_id=?");
  115. ps.setLong(1,id);
  116. ResultSet rs = ps.executeQuery();
  117. return (Channel) DTOPopulator.populate(rs,Channel.class).get(0);
  118. }
  119. public List getChildren(Long channelID) throws Exception {
  120. PreparedStatement ps = connection.prepareStatement("select * from news_column n where n.channel_Id=? and n.current_level=0 order by n.column_order");
  121. ps.setLong(1,channelID.longValue());
  122. ResultSet rs = ps.executeQuery();
  123. return DTOPopulator.populate(rs,Column.class);
  124. }
  125. public static void main(String[] args) {
  126. ChannelDAO dao = new ChannelDAOImpl();
  127. Connection conn =null;
  128. try {
  129. conn = Database.getConnection();
  130. dao.setConnection(conn);
  131. Channel channel=new Channel();
  132. channel.setChannelID(new Long(2));
  133. channel.setChannelName("国际新闻1");
  134. channel.setChannelOrder(new Integer(13));
  135. channel.setChannelStatus(new Integer(1));
  136. // dao.addChannel(channel);
  137. dao.updateChannel(channel);
  138. Database.commit();
  139. dao.listAllChannels();
  140. } catch (Exception e) {
  141. // TODO Auto-generated catch block
  142. e.printStackTrace();
  143. }finally{
  144. Database.releaseConnection(conn);
  145. }
  146. }
  147. /* (non-Javadoc)
  148.  * @see com.mycompany.news.dao.ChannelDAO#getChildren(java.lang.Long)
  149.  */
  150. }