Example usage for java.sql ResultSet first

List of usage examples for java.sql ResultSet first

Introduction

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

Prototype

boolean first() throws SQLException;

Source Link

Document

Moves the cursor to the first row in this ResultSet object.

Usage

From source file:org.plasma.sdo.jdbc.service.GraphQuery.java

private int countResults(Connection con, Query query, PlasmaType type) {
    int result = 0;
    Object[] params = new Object[0];
    FilterAssembler filterAssembler = null;

    StringBuilder sqlQuery = new StringBuilder();
    AliasMap aliasMap = new AliasMap(type);

    sqlQuery.append("SELECT COUNT(*)");
    sqlQuery.append(aliasMap.getAlias(type));
    Statement statement = null;//from  w  w w.  j a va  2 s  .  c om
    ResultSet rs = null;

    try {
        Where where = query.findWhereClause();
        if (where != null) {
            filterAssembler = new FilterAssembler(where, type, aliasMap);
            sqlQuery.append(" ");
            sqlQuery.append(filterAssembler.getFilter());
            params = filterAssembler.getParams();

            if (log.isDebugEnabled()) {
                log.debug("filter: " + filterAssembler.getFilter());
            }
        } else {
            sqlQuery.append(" FROM ");
            sqlQuery.append(getQualifiedPhysicalName(type));
            sqlQuery.append(" ");
            sqlQuery.append(aliasMap.getAlias(type));
        }
        if (query.getStartRange() != null && query.getEndRange() != null)
            log.warn("query range (start: " + query.getStartRange() + ", end: " + query.getEndRange()
                    + ") ignored for count operation");

        if (log.isDebugEnabled()) {
            log.debug("queryString: " + sqlQuery.toString());
            log.debug("executing...");
        }

        statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        statement.execute(sqlQuery.toString());
        rs = statement.getResultSet();
        rs.first();
        result = rs.getInt(1);
    } catch (Throwable t) {
        StringBuffer buf = this.generateErrorDetail(t, sqlQuery.toString(), filterAssembler);
        log.error(buf.toString());
        throw new DataAccessException(t);
    } finally {
        try {
            if (rs != null)
                rs.close();
            if (statement != null)
                statement.close();
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }
    return result;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public TestCaseExecution findTCExecutionByKey(long id) throws CerberusException {
    TestCaseExecution result = null;//from w  w  w  .j a v  a 2s . c  o  m
    final String query = "SELECT * FROM testcaseexecution exe, application app WHERE exe.application = app.application AND ID = ?";

    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setLong(1, id);

        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = this.loadTestCaseExecutionAndApplicationFromResultSet(resultSet);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return result;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public TestCaseExecution findLastTestCaseExecutionNotPE(String test, String testCase) throws CerberusException {
    TestCaseExecution result = null;//from   w  ww .j  a v a2  s. c  o  m
    StringBuilder query = new StringBuilder();
    query.append("SELECT exe.*  FROM `testcaseexecution` exe ");
    query.append(" WHERE Test = ? and TestCase= ? and ID = ");
    query.append(" (SELECT MAX(ID) from `testcaseexecution` ");
    query.append("WHERE Test= ? and TestCase= ? and ControlStatus!='PE')");

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        preStat.setString(1, test);
        preStat.setString(2, testCase);
        preStat.setString(3, test);
        preStat.setString(4, testCase);

        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return result;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public TestCaseExecution findLastTCExecutionByCriteria(String test, String testcase, String environment,
        String country, String build, String revision) throws CerberusException {
    TestCaseExecution result = null;//from www .jav  a2  s.  c  o m
    final String query = new StringBuffer("SELECT exe.* FROM testcaseexecution exe ")
            .append("WHERE exe.test = ? AND exe.testcase = ? AND exe.environment = ? ")
            .append("AND exe.country = ? AND exe.build = ? AND exe.revision = ? ")
            .append("ORDER BY exe.id DESC").toString();

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setString(1, test);
        preStat.setString(2, testcase);
        preStat.setString(3, environment);
        preStat.setString(4, country);
        preStat.setString(5, build);
        preStat.setString(6, revision);

        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = this.loadFromResultSet(resultSet);
                } else {
                    throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return result;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public TestCaseExecution findLastTCExecutionByCriteria(String test, String testCase, String environment,
        String country, String build, String revision, String browser, String browserVersion, String ip,
        String port, String tag) {
    TestCaseExecution result = null;/* ww w.j  a v  a 2s .  c  om*/
    final String query = new StringBuffer("SELECT exe.* FROM testcaseexecution exe ")
            .append("WHERE exe.test = ? AND exe.testcase = ? ")
            .append("AND exe.environment LIKE ? AND exe.country = ? AND exe.build LIKE ? ")
            .append("AND exe.revision LIKE ? AND exe.browser = ? AND exe.browserfullversion LIKE ? ")
            .append("AND exe.ip LIKE ? AND exe.port LIKE ? AND exe.tag LIKE ? ").append("ORDER BY exe.id DESC")
            .toString();

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        preStat.setString(1, test);
        preStat.setString(2, testCase);
        preStat.setString(3, ParameterParserUtil.wildcardIfEmpty(environment));
        preStat.setString(4, country);
        preStat.setString(5, ParameterParserUtil.wildcardIfEmpty(build));
        preStat.setString(6, ParameterParserUtil.wildcardIfEmpty(revision));
        preStat.setString(7, browser);
        preStat.setString(8, ParameterParserUtil.wildcardIfEmpty(browserVersion));
        preStat.setString(9, ParameterParserUtil.wildcardIfEmpty(ip));
        preStat.setString(10, ParameterParserUtil.wildcardIfEmpty(port));
        preStat.setString(11, ParameterParserUtil.wildcardIfEmpty(tag));

        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = this.loadFromResultSet(resultSet);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return result;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public AnswerItem readByKey(long executionId) {
    AnswerItem ans = new AnswerItem();
    TestCaseExecution result = null;//from   w w w  .  j  a v a2s.c  o m
    final String query = "SELECT * FROM `testcaseexecution` exe WHERE exe.`id` = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setLong(1, executionId);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME)
                            .replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }

    //sets the message
    ans.setResultMessage(msg);
    return ans;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public List<TestCaseExecution> findExecutionsByCampaignNameAndTag(String campaign, String tag)
        throws CerberusException {
    List<TestCaseExecution> campaignTestCaseExecutions = null;
    boolean throwException = false;

    final String query = new StringBuffer("select exe.*, app.* from ( ").append("select exe.* ")
            .append("from testcaseexecution exe ").append("inner join testbatterycontent tbc ")
            .append("on tbc.Test = exe.Test ").append("and tbc.TestCase = exe.TestCase ")
            .append("inner join campaigncontent cc ").append("on cc.testbattery = tbc.testbattery ")
            .append("where tag is not null ").append("and cc.campaign = ? ").append("and tag = ? ")
            .append("order by test, testcase, ID desc) as exe, application app ")
            .append("where exe.application = app.application ")
            .append("group by exe.test, exe.testcase, exe.Environment, exe.Browser, exe.Country ").toString();

    Connection connection = this.databaseSpring.connect();
    try {/*from w  w w .j  a  va 2s . co  m*/
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, campaign);
            preStat.setString(2, tag);

            ResultSet resultSet = preStat.executeQuery();
            try {
                if (!(resultSet.first())) {
                    throwException = true;
                } else {
                    campaignTestCaseExecutions = new ArrayList<TestCaseExecution>();
                    do {
                        campaignTestCaseExecutions.add(this.loadFromResultSet(resultSet));
                    } while (resultSet.next());
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                if (!(resultSet == null)) {
                    resultSet.close();
                }
            }
        } catch (Exception exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (Exception exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwException) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.EXECUTION_FA));
    }

    return campaignTestCaseExecutions;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public List<TestCaseExecution> findExecutionbyCriteria1(String dateLimit, String test, String testCase,
        String application, String country, String environment, String controlStatus, String status)
        throws CerberusException {
    List<TestCaseExecution> myTestCaseExecutions = null;
    TestCaseExecution Execution;//from   w w w .  jav a2 s .  c  om
    boolean throwException = false;
    final String query = new StringBuffer("SELECT exe.*, tec.*, app.* FROM testcaseexecution exe ")
            .append("LEFT JOIN testcase tec ON exe.test = tec.test AND exe.testcase = tec.testcase ")
            .append("LEFT JOIN application app ON exe.application = app.application ")
            .append("WHERE exe.start > ? AND exe.test LIKE ? AND exe.testcase LIKE ? AND exe.environment LIKE ? ")
            .append("AND exe.country LIKE ? AND exe.application LIKE ? AND exe.controlstatus LIKE ? ")
            .append("AND exe.status LIKE ?").toString();

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, dateLimit);
            preStat.setString(2, test);
            preStat.setString(3, testCase);
            preStat.setString(4, environment);
            preStat.setString(5, country);
            preStat.setString(6, application);
            preStat.setString(7, controlStatus);
            preStat.setString(8, status);

            ResultSet resultSet = preStat.executeQuery();
            try {
                if (!(resultSet.first())) {
                    throwException = true;
                } else {
                    myTestCaseExecutions = new ArrayList<TestCaseExecution>();
                    do {
                        Execution = this.loadWithDependenciesFromResultSet(resultSet);

                        myTestCaseExecutions.add(Execution);
                    } while (resultSet.next());
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                if (!(resultSet == null)) {
                    resultSet.close();
                }
            }
        } catch (Exception exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (Exception exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwException) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.EXECUTION_FA));
    }

    return myTestCaseExecutions;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public AnswerItem readLastByCriteria(String application) {
    AnswerItem ans = new AnswerItem();
    TestCaseExecution result = null;//  w  w w.ja v  a2s  . co m
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));

    StringBuilder searchSQL = new StringBuilder();

    StringBuilder query = new StringBuilder();
    //SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that 
    //were applied -- used for pagination p
    query.append("select * from testcaseexecution exe ");

    searchSQL.append(" where 1=1 ");

    if (!StringUtil.isNullOrEmpty(application)) {
        searchSQL.append(" and (`application` = ? )");
    }
    query.append(searchSQL);

    query.append(" order by id DESC limit 1 ");

    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }

    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            if (!StringUtil.isNullOrEmpty(application)) {
                preStat.setString(i++, application);
            }
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME)
                            .replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }

    //sets the message
    ans.setResultMessage(msg);
    return ans;
}

