1 6 7 package com.quikj.application.web.talk.jspbeans; 8 9 import java.sql.*; 10 import java.util.*; 11 12 16 public class AceCannedMessageServer implements CannedMessageServiceInterface 17 { 18 19 private String jdbcClass; 20 21 22 private String url; 23 24 25 private String user; 26 27 28 private String password; 29 30 private Connection connection = null; 31 32 33 public AceCannedMessageServer() 34 { 35 } 36 37 public String init() 38 { 39 if (getConnection() == null) 40 { 41 return "Could not obtain database connection"; 42 } 43 44 return null; 45 } 46 47 public CannedMessageSummaryElement[] list(String [] groups) 48 { 49 StringBuffer query = new StringBuffer ("select id, description, grp from canned_message_tbl"); 50 51 if (groups.length > 0) 52 { 53 query.append (" where"); 54 } 55 56 for (int i = 0; i < groups.length; i++) 57 { 58 if (i > 0) 59 { 60 query.append(" or"); 61 } 62 63 query.append(" grp = ?"); 64 } 65 66 query.append (" order by grp, description"); 67 try 68 { 69 Connection c = getConnection(); 70 if (c == null) 71 { 72 return null; 73 } 74 75 PreparedStatement pstmt = c.prepareStatement(query.toString()); 76 for (int i = 0; i < groups.length; i++) 77 { 78 pstmt.setString(i+1, groups[i]); 79 } 80 81 ResultSet rslt = pstmt.executeQuery(); 82 ArrayList lst = new ArrayList(); 83 while (rslt.next() == true) 84 { 85 CannedMessageSummaryElement el = new CannedMessageSummaryElement(); 86 el.setId(rslt.getString(1)); 87 el.setDescription(rslt.getString(2)); 88 el.setGroup(rslt.getString(3)); 89 90 lst.add(el); 91 } 92 93 CannedMessageSummaryElement[] els = new CannedMessageSummaryElement[lst.size()]; 94 lst.toArray(els); 95 return els; 96 } 97 catch (SQLException ex) 98 { 99 return null; 100 } 101 } 102 103 public String query(String id, String group) 104 { 105 String query = "select message from canned_message_tbl where id = ? and grp = ?"; 106 107 try 108 { 109 Connection c = getConnection(); 110 if (c == null) 111 { 112 return null; 113 } 114 115 PreparedStatement pstmt = c.prepareStatement(query); 116 pstmt.setString(1, id); 117 pstmt.setString(2, group); 118 ResultSet rslt = pstmt.executeQuery(); 119 120 if (rslt.next() == true) 121 { 122 Blob blob = rslt.getBlob(1); 123 return new String (blob.getBytes(1, (int)blob.length())); 124 } 125 } 126 catch (SQLException ex) 127 { 128 return null; 129 } 130 131 return null; 132 } 133 134 public void setParam(String name, String value) 135 { 136 if (name.equals("jdbc") == true) 137 { 138 jdbcClass = value; 139 } 140 else if (name.equals("url") == true) 141 { 142 url = value; 143 } 144 else if (name.equals("user") == true) 145 { 146 user = value; 147 } 148 else if (name.equals("password") == true) 149 { 150 password = value; 151 } 152 } 153 154 private void exerciseConnection() 155 { 156 Statement st; 157 try 158 { 159 st = connection.createStatement(); 160 st.executeQuery("SELECT 1"); 161 st.close(); 162 } 163 catch (SQLException e) 164 { 165 return; 168 } 169 } 170 171 private Connection getConnection() 172 { 173 try 174 { 175 boolean new_connection = false; 176 if (connection == null) 177 { 178 new_connection = true; 179 } 180 else 181 { 182 exerciseConnection(); 183 if (connection.isClosed() == true) 184 { 185 new_connection = true; 186 } 187 } 188 189 if (new_connection == true) 190 { 191 Class.forName(jdbcClass).newInstance(); 192 connection = DriverManager.getConnection(url, user, password); 193 return connection; 194 } 195 else 196 { 197 return connection; 198 } 199 } 200 catch (Exception ex) 201 { 202 return null; 203 } 204 } 205 206 210 public String getJdbcClass() 211 { 212 return this.jdbcClass; 213 } 214 215 219 public void setJdbcClass(String jdbcClass) 220 { 221 this.jdbcClass = jdbcClass; 222 } 223 224 228 public String getUrl() 229 { 230 return this.url; 231 } 232 233 237 public void setUrl(String url) 238 { 239 this.url = url; 240 } 241 242 246 public String getUser() 247 { 248 return this.user; 249 } 250 251 255 public void setUser(String user) 256 { 257 this.user = user; 258 } 259 260 264 public String getPassword() 265 { 266 return this.password; 267 } 268 269 273 public void setPassword(String password) 274 { 275 this.password = password; 276 } 277 278 public void destroy() 279 { 280 if (getConnection() != null) 281 { 282 try 283 { 284 getConnection().close(); 285 } 286 catch (SQLException ex) 287 { 288 } 289 } 290 } 291 292 } 293 | Popular Tags |