KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > forum > Utilities


1 package forum;
2
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5 import java.sql.*;
6 import java.sql.Connection JavaDoc;
7 import java.sql.Statement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9
10 public class Utilities {
11     
12     public static String JavaDoc getTopics(String JavaDoc forum_id){
13         
14         String JavaDoc topics = null;
15         try{
16             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
17             db.connect();
18     
19             ResultSet JavaDoc rs = db.selectQuery(
20                 "SELECT count(*) topics FROM forum_message " +
21                 "WHERE forum_id =\"" + forum_id + "\" " +
22                 "AND reply_id=\"0\"");
23                         
24             while(rs.next()){
25                 topics = rs.getString("topics");
26             }
27             db.close();
28         }catch(Exception JavaDoc e){}
29         
30         return topics;
31     }
32     
33     public static String JavaDoc getforumReplies(String JavaDoc forum_id){
34         
35         String JavaDoc replies = null;
36         try{
37             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
38             db.connect();
39     
40             ResultSet JavaDoc rs = db.selectQuery(
41                 "SELECT count(*)-" + getTopics(forum_id) + " replies FROM forum_message " +
42                 "WHERE forum_id =\"" + forum_id + "\"");
43                         
44             while(rs.next()){
45                 replies = rs.getString("replies");
46             }
47             db.close();
48         }catch(Exception JavaDoc e){}
49         
50         return replies;
51     }
52
53     public static String JavaDoc getReplies(String JavaDoc forum_id,String JavaDoc thread_id){
54         
55         String JavaDoc replies = null;
56         try{
57             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
58             db.connect();
59     
60             ResultSet JavaDoc rs = db.selectQuery(
61                 "SELECT count(*)-1 replies FROM forum_message " +
62                 "WHERE forum_id=\"" + forum_id + "\" AND thread_id =\"" + thread_id + "\"");
63                         
64             while(rs.next()){
65                 replies = rs.getString("replies");
66             }
67             db.close();
68         }catch(Exception JavaDoc e){}
69         
70         return replies;
71     }
72
73     public static String JavaDoc getMorePages(String JavaDoc replies,String JavaDoc forum_id,String JavaDoc thread_id,boolean pages)
74     {
75
76       String JavaDoc morePages = "";
77         if(!((Integer.parseInt(replies)+1) < (Integer.parseInt(Variable.getMessagePerPage()) + 1))){
78             
79             if(pages){morePages += "&nbsp;( ";}
80             
81             for(int i=0;i< (Integer.parseInt(replies)+1);i += (Integer.parseInt(Variable.getMessagePerPage())) ){
82                 morePages +=
83                     "<a HREF=message.jsp?forum_id=" + forum_id + "&thread_id=" + thread_id +
84                     "&start="+ i +
85                     " class=\"pathBarLink\">" + ((i/(Integer.parseInt(Variable.getMessagePerPage())))+1)+ "</a> ";
86             }
87             if(pages){morePages += " )</span>";}
88         }
89         return morePages;
90     }
91     
92     public static String JavaDoc getforumTile(String JavaDoc forum_id){
93         
94         String JavaDoc forumTitle = null;
95         try{
96             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
97             db.connect();
98     
99             ResultSet JavaDoc rs = db.selectQuery(
100                 "SELECT title FROM forum_forums "+
101                 "WHERE forum_id=\""+ forum_id + "\"");
102                         
103             while(rs.next()){
104                 forumTitle = rs.getString("title");
105             }
106             db.close();
107         }catch(Exception JavaDoc e){}
108         
109         return forumTitle;
110     }
111
112     public static String JavaDoc getThreadTile(String JavaDoc forum_id,String JavaDoc thread_id){
113         
114         String JavaDoc threadTitle = null;
115         try{
116             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
117             db.connect();
118     
119             ResultSet JavaDoc rs = db.selectQuery(
120                 "SELECT title FROM forum_threads "+
121                 "WHERE forum_id=\""+ forum_id + "\" " +
122                 "AND thread_id=\"" + thread_id + "\"");
123                         
124             while(rs.next()){
125                 threadTitle = rs.getString("title");
126             }
127             db.close();
128         }catch(Exception JavaDoc e){}
129         
130         return threadTitle;
131     }
132         
133     public static String JavaDoc getforumInfo(String JavaDoc forum_id){
134         
135         String JavaDoc forumInfo = null;
136         try{
137             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
138             db.connect();
139     
140             ResultSet JavaDoc rs = db.selectQuery(
141                 "SELECT forum_info FROM forum_forums "+
142                 "WHERE forum_id=\""+ forum_id + "\"");
143                         
144             while(rs.next()){
145                 forumInfo = rs.getString("forum_info");
146             }
147             db.close();
148         }catch(Exception JavaDoc e){}
149         
150         if(forumInfo.equals(null)){
151             forumInfo = "";
152         }
153         
154         return forumInfo;
155     }
156     
157
158     public static String JavaDoc lastPostInfo(String JavaDoc forum_id){
159         
160         String JavaDoc lastPostInfo = null;
161         String JavaDoc thread_id = null;
162         String JavaDoc title = null;
163         String JavaDoc date_time = null;
164         String JavaDoc user = null;
165         
166         try{
167             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
168             db.connect();
169     
170             ResultSet JavaDoc rs = db.selectQuery(
171                 "SELECT MAX(date_time) date_time FROM forum_message "+
172                 "WHERE forum_id=\""+ forum_id + "\"");
173                         
174             while(rs.next()){
175                 date_time = rs.getString("date_time");
176             }
177                 
178             ResultSet JavaDoc rs2 = db.selectQuery(
179                 "SELECT thread_id,user FROM forum_message "+
180                 "WHERE forum_id=\""+ forum_id + "\" "+
181                 "AND date_time=\"" + date_time + "\"");
182                         
183             while(rs2.next()){
184                 thread_id = rs2.getString("thread_id");
185                 user = rs2.getString("user");
186             }
187                 
188                 
189                                     
190             ResultSet JavaDoc rs3 = db.selectQuery(
191                 "SELECT * FROM forum_threads "+
192                 "WHERE thread_id=\""+ thread_id + "\" "+
193                 "AND forum_id=\"" + forum_id + "\"");
194                         
195             while(rs3.next()){
196                 title = rs3.getString("title");
197             }
198             
199             if(date_time == null){
200                 date_time = "No info";
201             }
202             if(user == null){
203                 user = "No info";
204             }
205             if(title == null){
206                 title = "No info";
207             }
208             
209             lastPostInfo = date_time + "<br>In: " + title + "<br>By: " + user;
210             
211             
212             db.close();
213         }catch(Exception JavaDoc e){}
214                 
215         return lastPostInfo;
216     }
217     
218     public static String JavaDoc lastActionInfo(String JavaDoc forum_id,String JavaDoc thread_id){
219         
220         String JavaDoc lastPostInfo = null;
221         String JavaDoc date_time = null;
222         String JavaDoc user = null;
223         
224         try{
225             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
226             db.connect();
227     
228             ResultSet JavaDoc rs = db.selectQuery(
229                 "SELECT MAX(date_time) date_time FROM forum_message "+
230                 "WHERE forum_id=\""+ forum_id + "\" "+
231                 "AND thread_id=\"" + thread_id + "\"");
232                         
233             while(rs.next()){
234                 date_time = rs.getString("date_time");
235             }
236             
237             ResultSet JavaDoc rs2 = db.selectQuery(
238                 "SELECT user FROM forum_message "+
239                 "WHERE forum_id=\""+ forum_id + "\" "+
240                 "AND thread_id=\"" + thread_id + "\" "+
241                 "AND date_time=\"" + date_time + "\"");
242                         
243             while(rs2.next()){
244                 user = rs2.getString("user");
245             }
246                         
247             if(date_time == null){
248                 date_time = "No info";
249             }
250             if(user == null){
251                 user = "No info";
252             }
253             
254             lastPostInfo = date_time + "<br>Last post by: " + user;
255             
256             
257             db.close();
258         }catch(Exception JavaDoc e){}
259                 
260         return lastPostInfo;
261     }
262     
263     public static void addView(String JavaDoc forum_id,String JavaDoc thread_id){
264
265         try{
266             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
267             db.connect();
268     
269             db.query(
270                 "UPDATE forum_threads SET views = views + 1 "+
271                 "WHERE forum_id =\"" + forum_id + "\" "+
272                 "AND thread_id=\"" + thread_id + "\"");
273
274             db.close();
275         }catch(Exception JavaDoc e){}
276     }
277     
278     public static String JavaDoc getViews(String JavaDoc forum_id,String JavaDoc thread_id){
279         
280         String JavaDoc views = null;
281         try{
282             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
283             db.connect();
284     
285             ResultSet JavaDoc rs = db.selectQuery(
286                 "SELECT views FROM forum_threads "+
287                 "WHERE forum_id=\""+ forum_id + "\" "+
288                 "AND thread_id=\"" + thread_id + "\"");
289                         
290             while(rs.next()){
291                 views = rs.getString("views");
292             }
293             db.close();
294         }catch(Exception JavaDoc e){}
295         
296         return views;
297     }
298     
299     public static int getMessageLength(String JavaDoc forum_id,String JavaDoc thread_id,String JavaDoc reply_id)
300     {
301         int messageLength = 0;
302         try{
303             DBConnectie db = new DBConnectie(Variable.getDb(),Variable.getDbLogin(),Variable.getDbPassword());
304             db.connect();
305             
306             ResultSet JavaDoc rs = db.selectQuery(
307                 "SELECT message FROM forum_message "+
308                 "WHERE forum_id=\""+ forum_id + "\" "+
309                 "AND thread_id=\"" + thread_id + "\" "+
310                 "AND reply_id=\"" + reply_id + "\"");
311                         
312             while(rs.next()){
313                 messageLength = rs.getString("message").length();
314             }
315             db.close();
316         }catch(Exception JavaDoc e){}
317         
318         return messageLength;
319     
320     }
321 }
Popular Tags