1 25 26 29 package net.killingar.forum.internal.managers; 30 31 import net.killingar.forum.internal.AccessDeniedException; 32 import net.killingar.forum.internal.AccessLevel; 33 import net.killingar.forum.internal.Quote; 34 import net.killingar.forum.internal.Utils; 35 36 import java.sql.Connection ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Statement ; 40 41 public class QuoteManager extends AbstractManager implements java.io.Serializable 42 { 43 46 public Quote getQuote(long id) throws SQLException 47 { 48 Connection c = null; 49 Statement statement = null; 50 ResultSet result = null; 51 52 try 53 { 54 c = getNewConnection(); 55 statement = c.createStatement(); 56 result = statement.executeQuery("select Body, Origin from Quotes where ID = " + id); 57 Quote r = null; 58 if (result.next()) 59 r = new Quote(id, result.getString(1), result.getString(2)); 60 return r; 61 } 62 finally { closeAll(c, statement, result); } 63 } 64 65 68 public Quote getRandomQuote() throws SQLException 69 { 70 Quote[] quotes = getQuotes(); 71 if (quotes.length == 0) 72 return null; 73 74 return quotes[(int)(Math.random()*(double)quotes.length)]; 75 } 76 77 80 public Quote[] getQuotes() throws SQLException 81 { 82 Connection c = null; 83 Statement statement = null; 84 ResultSet result = null; 85 86 try 87 { 88 c = getNewConnection(); 89 statement = c.createStatement(); 90 result = statement.executeQuery("select ID, Body, Origin from Quotes"); 91 92 java.util.ArrayList v = new java.util.ArrayList (); 93 94 while (result.next()) 95 v.add(new Quote(result.getLong(1), result.getString(2), result.getString(3))); 96 Quote quotes[] = new Quote[v.size()]; 97 v.toArray(quotes); 98 99 return quotes; 100 } 101 finally { closeAll(c, statement, result); } 102 } 103 104 107 public void addQuote(Quote quote) throws SQLException , AccessDeniedException 108 { 109 manager.checkMyAccess(AccessLevel.addQuote); 110 Connection c = null; 111 Statement statement = null; 112 ResultSet result = null; 113 114 try 115 { 116 c = getNewConnection(); 117 statement = c.createStatement(); 118 119 statement.executeUpdate("insert into Quotes (Body, Origin) values('"+Utils.disableSQL(quote.body)+"', '"+Utils.disableSQL(quote.origin)+"')"); 120 } 121 finally { closeAll(c, statement, result); } 122 } 123 124 127 public void removeQuote(long id) throws SQLException , AccessDeniedException 128 { 129 manager.checkMyAccess(AccessLevel.removeQuote); 130 Connection c = null; 131 Statement statement = null; 132 ResultSet result = null; 133 134 try 135 { 136 c = getNewConnection(); 137 statement = c.createStatement(); 138 139 statement.executeUpdate("delete from Quotes where ID = "+id); 140 } 141 finally { closeAll(c, statement, result); } 142 } 143 144 147 public void changeQuote(Quote quote) throws SQLException , AccessDeniedException 148 { 149 manager.checkMyAccess(AccessLevel.changeQuote); 150 Connection c = null; 151 Statement statement = null; 152 ResultSet result = null; 153 154 try 155 { 156 c = getNewConnection(); 157 statement = c.createStatement(); 158 159 statement.executeUpdate("update Quotes set Origin = '"+Utils.disableSQL(quote.origin)+"', Body = '"+Utils.disableSQL(quote.body)+"' where ID = "+quote.ID); 160 } 161 finally { closeAll(c, statement, result); } 162 } 163 } | Popular Tags |