Example usage for java.sql ResultSet getStatement

List of usage examples for java.sql ResultSet getStatement

Introduction

In this page you can find the example usage for java.sql ResultSet getStatement.

Prototype

Statement getStatement() throws SQLException;

Source Link

Document

Retrieves the Statement object that produced this ResultSet object.

Usage

From source file:com.runwaysdk.dataaccess.database.general.SQLServer.java

@Override
public boolean groupAttributeIndexExists(String table, String indexName, List<String> attributeColumnNames) {
    String sqlStmt = "sp_helpindex " + table;

    ResultSet resultSet = query(sqlStmt);

    List<String> attributeNameResultList = new LinkedList<String>();

    try {/*from  w ww.  j  ava2 s . c o m*/
        while (resultSet.next()) {
            String attrName = resultSet.getString("index_keys").toLowerCase();
            //      String indexType =  resultSet.getString("index_description").toLowerCase();
            String keyName = resultSet.getString("index_name").toLowerCase();

            // strip whitespace and convert to array
            attrName = attrName.replaceAll(" ", "");
            String[] tempNames = attrName.split(",");

            for (int j = 0; j < tempNames.length; j++) {
                //        if (keyName.equals(indexName) && attributeColumnNames.contains(tempNames[j]) && indexType.contains("unique"))
                if (keyName.equals(indexName) && attributeColumnNames.contains(tempNames[j])) {
                    attributeNameResultList.add(tempNames[j]);
                }
            }
        }

    } catch (SQLException sqlEx1) {
        Database.throwDatabaseException(sqlEx1);
    } finally {
        try {
            java.sql.Statement statement = resultSet.getStatement();
            resultSet.close();
            statement.close();
        } catch (SQLException sqlEx2) {
            Database.throwDatabaseException(sqlEx2);
        }
    }

    if (attributeColumnNames.size() != attributeNameResultList.size()) {
        return false;
    } else {
        return true;
    }

}

From source file:com.runwaysdk.dataaccess.database.general.PostgreSQL.java

/**
 * @see com.runwaysdk.dataaccess.database.Database#uniqueAttributeExists(String,
 *      String, String);/*from www. jav a  2  s . c o  m*/
 */
public boolean uniqueAttributeExists(String table, String columnName, String indexName) {
    String sqlStmt = "SELECT pg_attribute.attname \n" + "  FROM pg_attribute, pg_class \n"
            + " WHERE pg_class.relname = '" + indexName + "' \n"
            + "   AND pg_attribute.attrelid = pg_class.oid";

    ResultSet resultSet = query(sqlStmt);

    boolean returnResult = false;

    try {
        int loopCount = 0;

        if (resultSet.next()) {
            String attrName = resultSet.getString("attname");

            if (attrName.equals(columnName.toLowerCase())) {
                returnResult = true;
            }
            loopCount++;
        }

        if (loopCount != 1) {
            returnResult = false;
        }
    } catch (SQLException sqlEx1) {
        Database.throwDatabaseException(sqlEx1);
    } finally {
        try {
            java.sql.Statement statement = resultSet.getStatement();
            resultSet.close();
            statement.close();
        } catch (SQLException sqlEx2) {
            Database.throwDatabaseException(sqlEx2);
        }
    }

    return returnResult;
}

From source file:com.runwaysdk.dataaccess.database.general.PostgreSQL.java

/**
 * Returns true if a group attribute index exists with the given name and the
 * given attributes on the given table.//from w  ww. j  a va  2 s  . c  o m
 * 
 * @param tableName
 * @param indexName
 * @param attributeColumnNames
 */
public boolean groupAttributeIndexExists(String table, String indexName, List<String> attributeColumnNames) {
    String sqlStmt = "SELECT pg_attribute.attname \n" + "  FROM pg_attribute, pg_class \n"
            + " WHERE pg_class.relname = '" + indexName + "' \n"
            + "   AND pg_attribute.attrelid = pg_class.oid";

    ResultSet resultSet = query(sqlStmt);

    boolean returnResult = true;

    try {
        int resultCount = 0;
        while (resultSet.next()) {
            resultCount++;

            String attrName = resultSet.getString("attname").toLowerCase();
            if (!attributeColumnNames.contains(attrName)) {
                returnResult = false;
            }
        }

        if (resultCount != attributeColumnNames.size()) {
            returnResult = false;
        }
    } catch (SQLException sqlEx1) {
        Database.throwDatabaseException(sqlEx1);
    } finally {
        try {
            java.sql.Statement statement = resultSet.getStatement();
            resultSet.close();
            statement.close();
        } catch (SQLException sqlEx2) {
            Database.throwDatabaseException(sqlEx2);
        }
    }
    return returnResult;
}

