1 4 package org.oddjob.sql; 5 6 import java.io.IOException ; 7 import java.io.ObjectInputStream ; 8 import java.io.ObjectOutputStream ; 9 import java.io.Serializable ; 10 import java.sql.Connection ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 import java.sql.Statement ; 14 import java.util.List ; 15 16 import org.apache.commons.beanutils.RowSetDynaClass; 17 import org.apache.log4j.Logger; 18 19 20 39 public class SqlJob 40 implements Runnable , Serializable { 41 private static final long serialVersionUID = 20051106; 42 43 private static final Logger logger = Logger.getLogger(SqlJob.class); 44 45 50 private String name; 51 52 57 private String sql; 58 59 64 private transient Connection connection; 65 66 71 private List rows; 72 73 78 private int updateCount; 79 80 85 public String getName() { 86 return name; 87 } 88 89 94 public void setName(String name) { 95 this.name = name; 96 } 97 98 103 public void setConnection(Connection connection) { 104 this.connection = connection; 105 } 106 107 112 public Connection getConnection() { 113 return connection; 114 } 115 116 121 public void setSql(String sql) { 122 this.sql = sql; 123 } 124 125 130 public String getSql() { 131 return sql; 132 } 133 134 139 public Object [] getRows() { 140 if (rows == null) { 141 return null; 142 } 143 return rows.toArray(); 144 } 145 146 151 public Object getRow() { 152 if (rows == null) { 153 return null; 154 } 155 if (rows.size() != 1) { 156 return null; 157 } 158 return rows.get(0); 159 } 160 161 166 public int getRowCount() { 167 if (rows == null) { 168 return -1; 169 } 170 return rows.size(); 171 } 172 173 178 public int getUpdateCount() { 179 return updateCount; 180 } 181 182 186 public void run() { 187 if (connection == null) { 188 throw new NullPointerException ("No Connection!"); 189 } 190 if (sql == null) { 191 throw new NullPointerException ("No Query!"); 192 } 193 try { 194 executeWithConnection(connection); 195 } 196 catch (SQLException e) { 197 throw new RuntimeException (e); 198 } 199 finally { 200 try { 201 connection.close(); 202 } catch (SQLException e) { 203 throw new RuntimeException (e); 204 } 205 } 206 } 207 208 209 215 protected void executeWithConnection(Connection connection) throws SQLException { 216 logger.debug("Executing query: " + sql); 217 218 Statement stmt = connection.createStatement(); 219 220 RowSetDynaClass rsdc = null; 221 try { 222 boolean hasResults = stmt.execute(getSql()); 223 updateCount = stmt.getUpdateCount(); 224 if (hasResults) { 225 ResultSet results = stmt.getResultSet(); 226 rsdc = new RowSetDynaClass(results); 227 rows = rsdc.getRows(); 228 } 229 } 230 finally { 231 stmt.close(); 232 } 233 } 234 235 236 237 240 private void writeObject(ObjectOutputStream s) 241 throws IOException { 242 s.defaultWriteObject(); 243 } 244 245 248 private void readObject(ObjectInputStream s) 249 throws IOException , ClassNotFoundException { 250 s.defaultReadObject(); 251 } 252 253 257 public String toString() { 258 if (name == null) { 259 return "Sql"; 260 } 261 return name; 262 } 263 } 264 | Popular Tags |