/** * Quietly closes the <code>java.sql.PreparedStatement</code> used in a batch execution. The advantage over calling * <code>java.sql.PreparedStatement.close()</code> directly is not having to explicitly handle a checked exception * (<code>java.sql.SQLException</code>). * This method should typically be called in a finally block. So as not to displace any exception (e.g. from a failed * batch execution) that might already be in flight, this method swallows any exception that might arise from * closing the statement. This is generally seen as a worthwhile trade-off, as it much less likely for a close to fail * without a prior failure. * * @param ps <code>java.sql.PreparedStatement</code> with which a batch has been executed. If null, this is a no-op. */ public void closePreparedStatement(PreparedStatement ps) { closeQuietly(ps); }
void cleanStatementCache(Connection connection) { Map<String, PreparedStatement> stmsMap = statementCache.remove(connection); if(stmsMap != null) { //Close prepared statements to release cursors on connection pools for(PreparedStatement stmt : stmsMap.values()) { closeQuietly(stmt); } } } }
public void with(RowListener listener){ try { processRS(listener); } catch(SQLException e) { throw new DBException(e); } finally { //TODO: shouldn't these be closed in the same scope they were created? closeQuietly(rs); closeQuietly(s); } }
/** * Converts stack trace to string. * * @param throwable - throwable to convert. * @return message and stack trace converted to string. */ public static String getStackTraceString(Throwable throwable) { StringWriter sw = null; PrintWriter pw = null; try { sw = new StringWriter(); pw = new PrintWriter(sw); pw.println(throwable.toString()); throwable.printStackTrace(pw); pw.flush(); return sw.toString(); } finally { closeQuietly(pw); closeQuietly(sw); } }
/** * Reads contents of resource fully into a byte array. * * @param resourceName resource name. * @return entire contents of resource as byte array. */ public static byte[] readResourceBytes(String resourceName) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return bytes(is); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
/** * Returns lines of text of a resource as list. * * @param resourceName name of resource * @return list of text lines * @throws java.io.IOException in case of IO error */ public static List<String> getResourceLines(String resourceName) throws IOException { InputStreamReader isreader = null; BufferedReader reader = null; try { isreader = new InputStreamReader(Util.class.getResourceAsStream(resourceName)); reader = new BufferedReader(isreader); List<String> lines = new ArrayList<String>(); String tmp; while ((tmp = reader.readLine()) != null) { lines.add(tmp); } return lines; } finally { closeQuietly(reader); closeQuietly(isreader); } }
private static String clobToString(Clob clob) { Reader r = null; StringWriter sw = null; try { r = clob.getCharacterStream(); sw = new StringWriter(); copyStream(r, sw); return sw.toString(); } catch (Exception e) { throw new ConversionException(e); } finally { closeQuietly(sw); closeQuietly(r); } }
/** * Reads contents of file fully and returns as string. * * @param fileName file name. * @param charset name of supported charset. * @return contents of entire file. */ public static String readFile(String fileName, String charset) { FileInputStream in = null; try { in = new FileInputStream(fileName); return read(in, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(in); } }
/** * Reads file into a byte array. * * @param file file to read. * @return content of file. * @throws java.io.IOException */ public static byte[] read(File file) throws IOException { FileInputStream is = new FileInputStream(file); try { return bytes(is); } finally { closeQuietly(is); } }
/** * Saves content of byte array to file. * * @param path path to file - can be absolute or relative to current. * @param content bytes to save. */ public static void saveTo(String path, byte[] content) { InputStream is = null; try { is = new ByteArrayInputStream(content); saveTo(path, is); } finally { closeQuietly(is); } }
/** * Reads contents of the input stream fully and returns it as byte array. * * @param in InputStream to read from. * @return contents of the input stream fully as byte array * @throws IOException in case of IO error */ public static byte[] bytes(InputStream in) throws IOException { if (in == null) { throw new IllegalArgumentException("input stream cannot be null"); } ByteArrayOutputStream os = null; try { os = new ByteArrayOutputStream(1024); byte[] bytes = new byte[1024]; int len; while ((len = in.read(bytes)) != -1) { os.write(bytes, 0, len); } return os.toByteArray(); } finally { closeQuietly(os); } }
/** * Reads contents of resource fully into a string. * * @param resourceName resource name. * @param charset name of supported charset * @return entire contents of resource as string. */ public static String readResource(String resourceName, String charset) { InputStream is = Util.class.getResourceAsStream(resourceName); try { return read(is, charset); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(is); } }
/** * Reads contents of the input stream fully and returns it as String. * * @param in InputStream to read from. * @param charset name of supported charset to use * @return contents of the input stream fully as String. * @throws IOException in case of IO error */ public static String read(InputStream in, String charset) throws IOException { if (in == null) { throw new IllegalArgumentException("input stream cannot be null"); } InputStreamReader reader = null; try { reader = new InputStreamReader(in, charset); char[] buffer = new char[1024]; StringBuilder sb = new StringBuilder(); int len; while ((len = reader.read(buffer)) != -1) { sb.append(buffer, 0, len); } return sb.toString(); } finally { closeQuietly(reader); } }
/** * Saves content read from input stream into a file. * * @param path path to file. * @param in input stream to read content from. */ public static void saveTo(String path, InputStream in) { if (in == null) { throw new IllegalArgumentException("input stream cannot be null"); } if (path == null) { throw new IllegalArgumentException("path cannot be null"); } FileOutputStream out = null; try { out = new FileOutputStream(path); byte[] bytes = new byte[1024]; int len; while ((len = in.read(bytes)) != -1) { out.write(bytes, 0, len); } out.flush(); } catch (IOException e) { throw new RuntimeException(e); } finally { closeQuietly(out); } }
/** * Converts <code>java.sql.Blob</code> to bytes array. * * @param blob Blob to be converted * @return blob converted to byte array */ public static byte[] toBytes(Blob blob) { InputStream is = null; try { is = blob.getBinaryStream(); return bytes(is); } catch (Exception e) { throw new ConversionException(e); } finally { closeQuietly(is); } }
/** * This method returns the value of the first column of the first row. * If query results have more than one column or more than one row, they will be ignored. * * @param query query * @param params parameters * @return fetched value, or null if query did not fetch anything. */ public Object firstCell(final String query, Object... params) { PreparedStatement ps = null; ResultSet rs = null; try { Object result = null; long start = System.currentTimeMillis(); ps = connection().prepareStatement(query); setParameters(ps, params); rs = ps.executeQuery(); if (rs.next()) { result = rs.getObject(1); } LogFilter.logQuery(LOGGER, query, params, start); return result; } catch (SQLException e) { throw new DBException(query, params, e); } finally { closeQuietly(rs); closeQuietly(ps); } }
throw new DBException(query, params, e); } finally { closeQuietly(rs); closeQuietly(ps);
/** * Executes a raw query and calls instance of <code>RowListener</code> with every row found. * Use this method for very large result sets. * * @param sql raw SQL query. * @param listener client listener implementation for processing individual rows. */ public void find(String sql, RowListener listener) { Statement s = null; ResultSet rs = null; try { s = createStreamingStatement(); rs = s.executeQuery(sql); RowProcessor p = new RowProcessor(rs, s); p.with(listener); } catch (SQLException e) { throw new DBException(sql, null, e); } finally { closeQuietly(rs); closeQuietly(s); } }
/** * Executes DML. Use it for inserts and updates. * * @param query raw DML. * @return number of rows afected by query. */ public int exec(String query){ long start = System.currentTimeMillis(); Statement s = null; try { s = connection().createStatement(); int count = s.executeUpdate(query); LogFilter.logQuery(LOGGER, query, null, start); return count; } catch (SQLException e) { logException("Query failed: " + query, e); throw new DBException(query, null, e); } finally { closeQuietly(s); } }
/** * Executes parametrized DML - will contain question marks as placeholders. * * @param query query to execute - will contain question marks as placeholders. * @param params query parameters. * @return number of records affected. */ public int exec(String query, Object... params){ if(query.indexOf('?') == -1) throw new IllegalArgumentException("query must be parametrized"); long start = System.currentTimeMillis(); PreparedStatement ps = null; try { ps = connection().prepareStatement(query); setParameters(ps, params); int count = ps.executeUpdate(); LogFilter.logQuery(LOGGER, query, params, start); return count; } catch (SQLException e) { logException("Failed query: " + query, e); throw new DBException(query, params, e); } finally { closeQuietly(ps); } }