From source file:net.geoprism.dashboard.DashboardMap.java

/**
 * Format the bounding box result set returned from a PostGIS database query
 * //w  w  w. j  ava  2 s.  c  om
 * @param resultSet
 * @return
 */
private JSONArray formatBBox(ResultSet resultSet) {
    JSONArray bboxArr = new JSONArray();
    Dashboard dashboard = this.getDashboard();

    try {
        if (resultSet.next()) {
            String bbox = resultSet.getString("bbox");
            if (bbox != null) {
                Pattern p = Pattern.compile("POLYGON\\(\\((.*)\\)\\)");
                Matcher m = p.matcher(bbox);

                if (m.matches()) {
                    String coordinates = m.group(1);
                    List<Coordinate> coords = new LinkedList<Coordinate>();

                    for (String c : coordinates.split(",")) {
                        String[] xAndY = c.split(" ");
                        double x = Double.valueOf(xAndY[0]);
                        double y = Double.valueOf(xAndY[1]);

                        coords.add(new Coordinate(x, y));
                    }

                    Envelope e = new Envelope(coords.get(0), coords.get(2));

                    try {
                        bboxArr.put(e.getMinX());
                        bboxArr.put(e.getMinY());
                        bboxArr.put(e.getMaxX());
                        bboxArr.put(e.getMaxY());
                    } catch (JSONException ex) {
                        throw new ProgrammingErrorException(ex);
                    }
                } else {
                    String error = "The countries of dashboard [" + dashboard.getDisplayLabel().getValue()
                            + "] could not be used to create a valid bounding box";

                    throw new ProgrammingErrorException(error);
                }
            }
        }
    } catch (SQLException sqlEx1) {
        Database.throwDatabaseException(sqlEx1);
    } finally {
        try {
            java.sql.Statement statement = resultSet.getStatement();
            resultSet.close();
            statement.close();
        } catch (SQLException sqlEx2) {
            Database.throwDatabaseException(sqlEx2);
        }
    }

    return bboxArr;
}

From source file:mondrian.olap.Util.java

/**
 * Closes a JDBC result set, statement, and connection, ignoring any errors.
 * If any of them are null, that's fine.
 *
 * <p>If any of them throws a {@link SQLException}, returns the first
 * such exception, but always executes all closes.</p>
 *
 * @param resultSet Result set/*  w  w  w  .j a v a  2s.c  o m*/
 * @param statement Statement
 * @param connection Connection
 */
public static SQLException close(ResultSet resultSet, Statement statement, Connection connection) {
    SQLException firstException = null;
    if (resultSet != null) {
        try {
            if (statement == null) {
                statement = resultSet.getStatement();
            }
            resultSet.close();
        } catch (Throwable t) {
            firstException = new SQLException();
            firstException.initCause(t);
        }
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (Throwable t) {
            if (firstException == null) {
                firstException = new SQLException();
                firstException.initCause(t);
            }
        }
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (Throwable t) {
            if (firstException == null) {
                firstException = new SQLException();
                firstException.initCause(t);
            }
        }
    }
    return firstException;
}

From source file:it.cnr.icar.eric.server.persistence.rdb.RegistryObjectDAO.java

/**
 * Get a HashMap with registry object id as key and owner id as value
 *//*from  w w w .jav  a 2s .c  o m*/
