List of usage examples for java.sql PreparedStatement setQueryTimeout
void setQueryTimeout(int seconds) throws SQLException;
Statement
object to execute to the given number of seconds. From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public Map<String, Object> queryForMap(final String sql, final Object... args) { logSql(sql, args);//from w w w. ja v a 2 s . c om return execute(new IConnectionCallback<Map<String, Object>>() { @SuppressWarnings("resource") public Map<String, Object> execute(Connection con) throws SQLException { Map<String, Object> result = null; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setQueryTimeout(settings.getQueryTimeout()); if (args != null && args.length > 0) { setValues(ps, args); } rs = ps.executeQuery(); if (rs.next()) { ResultSetMetaData meta = rs.getMetaData(); int colCount = meta.getColumnCount(); result = new LinkedCaseInsensitiveMap<Object>(colCount); for (int i = 1; i <= colCount; i++) { String key = meta.getColumnName(i); Object value = rs.getObject(i); if (value instanceof Blob) { Blob blob = (Blob) value; try { value = IOUtils.toByteArray(blob.getBinaryStream()); } catch (IOException e) { throw new IoException(e); } } else if (value instanceof Clob) { Clob clob = (Clob) value; try { value = IOUtils.toByteArray(clob.getCharacterStream()); } catch (IOException e) { throw new IoException(e); } } else if (value != null) { Class<?> clazz = value.getClass(); Class<?> superClazz = clazz.getSuperclass(); if (superClazz != null && superClazz.getName().equals("oracle.sql.Datum")) { try { Method method = superClazz.getMethod("toJdbc"); value = method.invoke(value); } catch (Exception e) { throw new IllegalStateException(e); } } } result.put(key, value); } } } finally { close(rs); close(ps); } return result; } }); }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
public int update(final String sql, final Object[] args, final int[] types) { logSql(sql, args);//from w w w . ja v a 2s. c o m return execute(new IConnectionCallback<Integer>() { public Integer execute(Connection con) throws SQLException { if (args == null) { Statement stmt = null; try { stmt = con.createStatement(); stmt.setQueryTimeout(settings.getQueryTimeout()); stmt.execute(sql); return stmt.getUpdateCount(); } finally { close(stmt); } } else { PreparedStatement ps = null; try { ps = con.prepareStatement(sql); ps.setQueryTimeout(settings.getQueryTimeout()); if (types != null) { setValues(ps, args, types, getLobHandler().getDefaultHandler()); } else { setValues(ps, args); } ps.execute(); return ps.getUpdateCount(); } finally { close(ps); } } } }); }
From source file:org.jumpmind.db.sql.JdbcSqlTemplate.java
protected long insertWithGeneratedKey(Connection conn, String sql, String column, String sequenceName, Object[] args, int[] types) throws SQLException { long key = 0; PreparedStatement ps = null; try {// w w w . j a va2 s .c o m boolean supportsGetGeneratedKeys = supportsGetGeneratedKeys(); boolean supportsReturningKeys = supportsReturningKeys(); if (allowsNullForIdentityColumn()) { if (supportsGetGeneratedKeys) { ps = conn.prepareStatement(sql, new int[] { 1 }); } else if (supportsReturningKeys) { ps = conn.prepareStatement(sql + " returning " + column); } else { ps = conn.prepareStatement(sql); } } else { String replaceSql = sql.replaceFirst("\\([\"|\\w]*,", "(").replaceFirst("\\(null,", "("); if (supportsGetGeneratedKeys) { ps = conn.prepareStatement(replaceSql, Statement.RETURN_GENERATED_KEYS); } else { ps = conn.prepareStatement(replaceSql); } } ps.setQueryTimeout(settings.getQueryTimeout()); setValues(ps, args, types, lobHandler.getDefaultHandler()); ResultSet rs = null; if (supportsGetGeneratedKeys) { ps.execute(); try { rs = ps.getGeneratedKeys(); if (rs.next()) { key = rs.getLong(1); } } finally { close(rs); } } else if (supportsReturningKeys) { try { rs = ps.executeQuery(); if (rs.next()) { key = rs.getLong(1); } } finally { close(rs); } } else { Statement st = null; ps.execute(); try { st = conn.createStatement(); rs = st.executeQuery(getSelectLastInsertIdSql(sequenceName)); if (rs.next()) { key = rs.getLong(1); } } finally { close(rs); close(st); } } } finally { close(ps); } return key; }
From source file:org.jumpmind.db.sql.JdbcSqlTransaction.java
public <T> List<T> query(final String sql, final ISqlRowMapper<T> mapper, final Object[] args, final int[] types) { return executeCallback(new IConnectionCallback<List<T>>() { public List<T> execute(Connection c) throws SQLException { PreparedStatement st = null; ResultSet rs = null;//from ww w .j ava 2 s. c o m try { logSql(sql, args); st = c.prepareStatement(sql); st.setQueryTimeout(jdbcSqlTemplate.getSettings().getQueryTimeout()); if (args != null) { jdbcSqlTemplate.setValues(st, args, types, jdbcSqlTemplate.getLobHandler().getDefaultHandler()); } st.setFetchSize(jdbcSqlTemplate.getSettings().getFetchSize()); rs = st.executeQuery(); List<T> list = new ArrayList<T>(); while (rs.next()) { Row row = JdbcSqlReadCursor.getMapForRow(rs, jdbcSqlTemplate.getSettings().isReadStringsAsBytes()); T value = mapper.mapRow(row); list.add(value); } return list; } finally { JdbcSqlTemplate.close(rs); JdbcSqlTemplate.close(st); } } }); }
From source file:org.mule.transport.jdbc.ExtendedQueryRunner.java
@Override protected PreparedStatement prepareStatement(Connection conn, String sql) throws SQLException { PreparedStatement statement = super.prepareStatement(conn, sql); if (this.queryTimeout >= 0) { statement.setQueryTimeout(this.queryTimeout); }/*from w w w. j a va 2 s .com*/ return statement; }
From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java
@Override public boolean shareNameAvailable(String shareName) throws ShareManagerException, UnrecoverableKeyException, ShareMetaDataException { logger.debug("ShareManager : isShareNameAvailable(" + shareName + ")"); ResultSet rs = null;/*w ww .j a va2s.c om*/ String sql = "select * from " + TABLE_SHARES + " where name=?"; PreparedStatement pstatement = null; try { pstatement = connection.prepareStatement(sql); pstatement.setString(1, shareName); pstatement.setQueryTimeout(30); // set timeout to 30 sec. rs = pstatement.executeQuery(); if (rs.next()) { return false; } else { return true; } } catch (SQLException ex) { throw new ShareManagerException("Failed to run shareNameAvailable: ", ex); } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } try { if (pstatement != null) pstatement.close(); } catch (Exception e) { } } }
From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java
@Override public PanboxShare getShareForName(String shareName) throws ShareDoesNotExistException, ShareManagerException, UnrecoverableKeyException, ShareMetaDataException { logger.debug("ShareManager : getShareForName(" + shareName + ")"); ResultSet rs = null;/*from ww w . j a v a 2s . co m*/ String sql = "select * from " + TABLE_SHARES + " where name=?"; PreparedStatement pstatement = null; try { pstatement = connection.prepareStatement(sql); pstatement.setString(1, shareName); pstatement.setQueryTimeout(30); // set timeout to 30 sec. rs = pstatement.executeQuery(); if (rs.next()) { String shareType = rs.getString("type"); String shareURL = rs.getString("backendURL"); String shareUUID = rs.getString("uuid"); try { PanboxShare share = nameTypeUrlToVolumeData(shareName, shareURL, getShareType(shareType), UUID.fromString(shareUUID)); return share; } catch (FileNotFoundException ex) { throw new ShareInaccessibleException("A share with the specified share name (" + shareName + ") does exist, but is not accessible anymore!", ex); } } else { throw new ShareDoesNotExistException( "A share with the specified share name (" + shareName + ") does not exist."); } } catch (SQLException | IOException ex) { throw new ShareManagerException("Failed to run getShareForName: ", ex); } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } try { if (pstatement != null) pstatement.close(); } catch (Exception e) { } } }
From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java
@Override public PanboxShare getShareForPath(String sharePath) throws UnrecoverableKeyException, ShareManagerException, ShareMetaDataException { logger.debug("ShareManager: getShareForPath(" + sharePath + ")"); ResultSet rs = null;/*from w w w . j a v a 2 s.c o m*/ String sql = "select * from " + TABLE_SHARES + " where backendURL=?"; PreparedStatement pstatement = null; try { pstatement = connection.prepareStatement(sql); pstatement.setString(1, sharePath); pstatement.setQueryTimeout(30); // set timeout to 30 sec. rs = pstatement.executeQuery(); if (rs.next()) { String shareName = rs.getString("name"); String shareType = rs.getString("type"); String shareUUID = rs.getString("uuid"); try { PanboxShare share = nameTypeUrlToVolumeData(shareName, sharePath, getShareType(shareType), UUID.fromString(shareUUID)); return share; } catch (FileNotFoundException ex) { throw new ShareInaccessibleException("A share with the specified share path (" + sharePath + ") does exist, but is not accessible anymore!", ex); } } else { logger.warn("A share with the specified share path (" + sharePath + ") does not exist."); return null; } } catch (SQLException | IOException ex) { throw new ShareManagerException("Failed to run getShareForPath: ", ex); } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } try { if (pstatement != null) pstatement.close(); } catch (Exception e) { } } }
From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java
@Override public boolean sharePathAvailable(String path) throws UnrecoverableKeyException, ShareManagerException, ShareMetaDataException { logger.debug("ShareManager : isSharePathAvailable(" + path + ")"); ResultSet rs = null;/*from ww w . java 2s . c om*/ String sql = "select * from " + TABLE_SHARES + " where backendURL=?"; PreparedStatement pstatement = null; try { pstatement = connection.prepareStatement(sql); pstatement.setString(1, path); pstatement.setQueryTimeout(30); // set timeout to 30 sec. rs = pstatement.executeQuery(); if (rs.next()) { return false; } else { return true; } } catch (SQLException ex) { throw new ShareManagerException("Failed to run sharePathAvailable: ", ex); } finally { try { if (rs != null) rs.close(); } catch (Exception e) { } try { if (pstatement != null) pstatement.close(); } catch (Exception e) { } } }
From source file:org.panbox.desktop.common.sharemgmt.ShareManagerImpl.java
@Override public void removeShareFromDB(String shareName) throws ShareManagerException { logger.debug("ShareManager : removeShareFromDB(" + shareName + ")"); PreparedStatement pstatement = null; String sql = "DELETE from " + TABLE_SHARES + " WHERE name=?;"; try {//from w ww . j a v a 2s .c o m pstatement = connection.prepareStatement(sql); pstatement.setString(1, shareName); pstatement.setQueryTimeout(30); // set timeout to 30 sec. pstatement.executeUpdate(); } catch (SQLException ex) { throw new ShareManagerException("Failed to run SQL command removeShareFromDB: ", ex); } finally { try { if (pstatement != null) pstatement.close(); } catch (Exception e) { } } }