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.BusinessDAOFactory.java

/**
 * Returns a List of BusinessDAO objects that match the criteria specified
 * with the given BusinessDAOQuery./* w  ww .  j  a v a  2  s  .  c o m*/
 * 
 * @param businessDAOquery
 *          specifies criteria.
 * @param cacheStrategy
 *          cacheStrategy that is updated for each result.
 * @return List of BusinessDAO objects that match the criteria specified with
 *         the given BusinessDAOQuery.
 */
public static List<BusinessDAO> queryBusiessDAOs(BusinessDAOQuery businessDAOquery,
        CacheAllBusinessDAOstrategy cacheStrategy) {
    List<BusinessDAO> businessDAOlist = new LinkedList<BusinessDAO>();

    String sqlStmt = businessDAOquery.getSQL();

    Map<String, ColumnInfo> columnInfoMap = businessDAOquery.getColumnInfoMap();
    ResultSet results = Database.query(sqlStmt);

    // ThreadRefactor: get rid of this map.
    // Key: ID of an MdAttribute Value: MdEntity that defines the attribute;
    Map<String, MdEntityDAOIF> definedByMdEntityMap = new HashMap<String, MdEntityDAOIF>();
    // This is map improves performance.
    // Key: type Values: List of MdAttributeIF objects that an instance of the
    // type has.
    Map<String, List<? extends MdAttributeConcreteDAOIF>> mdEntityMdAttributeMap = new HashMap<String, List<? extends MdAttributeConcreteDAOIF>>();

    MdEntityDAOIF rootMdEntityIF = businessDAOquery.getMdEntityIF().getRootMdClassDAO();
    String typeColumnAlias = columnInfoMap.get(rootMdEntityIF.definesType() + "." + EntityDAOIF.TYPE_COLUMN)
            .getColumnAlias();

    Statement statement = null;
    try {
        statement = results.getStatement();
        while (results.next()) {
            String businessDAOtype = results.getString(typeColumnAlias);

            List<? extends MdAttributeConcreteDAOIF> mdAttributeIFList = mdEntityMdAttributeMap
                    .get(businessDAOtype);
            if (mdAttributeIFList == null) {
                mdAttributeIFList = MdElementDAO.getMdElementDAO(businessDAOtype).getAllDefinedMdAttributes();
                mdEntityMdAttributeMap.put(businessDAOtype, mdAttributeIFList);
            }

            BusinessDAO businessDAO = buildObjectFromQuery(businessDAOtype, columnInfoMap, definedByMdEntityMap,
                    mdAttributeIFList, results);

            if (cacheStrategy != null) {
                cacheStrategy.updateCache(businessDAO);
            }
            // instantiate the businessDAO
            businessDAOlist.add(businessDAO);
        }
    } catch (SQLException sqlEx) {
        Database.throwDatabaseException(sqlEx);
    } finally {
        try {
            results.close();
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException sqlEx) {
            Database.throwDatabaseException(sqlEx);
        }
    }

    return businessDAOlist;
}

From source file:com.fer.hr.web.rest.resources.Query2Resource.java

/**
 * Drill through on the query result set.
 * @summary Drill through//from  w w  w.j a v a2s. c o m
 * @param queryName The query name
 * @param maxrows The max rows returned
 * @param position The position
 * @param returns The returned dimensions and levels
 * @return A query result set.
 */
