1 package smile.stored; 2 3 import java.util.*; 4 import java.text.*; 5 import java.sql.*; 6 7 15 public abstract class StoredProcedure { 16 17 20 protected ResultSet psResult; 21 22 25 protected Statement psStatement; 26 27 30 protected Connection psConnection; 31 32 35 protected HashMap data = null; 36 37 40 protected String psName = "StoredProcedure"; 41 42 45 protected String action = ""; 46 47 50 protected String targetDataStore; 51 52 55 protected static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 56 57 61 protected final static String queryResultOk = "select 0 as returnValue"; 62 63 70 public abstract void checkParams() throws SQLException; 71 72 74 80 public abstract void checkAction() throws SQLException; 81 82 84 90 public abstract void executeAction() throws SQLException; 91 92 100 public void init(HashMap initData, Connection con) { 101 setConnection(con); 102 data = utils.HashMapToUpperCase(initData); 103 } 104 105 111 public void setTargetDataStore(String pTargetDataStore) { 112 this.targetDataStore = pTargetDataStore; 113 } 114 115 121 public void setConnection(Connection con) { 122 this.psConnection = con; 123 } 124 125 130 public ResultSet getResultSet() { 131 return psResult; 132 } 133 134 140 public void close() throws SQLException { 141 try { 142 if (psResult != null) { 143 psResult.close(); 144 } 145 if (psStatement != null) { 146 psStatement.close(); 147 } 148 } catch (SQLException sqle) { 149 utils.log("!! SQLException when trying to close statement and resultset !!"); 150 throw sqle; 151 } 152 } 153 154 160 public void execute() throws SQLException { 161 checkParams(); 162 checkAction(); 163 executeAction(); 164 return; 165 } 166 167 176 protected void execStatement(String query) throws SQLException { 177 try { 178 psStatement = psConnection.createStatement(); 179 psStatement.execute(query); 180 } catch (SQLException sqle) { 181 throw sqle; 182 } 183 } 184 185 194 protected void execStatementToRS(String query) throws SQLException { 195 try { 196 psStatement = psConnection.createStatement(); 197 psStatement.execute(query); 198 psResult = psStatement.getResultSet(); 199 200 } catch (SQLException sqle) { 201 throw sqle; 202 } 203 } 204 205 214 protected String execStatementToValue(String query) throws SQLException { 215 try { 216 psStatement = psConnection.createStatement(); 217 psStatement.execute(query); 218 psResult = psStatement.getResultSet(); 219 psResult.next(); 220 return (psResult.getString(1)); 221 } catch (SQLException sqle) { 222 throw sqle; 223 } 224 } 225 226 233 protected String doubleQuote(String str) { 234 return utils.replaceString(str, "'", "''"); 235 } 236 237 248 protected String getMax(String tableName, String ColumnName) throws SQLException { 249 String vReturn = ""; 250 String v_getMax = ""; 251 v_getMax += "SELECT MAX(" + ColumnName + ") "; 252 v_getMax += "FROM " + tableName; 253 254 vReturn = execStatementToValue(v_getMax); 255 return (vReturn); 256 } 257 258 } 259 | Popular Tags |