1 package org.ashkelon.db; 2 6 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.SQLException ; 11 import java.sql.Statement ; 12 import java.util.ArrayList ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.ashkelon.util.Logger; 20 import org.ashkelon.util.StringUtils; 21 22 32 public class DBUtils 33 { 34 40 public static void insert(Connection conn, String tableName, Map fieldInfo) throws SQLException 41 { 42 String cmd = null; 43 if (fieldInfo == null || fieldInfo.isEmpty()) 44 { 45 throw new SQLException ("Cannot insert a record without field information"); 46 } 47 cmd = "INSERT INTO " + tableName; 48 String fieldList = StringUtils.join(fieldInfo.keySet().toArray(), ","); 49 cmd += " (" + fieldList + ")"; 50 cmd += " VALUES (" + StringUtils.join("?", ",", fieldInfo.size()) + ")"; 51 52 PreparedStatement pstmt = conn.prepareStatement(cmd); 53 bind(pstmt, fieldInfo.values().toArray()); 54 pstmt.executeUpdate(); 55 pstmt.close(); 56 } 57 58 63 public static void bind(PreparedStatement pstmt, Object [] parms) throws SQLException 64 { 65 for (int i=0; i<parms.length; i++) 66 { 67 if (parms[i] == null) 68 parms[i] = ""; 69 if (parms[i] instanceof String ) 73 { 74 parms[i] = StringUtils.stripNull((String ) parms[i]); 75 } 76 pstmt.setObject(i+1, parms[i]); 77 } 78 } 79 80 92 public static PreparedStatement select(Connection conn, String tableName, String [] returnFields, Map constraints, String orderBy[]) 93 throws SQLException 94 { 95 String cmd = "SELECT "; 96 if (returnFields == null || returnFields.length == 0) 97 { 98 cmd += "*"; 99 } else 100 { 101 cmd += StringUtils.join(returnFields, ","); 102 } 103 104 cmd += " FROM " + tableName; 105 106 PreparedStatement pstmt = null; 107 108 if (constraints!=null && !constraints.isEmpty()) 109 { 110 cmd += " WHERE "; 111 Set keyset = constraints.keySet(); 112 Iterator itr = keyset.iterator(); 113 ArrayList list = new ArrayList (); 114 while (itr.hasNext()) 115 { 116 list.add(itr.next() + "=?"); 117 } 118 cmd += StringUtils.join(list.toArray(), " AND "); 119 120 pstmt = conn.prepareStatement(cmd); 121 bind(pstmt, constraints.values().toArray()); 122 } 123 124 if (orderBy!=null && orderBy.length>0) 125 { 126 cmd += " ORDER BY " + StringUtils.join(orderBy, ","); 127 } 128 129 if (pstmt == null) 130 pstmt = conn.prepareStatement(cmd); 131 132 return pstmt; 133 } 134 135 public static PreparedStatement select(Connection conn, String tableName, String [] returnFields, Map constraints) 136 throws SQLException 137 { 138 return select(conn, tableName, returnFields, constraints, null); 139 } 140 141 public static PreparedStatement select(Connection conn, String tableName, String [] returnFields, String [] orderBy) 142 throws SQLException 143 { 144 return select(conn, tableName, returnFields, null, orderBy); 145 } 146 147 public static PreparedStatement select(Connection conn, String tableName, Map constraints) throws SQLException 148 { 149 String [] returnFields = {"*"}; 150 return select(conn, tableName, returnFields, constraints, null); 151 } 152 153 public static int getCount(Connection conn, String tableName) throws SQLException 154 { 155 String sql = "select count(*) from " + tableName; 156 Statement stmt = conn.createStatement(); 157 ResultSet rset = stmt.executeQuery(sql); 158 rset.next(); 159 int count = rset.getInt(1); 160 rset.close(); 161 stmt.close(); 162 return count; 163 } 164 165 171 public static boolean exists(Connection conn, String tableName, Map constraints) 172 throws SQLException 173 { 174 PreparedStatement pstmt = select(conn, tableName, constraints); 175 ResultSet rset = pstmt.executeQuery(); 176 boolean found = false; 177 if (rset.next()) 178 { 179 found = true; 180 } 181 rset.close(); 182 pstmt.close(); 183 return found; 184 } 185 186 195 public static Object getObject(Connection conn, String tableName, String targetField, Map constraints) throws SQLException 196 { 197 String [] returnFields = new String [1]; 198 returnFields[0] = targetField; 199 PreparedStatement pstmt = select(conn, tableName, returnFields, constraints); 200 ResultSet rset = pstmt.executeQuery(); 201 Object result = null; 202 if (rset.next()) 203 { 204 result = rset.getObject(1); 205 } 206 rset.close(); 207 pstmt.close(); 208 return result; 209 } 210 211 214 public static int delete(Connection conn, String tableName, Map constraints) throws SQLException 215 { 216 String sql = "DELETE FROM " + tableName + " WHERE "; 217 218 Set keyset = constraints.keySet(); 219 Iterator itr = keyset.iterator(); 220 ArrayList list = new ArrayList (); 221 while (itr.hasNext()) 222 { 223 list.add(itr.next() + "=?"); 224 } 225 sql += StringUtils.join(list.toArray(), " AND "); 226 227 PreparedStatement pstmt = conn.prepareStatement(sql); 228 bind(pstmt, constraints.values().toArray()); 229 int num_deleted = pstmt.executeUpdate(); 230 pstmt.close(); 231 return num_deleted; 232 } 233 234 238 public static void logSQLException(SQLException ex) 239 { 240 Logger log = Logger.getInstance(); 241 log.error("Exception: "+ex.getMessage()); 242 log.error("Error Code: "+ex.getErrorCode()); 243 log.error("SQL State: "+ex.getSQLState()); 244 ex.printStackTrace(log.getWriter()); 245 246 if (ex.getErrorCode() == -1311 || ex.getErrorCode() == -1310) 248 { 249 ex.printStackTrace(); 251 } 252 253 while ((ex = ex.getNextException()) != null) 254 { 255 log.error("Chained sql exception:"); 256 logSQLException(ex); 257 } 258 } 259 260 public static void logSQLException(SQLException ex, String sql) 261 { 262 Logger log = Logger.getInstance(); 263 log.error("Sql Statement: "+sql); 264 logSQLException(ex); 265 } 266 267 274 public static int getNextVal(Connection conn, String sequence) throws SQLException 275 { 276 Statement stmt = conn.createStatement(); 277 String sql = "SELECT " + sequence + ".NEXTVAL FROM DUAL"; 278 ResultSet rset = stmt.executeQuery(sql); 279 int nextVal = rset.getInt(1); 280 rset.close(); 281 stmt.close(); 282 return nextVal; 283 } 284 285 289 public static void submitBatch(Connection conn, List commands) throws SQLException 290 { 291 Logger log = Logger.getInstance(); 292 293 if (commands.isEmpty()) return; 294 Statement stmt = conn.createStatement(); 295 296 try 298 { 299 for (int i=0; i<commands.size(); i++) 300 stmt.addBatch((String ) commands.get(i)); 301 stmt.executeBatch(); 302 } 303 catch (SQLException ex) 304 { 305 if (ex.getMessage().toLowerCase().indexOf("not implemented") == -1) 306 { 307 throw ex; 308 } 309 310 for (int i=0; i<commands.size(); i++) 312 { 313 log.debug((String ) commands.get(i)); 314 stmt.execute((String ) commands.get(i)); 315 } 316 } 317 318 if (!conn.getAutoCommit()) 319 conn.commit(); 320 stmt.close(); 321 322 } 323 324 325 public static Object getObject(Connection conn, String sql) throws SQLException 326 { 327 Statement stmt = conn.createStatement(); 328 ResultSet rset = stmt.executeQuery(sql); 329 Object result = rset.getObject(1); 330 rset.close(); 331 stmt.close(); 332 return result; 333 } 334 335 public static void main(String args[]) throws Exception 337 { 338 Connection conn = DBMgr.getInstance().getConnection(); 339 344 345 351 Map fieldinfo = new HashMap (); 352 fieldinfo.put("id", new Integer (13176)); 353 fieldinfo.put("name", "eitan"); 354 fieldinfo.put("qualifiedname", "eitan suez"); 355 fieldinfo.put("isstatic", new Integer (1)); 357 DBUtils.insert(conn, "member", fieldinfo); 358 359 360 DBMgr.getInstance().releaseConnection(conn); 361 362 363 } 364 365 } 366 | Popular Tags |