@GET
@Produces({ "application/json" })
@Path("/{queryname}/drillthrough")
public QueryResult drillthrough(@PathParam("queryname") String queryName,
        @QueryParam("maxrows") @DefaultValue("100") Integer maxrows, @QueryParam("position") String position,
        @QueryParam("returns") String returns) {
    if (log.isDebugEnabled()) {
        log.debug("TRACK\t" + "\t/query/" + queryName + "/drillthrough\tGET");
    }
    QueryResult rsc;
    ResultSet rs = null;
    try {
        Long start = (new Date()).getTime();
        if (position == null) {
            rs = thinQueryService.drillthrough(queryName, maxrows, returns);
        } else {
            String[] positions = position.split(":");
            List<Integer> cellPosition = new ArrayList<>();

            for (String p : positions) {
                Integer pInt = Integer.parseInt(p);
                cellPosition.add(pInt);
            }

            rs = thinQueryService.drillthrough(queryName, cellPosition, maxrows, returns);
        }
        rsc = RestUtil.convert(rs);
        Long runtime = (new Date()).getTime() - start;
        rsc.setRuntime(runtime.intValue());

    } catch (Exception e) {
        log.error("Cannot execute query (" + queryName + ")", e);
        String error = ExceptionUtils.getRootCauseMessage(e);
        rsc = new QueryResult(error);

    } finally {
        if (rs != null) {
            Statement statement = null;
            try {
                statement = rs.getStatement();
            } catch (Exception e) {
                throw new SaikuServiceException(e);
            } finally {
                try {
                    rs.close();
                    if (statement != null) {
                        statement.close();
                    }
                } catch (Exception ee) {
                }

            }
        }
    }
    return rsc;

}

From source file:com.fer.hr.web.rest.resources.Query2Resource.java

/**
 * Export the drill through to a CSV file for further analysis
 * @summary Export to CSV/*from  w  w w  .  java2  s  . c  o  m*/
 * @param queryName The query name
 * @param maxrows The max rows
 * @param position The position
 * @param returns The returned dimensions and levels
 * @return A response containing a CSV file
 */
@GET
@Produces({ "text/csv" })
@Path("/{queryname}/drillthrough/export/csv")
public Response getDrillthroughExport(@PathParam("queryname") String queryName,
        @QueryParam("maxrows") @DefaultValue("100") Integer maxrows, @QueryParam("position") String position,
        @QueryParam("returns") String returns) {
    if (log.isDebugEnabled()) {
        log.debug("TRACK\t" + "\t/query/" + queryName + "/drillthrough/export/csv (maxrows:" + maxrows
                + " position" + position + ")\tGET");
    }
    ResultSet rs = null;

    try {
        if (position == null) {
            rs = thinQueryService.drillthrough(queryName, maxrows, returns);
        } else {
            String[] positions = position.split(":");
            List<Integer> cellPosition = new ArrayList<>();

            for (String p : positions) {
                Integer pInt = Integer.parseInt(p);
                cellPosition.add(pInt);
            }

            rs = thinQueryService.drillthrough(queryName, cellPosition, maxrows, returns);
        }
        byte[] doc = thinQueryService.exportResultSetCsv(rs);
        String name = SaikuProperties.webExportCsvName;
        return Response.ok(doc, MediaType.APPLICATION_OCTET_STREAM)
                .header("content-disposition", "attachment; filename = " + name + "-drillthrough.csv")
                .header("content-length", doc.length).build();

    } catch (Exception e) {
        log.error("Cannot export drillthrough query (" + queryName + ")", e);
        return Response.serverError().build();
    } finally {
        if (rs != null) {
            try {
                Statement statement = rs.getStatement();
                statement.close();
                rs.close();
            } catch (SQLException e) {
                throw new SaikuServiceException(e);
            } finally {
            }
        }
    }

}

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

@Test
public void testExecuteSQLQuery() throws Exception {
    Statement statement = getConnection().createStatement();
    ResultSet rs = statement.executeQuery(SQL_EMPS);
    assertSame(statement, rs.getStatement());

    assertEquals(ResultSet.TYPE_FORWARD_ONLY, rs.getType());
    assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    assertEquals(ResultSet.HOLD_CURSORS_OVER_COMMIT, rs.getHoldability());

    assertFalse(rs.isClosed());//from   w ww. j a va2s. com
    assertTrue(rs.isBeforeFirst());
    assertFalse(rs.isAfterLast());

    assertEquals(1, rs.findColumn("empno"));
    assertEquals(2, rs.findColumn("ename"));
    assertEquals(3, rs.findColumn("salary"));
    assertEquals(4, rs.findColumn("hiredate"));

    int count = printResultSet(rs);

    assertEquals(getEmpRowCount(), count);
    assertFalse(rs.isBeforeFirst());
    assertTrue(rs.isAfterLast());
    rs.close();
    assertTrue(rs.isClosed());

    statement.close();
    assertTrue(statement.isClosed());
}

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