public HashMap<String, String> getOwnersMap(List<String> ids) throws RegistryException {
    Statement stmt = null;
    List<?> resultSets = null;
    HashMap<String, String> ownersMap = new HashMap<String, String>();

    final String prefixPred = "SELECT ao.id, ae.user_, max(concat(ae.timeStamp_, ae.eventType)) FROM AuditableEvent ae, AffectedObject ao WHERE ao.eventId = ae.id";
    final String suffixPred = " AND (ae.eventType = '" + BindingUtility.CANONICAL_EVENT_TYPE_ID_Created
            + "' OR ae.eventType = '" + BindingUtility.CANONICAL_EVENT_TYPE_ID_Versioned
            + "' OR ae.eventType = '" + BindingUtility.CANONICAL_EVENT_TYPE_ID_Deleted + "' OR ae.eventType = '"
            + BindingUtility.CANONICAL_EVENT_TYPE_ID_Relocated + "') GROUP BY ao.id, ae.user_";

    if (ids.size() == 0) {
        return ownersMap;
    }

    try {

        if (ids.size() == 1) {
            // Optmization for 1 term case
            stmt = context.getConnection().createStatement();
            StringBuffer query = new StringBuffer(prefixPred);
            query.append(" AND ao.id = '").append(ids.get(0)).append("'");
            query.append(suffixPred);

            ResultSet rs = stmt.executeQuery(query.toString());

            // contains one line for each id and user with latest event
            while (rs.next()) {

                if (!rs.getString(3).endsWith(BindingUtility.CANONICAL_EVENT_TYPE_ID_Deleted))
                    ownersMap.put(rs.getString(1), rs.getString(2));
            }

            // while (rs.next()) {
            // ownersMap.put(rs.getString(1), rs.getString(2));
            // }
        } else {
            // This will handle unlimited terms using buffered Selects
            StringBuffer query = new StringBuffer(prefixPred);
            query.append(" AND ao.id IN ( $InClauseTerms ) ");
            query.append(suffixPred);

            resultSets = executeBufferedSelectWithINClause(query.toString(), ids, inClauseTermLimit);

            Iterator<?> resultsSetsIter = resultSets.iterator();

            while (resultsSetsIter.hasNext()) {
                ResultSet rs = (ResultSet) resultsSetsIter.next();

                // contains one line for each id and user with latest event
                while (rs.next()) {

                    if (!rs.getString(3).endsWith(BindingUtility.CANONICAL_EVENT_TYPE_ID_Deleted))
                        ownersMap.put(rs.getString(1), rs.getString(2));
                }

                // while (rs.next()) {
                // ownersMap.put(rs.getString(1), rs.getString(2));
                // }
            }
        }

        return ownersMap;
    } catch (SQLException e) {
        log.error(ServerResourceBundle.getInstance().getString("message.CaughtException"), e);
        throw new RegistryException(e);
    } finally {
        if (stmt != null) {
            closeStatement(stmt);
        }

        if (resultSets != null) {
            Iterator<?> resultsSetsIter = resultSets.iterator();
            while (resultsSetsIter.hasNext()) {
                try {
                    ResultSet rs = (ResultSet) resultsSetsIter.next();
                    Statement stmt2 = rs.getStatement();
                    closeStatement(stmt2);
                } catch (SQLException e) {
                    log.error(e, e);
                }
            }
        }
    }
}

From source file:net.geoprism.test.Sandbox.java

