Repository navigation
Expand file tree
/
Copy pathReplyInfoDAO.java
More file actions
executable file
·269 lines (254 loc) · 7.74 KB
/
Copy pathReplyInfoDAO.java
File metadata and controls
executable file
·269 lines (254 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.entity.*;
import com.page.*;
public class ReplyInfoDAO {
private BaseDAO dao = new BaseDAO();
private ResultSet rs = null;
/**
* 根据当前版块id,获得同一版块下所有主帖的最后一条跟帖记录
*
* @param sId
* 版块编号
* @param tId
* 主帖编号
* @return IndexPage 返回一个对象
*/
public IndexPage getAllReplyLastTimeById(Integer sId, Integer tId) {
IndexPage index_page = null;
String sql = "select (select uName from userInfo where userInfo.uId= tb.rUid ) as author,rTopic as title,rPublishTime as publishTime,rTid as tid"
+ " from "
+ " (select * from replyInfo where rPublishTime =(select max(rPublishTime) from replyInfo where rSid = ? and rTid = ?)) as tb";
try {
rs = dao.executeQuery(sql, new Object[] { sId, tId });
if (rs != null && rs.next()) {
index_page = new IndexPage();
//
index_page.setAuthor(rs.getString("author"));
index_page.setPublishtime(rs.getTimestamp("publishTime"));
index_page.setTid(rs.getInt("tid"));
index_page.setTitle(rs.getString("title"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return index_page;
}
/**
* 根据主帖id获得跟帖用户信息的集合
*
* @param tid
* 主帖编号
* @return List<DetailPage> 返回一个集合
*/
public List<DetailPage> getReplyInfoAndUserInfoById(Integer tid) {
List<DetailPage> list = null;
String sql = "select rTopic,rContents,rPublishTime,rModifyTime,rId,rUid,"
+ "(select uName from userInfo where userInfo.uid = replyInfo.ruid) as rName,"
+ "(select uFace from userInfo where userInfo.uid = replyInfo.ruid) as rFace,"
+ "(select uRegTime from userInfo where userInfo.uid = replyInfo.ruid) as rRegTime,"
+ "(select uType from userInfo where userInfo.uid = replyInfo.ruid) as rUserType,"
+ "(select uSex from userInfo where userInfo.uid = replyInfo.ruid) as rUsex"
+ " from replyInfo where rtid in (?) order by rPublishTime";
try {
rs = dao.executeQuery(sql, new Object[] { tid });
if (rs != null) {
list = new ArrayList<DetailPage>();
while (rs.next()) {
DetailPage temp = new DetailPage();
//
temp.setContents(rs.getString("rContents"));
temp.setFace(rs.getString("rFace"));
temp.setModifytime(rs.getTimestamp("rModifyTime"));
temp.setName(rs.getString("rName"));
temp.setPublishtime(rs.getTimestamp("rPublishTime"));
temp.setRegtime(rs.getDate("rRegTime"));
temp.setTitle(rs.getString("rTopic"));
temp.setType(rs.getInt("rUserType"));
temp.setSex(rs.getBoolean("rUsex"));
temp.setId(rs.getInt("rId"));
temp.setUid(rs.getInt("rUid"));
//
list.add(temp);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return list;
}
/**
* 根据跟帖id,获得需要修改的信息
*
* @param rid
* 跟帖编号
* @return ReplyInfo 返回一个对象
*/
public ReplyInfo getEditReplyInfoById(Integer rid) {
ReplyInfo replyinfo = null;
String sql = "select * from replyInfo where rid = ?";
String content = "";
try {
rs = dao.executeQuery(sql, new Object[] { rid });
if (rs != null && rs.next()) {
replyinfo = new ReplyInfo();
//
replyinfo.setRmodifytime(rs.getDate("rModifyTime"));
replyinfo.setRpublishtime(rs.getDate("rPublishTime"));
// <br>替换成\r\n
content = rs.getString("rContents").replace("<br>", "\r\n");
content = content.replace(">", ">");
content = content.replace("<", "<");
content = content.replace(" ", " ");
replyinfo.setRcontents(content);
replyinfo.setRid(rs.getInt("rId"));
replyinfo.setRsid(rs.getInt("rSId"));
replyinfo.setRtid(rs.getInt("rTId"));
replyinfo.setRtopic(rs.getString("rTopic"));
replyinfo.setRuid(rs.getInt("rUId"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return replyinfo;
}
/**
* 根据跟帖id更新跟帖标题和内容
*
* @param title
* 标题
* @param content
* 内容
* @param rid
* 跟帖编号
* @return Boolean 返回布尔型 true:表示修改成功
*/
public Boolean updateReplyInfoById(String title, String content, Integer rid) {
int result = -1;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String time = sdf.format(new Date());
// 把\r\n替换成<br>
content = content.replace("<", "<");
content = content.replace(">", ">");
content = content.replace(" ", " ");
content = content.replace("\r\n", "<br>");
String sql = "update replyInfo set rTopic = ?,rContents = ?,rModifyTime = ? where rid = ?";
try {
result = dao.executeUpdate(sql, new Object[] { title, content,
time, rid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 根据跟帖id删除跟帖
*
* @param rid
* 跟帖编号
* @return Boolean 返回布尔型 true:表示删除成功
*/
public Boolean delReplyInfoById(Integer rid) {
int result = -1;
String sql = "delete from replyInfo where rid = ?";
try {
result = dao.executeUpdate(sql, new Object[] { rid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 插入跟帖信息
*
* @param title
* 标题
* @param contents
* 内容
* @param tid
* 主帖编号
* @param sid
* 版块编号
* @param uid
* 用户编号
* @return Boolean 返回布尔型 true:表示插入成功
*/
public Boolean insertReplyInfo(String title, String content, Integer tid,
Integer sid, Integer uid) {
int result = -1;
String sql = "insert into replyInfo(rTid,rSid,rUid,rTopic,rContents)"
+ " values(?,?,?,?,?)";
// 把\r\n替换成<br>
content = content.replace("<", "<");
content = content.replace(">", ">");
content = content.replace(" ", " ");
content = content.replace("\r\n", "<br>");
try {
result = dao.executeUpdate(sql, new Object[] { tid, sid, uid,
title, content });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 根据版块id和主帖id统计主帖的回复数量
*
* @param tid
* 主帖编号
* @param sid
* 版块编号
* @return Boolean 返回布尔型 true:表示插入成功
*/
public Boolean getReplyCountById(Integer tid, Integer sid) {
int result = -1;
String sql = "update topicInfo set tReplyCount = "
+ " (select count(*) as replyCount from replyInfo where rTid = ? and rSid = ?) "
+ " where tid = ?";
try {
result = dao.executeUpdate(sql, new Object[] { tid, sid, tid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
// 测试
public static void main(String[] args) {
ReplyInfoDAO dao = new ReplyInfoDAO();
// Boolean x = dao.insertTopicInfo("d","d",11,1);
// System.out.print(x);
}
}