From source file:org.cerberus.crud.dao.impl.TestCaseExecutionDAO.java

@Override
public long insertTCExecution(TestCaseExecution tCExecution) throws CerberusException {
    boolean throwEx = false;
    final String query = "INSERT INTO testcaseexecution(test, testcase, build, revision, environment, environmentData, country, browser, application, ip, "
            + "url, port, tag, verbose, status, start, controlstatus, controlMessage, crbversion, finished, browserFullVersion, executor, screensize,"
            + "conditionOper, conditionVal1Init, conditionVal2Init, conditionVal1, conditionVal2, manualExecution) "
            + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    Connection connection = this.databaseSpring.connect();
    try {//from w  ww .ja va 2  s  .  c  om
        PreparedStatement preStat = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
        try {
            int i = 1;
            preStat.setString(i++, tCExecution.getTest());
            preStat.setString(i++, tCExecution.getTestCase());
            preStat.setString(i++, tCExecution.getBuild());
            preStat.setString(i++, tCExecution.getRevision());
            preStat.setString(i++, tCExecution.getEnvironment());
            preStat.setString(i++, tCExecution.getEnvironmentData());
            preStat.setString(i++, tCExecution.getCountry());
            preStat.setString(i++, tCExecution.getBrowser());
            preStat.setString(i++, tCExecution.getApplicationObj().getApplication());
            preStat.setString(i++, tCExecution.getIp());
            preStat.setString(i++, tCExecution.getUrl());
            preStat.setString(i++, tCExecution.getPort());
            preStat.setString(i++, tCExecution.getTag());
            preStat.setInt(i++, tCExecution.getVerbose());
            preStat.setString(i++, tCExecution.getStatus());
            preStat.setTimestamp(i++, new Timestamp(tCExecution.getStart()));
            preStat.setString(i++, tCExecution.getControlStatus());
            preStat.setString(i++, StringUtil.getLeftString(tCExecution.getControlMessage(), 500));
            preStat.setString(i++, tCExecution.getCrbVersion());
            preStat.setString(i++, tCExecution.getFinished());
            preStat.setString(i++, tCExecution.getBrowserFullVersion());
            preStat.setString(i++, tCExecution.getExecutor());
            preStat.setString(i++, tCExecution.getScreenSize());
            preStat.setString(i++, tCExecution.getConditionOper());
            preStat.setString(i++, tCExecution.getConditionVal1Init());
            preStat.setString(i++, tCExecution.getConditionVal2Init());
            preStat.setString(i++, tCExecution.getConditionVal1());
            preStat.setString(i++, tCExecution.getConditionVal2());
            preStat.setString(i++, tCExecution.isManualExecution() ? "Y" : "N");

            preStat.executeUpdate();
            ResultSet resultSet = preStat.getGeneratedKeys();
            try {
                if (resultSet.first()) {
                    return resultSet.getInt(1);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                throwEx = true;
            } finally {
                resultSet.close();
            }

        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            throwEx = true;
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        throwEx = true;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    if (throwEx) {
        throw new CerberusException(new MessageGeneral(MessageGeneralEnum.EXECUTION_FA));
    }
    return 0;
}