public static JSONArray getMapLayersBBox(List<Layer> layers) {

    JSONArray bboxArr = new JSONArray();
    ResultSet resultSet = null;
    String[] layerNames = null;//from   w  ww. j av a2  s  .  c o  m

    // if (layers.size() > 0)
    if (layers.size() < 1) // for testing
    {
        layerNames = new String[layers.size()];
        String sql;

        // if (layers.size() == 1)
        if (layers.size() < 1) // for testing
        {
            // String layer = layers.get(0);
            // String viewName = layers.get;
            // layerNames[0] = layers.get(0);

            String viewName = "aa_test_data_view";

            sql = "SELECT ST_AsText(ST_Extent(" + viewName + "." + GeoserverFacade.GEOM_COLUMN
                    + ")) AS bbox FROM " + viewName;
        } else {
            // More than one layer so union the geometry columns
            sql = "SELECT ST_AsText(ST_Extent(geo_v)) AS bbox FROM (\n";

            for (int i = 0; i < layers.size(); i++) {
                Layer layer = layers.get(i);
                String viewName = layer.getName();
                layerNames[i] = layer.getName();

                sql += "(SELECT " + GeoserverFacade.GEOM_COLUMN + " AS geo_v FROM " + viewName + ") \n";

                if (i != layers.size() - 1) {
                    sql += "UNION \n";
                }
            }

            sql += ") bbox_union";
        }

        resultSet = Database.query(sql);
    }
    try {
        if (resultSet.next()) {

            String bbox = resultSet.getString("bbox");
            if (bbox != null) {
                Pattern p = Pattern.compile("POLYGON\\(\\((.*)\\)\\)");
                Matcher m = p.matcher(bbox);

                if (m.matches()) {
                    String coordinates = m.group(1);
                    List<Coordinate> coords = new LinkedList<Coordinate>();

                    for (String c : coordinates.split(",")) {
                        String[] xAndY = c.split(" ");
                        double x = Double.valueOf(xAndY[0]);
                        double y = Double.valueOf(xAndY[1]);

                        coords.add(new Coordinate(x, y));
                    }

                    Envelope e = new Envelope(coords.get(0), coords.get(2));

                    try {
                        bboxArr.put(e.getMinX());
                        bboxArr.put(e.getMinY());
                        bboxArr.put(e.getMaxX());
                        bboxArr.put(e.getMaxY());
                    } catch (JSONException ex) {
                        throw new ProgrammingErrorException(ex);
                    }
                } else {
                    // There will not be a match if there is a single point geo
                    // entity.
                    // In this case, return the x,y coordinates to OpenLayers.

                    p = Pattern.compile("POINT\\((.*)\\)");
                    m = p.matcher(bbox);
                    if (m.matches()) {
                        String c = m.group(1);
                        String[] xAndY = c.split(" ");
                        double x = Double.valueOf(xAndY[0]);
                        double y = Double.valueOf(xAndY[1]);

                        try {
                            bboxArr.put(x);
                            bboxArr.put(y);
                        } catch (JSONException ex) {
                            throw new ProgrammingErrorException(ex);
                        }
                    } else {
                        String error = "The database view(s) [" + StringUtils.join(layerNames, ",")
                                + "] could not be used to create a valid bounding box";
                        // throw new GeoServerReloadException(error);
                    }
                }
            }
        }

        return bboxArr;
    } catch (SQLException sqlEx1) {
        Database.throwDatabaseException(sqlEx1);
    } finally {
        try {
            java.sql.Statement statement = resultSet.getStatement();
            resultSet.close();
            statement.close();
        } catch (SQLException sqlEx2) {
            Database.throwDatabaseException(sqlEx2);
        }
    }

    // Some problem occured and the bbox couldn't be calculated.
    // Just return the African defaults
    try {
        bboxArr.put(36.718452);
        bboxArr.put(-17.700377000000003);
        bboxArr.put(36.938452);
        bboxArr.put(-17.480376999999997);
    } catch (JSONException ex) {
        throw new ProgrammingErrorException(ex);
    }

    return bboxArr;
}

From source file:com.github.woonsan.jdbc.jcr.impl.JcrJdbcResultSetTest.java

@SuppressWarnings("deprecation")
@Test//from  ww  w  .  j av  a 2 s.  c  om
public void testResultSetWhenClosed() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);

    rs.close();

    try {
        rs.isBeforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isAfterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.isLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.beforeFirst();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.afterLast();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.first();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.last();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.next();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getRow();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getType();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getConcurrency();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowUpdated();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowDeleted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.rowInserted();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getStatement();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.wasNull();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getString("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBoolean("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getByte("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(1, null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("col1", null);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getUnicodeStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream("col1");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getMetaData();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchDirection(1);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchDirection();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.setFetchSize(100);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFetchSize();
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getHoldability();
        fail();
    } catch (SQLException ignore) {
    }

    statement.close();
}

From source file:it.prato.comune.sit.LayerTerritorio.java

/**
 * Chiude il result set risultante da una query diretta alla base di dati.
 * // ww w  .j  a v a2s.  com
 * @param rs
 * @throws SQLException
 */
public void queryDirettaCloseRs(ResultSet rs) throws SQLException {

    try {

        Statement st = rs.getStatement();
        st.close();

        if (rs != null) {
            rs.close();
            rs = null;
        }

        if (st != null) {
            st.close();
            st = null;
        }
    } catch (SQLException e) {
        logger.error("Errore durante chiusura result set in query diretta", e);
        throw e;
    }
}