/**
 * Returns true if a column with the given name exists on the table with the given name, false otherwise.
 *
 * @param columnName assumes column name is lower case.
 * @param tableName//from  w  w w .  ja v  a  2  s. co m
 *
 * @return true if a column with the given name exists on the table with the given name, false otherwise.
 */
@Override
public boolean columnExists(String columnName, String tableName) {
    String sqlStmt = " SELECT column_name AS field \n" + "   FROM information_schema.columns \n"
            + "  WHERE table_name = '" + tableName + "' \n" + "    AND column_name = '" + columnName + "'";
    ResultSet resultSet = query(sqlStmt);

    try {
        while (resultSet.next()) {
            return true;
        }
    } 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 false;
}

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

/**
 * Returns true if a table with the given name already exists in the database,
 * false otherwise.//from   w w  w.  j a  va 2s .co  m
 * 
 * <br/>
 * <b>Precondition:</b> tableName != null <br/>
 * <b>Precondition:</b> !tableName.trim().equals("")
 * 
 * @param tableName
 *          name of a table in the database
 * @return true if a table with the given name already exists in the database,
 *         false otherwise.
 */
public boolean tableExists(String tableName) {
    String sqlStmt = "show tables like '" + tableName + "'";

    ResultSet resultSet = query(sqlStmt);

    boolean returnResult = false;

    try {
        if (resultSet.next()) {
            returnResult = true;
        }
    } 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.SQLServer.java

@Override
public boolean tableExists(String tableName) {
    String sqlStmt = "SELECT * FROM information_schema.tables " + "WHERE table_name = '" + tableName + "'";

    ResultSet resultSet = query(sqlStmt);

    boolean returnResult = false;

    try {/*from  w  w  w  .  j a  va 2 s .  co m*/
        if (resultSet.next()) {
            returnResult = true;
        }
    } 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.MySQL.java

/**
 * Returns a List containing the fields in a table.
 * //from  w w  w .j  av a 2 s. co m
 * @param table
 *          The table to get the field list from.
 * @return The List of the fields in the table.
 */
public List<String> getColumnNames(String tableName) {
    ResultSet resultSet = query("desc " + tableName);
    LinkedList<String> fields = new LinkedList<String>();

    try {
        while (resultSet.next()) {
            fields.add((String) resultSet.getString("field"));
        }
    } 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 fields;
}

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

/**
 * Returns true if a column with the given name exists on the table with the
 * given name, false otherwise.//from   ww w . j a v  a2  s  .  c  o m
 * 
 * @param columnName
 *          assumes column name is lower case.
 * @param tableName
 * 
 * @return true if a column with the given name exists on the table with the
 *         given name, false otherwise.
 */
@Override
public boolean columnExists(String columnName, String tableName) {
    ResultSet resultSet = query("desc " + tableName);

    try {
        while (resultSet.next()) {
            if (resultSet.getString("field").toLowerCase().equals(columnName)) {
                return true;
            }
        }
    } 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 false;
}

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

/**
 * @see com.runwaysdk.dataaccess.database.relationship.AbstractDatabase#getChildCountForParent(java.lang.String,
 *      java.lang.String)// w  w  w  .  ja  v  a2 s.  c om
 */
public long getChildCountForParent(String parent_id, String relationshipTableName) {
    String query = " SELECT COUNT(*) AS CT \n" + " FROM " + relationshipTableName + " \n" + " WHERE "
            + RelationshipDAOIF.PARENT_ID_COLUMN + " = '" + parent_id + "' \n" + " AND "
            + RelationshipDAOIF.CHILD_ID_COLUMN + " IN " + "   (SELECT DISTINCT "
            + RelationshipDAOIF.CHILD_ID_COLUMN + " \n" + "    FROM " + relationshipTableName + " \n"
            + "    WHERE " + RelationshipDAOIF.PARENT_ID_COLUMN + " = '" + parent_id + "')";

    ResultSet resultSet = this.query(query);

    long returnValue = 0;

    try {
        if (resultSet.next()) {
            Long number = (Long) resultSet.getLong("ct");
            returnValue = number.longValue();
        }
    } 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 returnValue;
}