List of usage examples for java.sql ResultSet last
boolean last() throws SQLException;
ResultSet
object. From source file:com.clustercontrol.sql.factory.RunMonitorSqlString.java
/** * SQL?/*from ww w. jav a 2s . c o m*/ * * @param facilityId ID * @return ???????true */ @Override public boolean collect(String facilityId) { // set Generation Date if (m_now != null) { m_nodeDate = m_now.getTime(); } boolean result = false; AccessDB access = null; ResultSet rSet = null; String url = m_url; try { // ???URL?? if (nodeInfo != null && nodeInfo.containsKey(facilityId)) { Map<String, String> nodeParameter = RepositoryUtil.createNodeParameter(nodeInfo.get(facilityId)); StringBinder strbinder = new StringBinder(nodeParameter); url = strbinder.bindParam(m_url); if (m_log.isTraceEnabled()) m_log.trace("jdbc request. (nodeInfo = " + nodeInfo + ", facilityId = " + facilityId + ", url = " + url + ")"); } // DB?? access = new AccessDB(m_jdbcDriver, url, m_user, m_password); // SQL????? if (m_query.length() >= 6) { String work = m_query.substring(0, 6); if (work.equalsIgnoreCase("SELECT")) { rSet = access.read(m_query); //1?1?? rSet.first(); m_value = rSet.getString(1); //? rSet.last(); int number = rSet.getRow(); NumberFormat numberFormat = NumberFormat.getNumberInstance(); m_messageOrg = MessageConstant.RECORD_VALUE.getMessage() + " : " + m_value + ", " + MessageConstant.RECORDS_NUMBER.getMessage() + " : " + numberFormat.format(number); m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; result = true; } else { //SELECT? m_log.info("collect(): " + MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } } else { //SELECT? m_log.info("collect(): " + MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } } catch (ClassNotFoundException e) { m_log.debug("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_CANNOT_FIND_JDBC_DRIVER.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query + " (" + e.getMessage() + ")"; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } catch (SQLException e) { // SQL m_log.info("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_FAILED_TO_EXECUTE_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query + " (" + e.getMessage() + ")"; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } finally { try { if (rSet != null) { rSet.close(); } if (access != null) { // DB? access.terminate(); } } catch (SQLException e) { m_log.warn("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } } return result; }
From source file:com.clustercontrol.sql.factory.RunMonitorSql.java
/** * SQL?// ww w . j a va 2 s .c o m * * @param facilityId ID * @return ???????true */ @Override public boolean collect(String facilityId) { // set Generation Date if (m_now != null) { m_nodeDate = m_now.getTime(); } boolean result = false; AccessDB access = null; ResultSet rSet = null; String url = m_url; try { // ???URL?? if (nodeInfo != null && nodeInfo.containsKey(facilityId)) { Map<String, String> nodeParameter = RepositoryUtil.createNodeParameter(nodeInfo.get(facilityId)); StringBinder strbinder = new StringBinder(nodeParameter); url = strbinder.bindParam(m_url); if (m_log.isTraceEnabled()) m_log.trace("jdbc request. (nodeInfo = " + nodeInfo + ", facilityId = " + facilityId + ", url = " + url + ")"); } // DB?? access = new AccessDB(m_jdbcDriver, url, m_user, m_password); // SQL????? if (m_query.length() >= 6) { String work = m_query.substring(0, 6); if (work.equalsIgnoreCase("SELECT")) { rSet = access.read(m_query); //1?1?? rSet.first(); double count = rSet.getDouble(1); m_value = count; //? rSet.last(); int number = rSet.getRow(); NumberFormat numberFormat = NumberFormat.getNumberInstance(); m_message = MessageConstant.SELECT_VALUE.getMessage() + " : " + m_value; m_messageOrg = MessageConstant.RECORD_VALUE.getMessage() + " : " + numberFormat.format(m_value) + ", " + MessageConstant.RECORDS_NUMBER.getMessage() + " : " + numberFormat.format(number); m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; result = true; } else { //SELECT? m_log.info("collect(): " + MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } } else { //SELECT? m_log.info("collect(): " + MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_PLEASE_SET_SELECT_STATEMENT_IN_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } } catch (ClassNotFoundException e) { m_log.debug("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_CANNOT_FIND_JDBC_DRIVER.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query + " (" + e.getMessage() + ")"; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } catch (SQLException e) { // SQL m_log.info("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage()); m_unKnownMessage = MessageConstant.MESSAGE_FAILED_TO_EXECUTE_SQL.getMessage(); m_messageOrg = MessageConstant.SQL_STRING.getMessage() + " : " + m_query + " (" + e.getMessage() + ")"; m_messageOrg += "\n" + MessageConstant.CONNECTION_URL.getMessage() + " : " + url; } finally { try { if (rSet != null) { rSet.close(); } if (access != null) { // DB? access.terminate(); } } catch (SQLException e) { m_log.warn("collect() : " + e.getClass().getSimpleName() + ", " + e.getMessage(), e); } } return result; }
From source file:com.commander4j.db.JDBDespatch.java
public Boolean isPalletBatchStatusOK(String despatchNo) { Boolean result = true;/* ww w. j a v a2 s . co m*/ String temp = Common.hostList.getHost(getHostID()).getSqlstatements() .getSQL("JDBDespatch.checkPalletBatchStatus"); // Replace Despatch No temp = StringUtils.replace(temp, "%1", "'" + despatchNo + "'"); // Replace Valid Pallet Status List String ps = lt.getPermittedPalletStatus(); String[] split = StringUtils.split(ps, '^'); String l = ""; for (int cur = 0; cur < split.length; cur++) { l = l + "'" + split[cur] + "'"; if (cur < (split.length - 1)) { l = l + ","; } } temp = StringUtils.replace(temp, "%2", l); // Replace Valid Batch Status List ps = lt.getPermittedBatchStatus(); split = StringUtils.split(ps, '^'); l = ""; for (int cur = 0; cur < split.length; cur++) { l = l + "'" + split[cur] + "'"; if (cur < (split.length - 1)) { l = l + ","; } } temp = StringUtils.replace(temp, "%3", l); PreparedStatement stmt = null; ResultSet rs; try { stmt = Common.hostList.getHost(getHostID()).getConnection(getSessionID()).prepareStatement(temp); stmt.setFetchSize(50); rs = stmt.executeQuery(); rs.last(); int rows = rs.getRow(); rs.beforeFirst(); if (rows > 0) { result = false; } rs.close(); stmt.close(); } catch (SQLException e) { setErrorMessage(e.getMessage()); } return result; }
From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBWriter.java
/** Checks to see if the security exists in the securities table. If it does, return its ID number. If it * does not, add it to the securities table and return its ID number */ private int addSecurity(String security) { Connection conn = null;/* w w w .j a v a 2s . c o m*/ Object[] results = null; int id = -1; try { conn = dbc.getConnection(); String query = "select id from securities where security_name='" + security + "'"; results = dbc.executeQuery(query, conn); //Check if the security is there -- if so, return its id if (results != null) { ResultSet rs = (ResultSet) results[0]; if (!rs.wasNull()) { rs.last(); int size = rs.getRow(); if (size > 0) { return rs.getInt("id"); } } } //dbc.closeQuery(results); //If the security was not found then add it and return the generated id String update = "insert into securities values(0, '" + security + "')"; results = dbc.executeUpdate(update, conn); ResultSet rs = (ResultSet) results[0]; rs.next(); id = rs.getInt(1); } catch (SQLException e) { log.error("Failed to add security " + security + " to securities table", e); return -1; } finally { dbc.closeQuery(results, conn); } return id; }
From source file:edu.caltechUcla.sselCassel.projects.jMarkets.server.data.DBWriter.java
/** Checks to see if the group exists in the market_groups table. If it does, return its ID number. If it * does not, add it to the market_groups table and return its ID number */ private int addGroup(String group) { Connection conn = null;/*w w w. j av a2 s .c o m*/ Object[] results = null; int id = -1; try { conn = dbc.getConnection(); String query = "select id from market_groups where group_name='" + group + "'"; results = dbc.executeQuery(query, conn); //Check if the security is there -- if so, return its id if (results != null) { ResultSet rs = (ResultSet) results[0]; if (!rs.wasNull()) { rs.last(); int size = rs.getRow(); if (size > 0) { id = rs.getInt("id"); dbc.closeQuery(results); return id; } } } dbc.closeQuery(results); //If the group was not found then add it and return the generated id String update = "insert into market_groups values(0, '" + group + "')"; results = dbc.executeUpdate(update, conn); ResultSet rs = (ResultSet) results[0]; rs.next(); return rs.getInt(1); } catch (SQLException e) { log.error("Failed to add group " + group + " to market_groups table", e); } finally { dbc.closeQuery(results, conn); } return id; }
From source file:edu.ku.brc.specify.conversion.BasicSQLUtils.java
/** * Returns the last ID that was inserted into the database * @param connection db connection/* w ww. jav a 2 s. c om*/ * @param tableName the name of the table * @param idColName primary key column name * @return the last ID that was inserted into the database */ public static int getHighestId(final Connection connection, final String idColName, final String tableName) { try { Statement cntStmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = cntStmt .executeQuery("select " + idColName + " from " + tableName + " order by " + idColName + " asc"); int id = 0; if (rs.last()) { id = rs.getInt(1); } else { id = 1; } rs.close(); cntStmt.close(); return id; } catch (SQLException ex) { edu.ku.brc.af.core.UsageTracker.incrSQLUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(BasicSQLUtils.class, ex); log.error(ex); } return -1; }
From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java
/** * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#loadUserAccount(java.lang.String) *//*from w w w. j av a2 s .c o m*/ public synchronized List<Object> loadUserAccount(final String userGuid) throws UserManagementException { final String methodName = SQLUserManager.CNAME + "#loadUserAccount(final String guid) throws UserManagementException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", userGuid); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object> userAccount = null; try { sqlConn = SQLUserManager.dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); stmt = sqlConn.prepareCall("{ CALL loadUserAccount(?) }"); stmt.setString(1, userGuid); // common name if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("ResultSet: {}", resultSet); } if (resultSet.next()) { resultSet.last(); int x = resultSet.getRow(); if (DEBUG) { DEBUGGER.debug("x: {}", x); } if ((x == 0) || (x > 1)) { throw new UserManagementException("No user account was located for the provided data."); } resultSet.first(); userAccount = new ArrayList<Object>( Arrays.asList(resultSet.getString(userAttributes.getCommonName()), resultSet.getString(userAttributes.getUserId()), resultSet.getString(securityAttributes.getLockCount()), resultSet.getString(securityAttributes.getLastLogin()), resultSet.getString(securityAttributes.getExpiryDate()), resultSet.getString(userAttributes.getSurname()), resultSet.getString(userAttributes.getGivenName()), resultSet.getString(userAttributes.getDisplayName()), resultSet.getString(userAttributes.getEmailAddr()), resultSet.getString(userAttributes.getTelephoneNumber()), resultSet.getString(userAttributes.getMemberOf()), resultSet.getString(securityAttributes.getIsSuspended()), resultSet.getString(securityAttributes.getOlrSetupReq()), resultSet.getString(securityAttributes.getOlrLocked()))); if (DEBUG) { DEBUGGER.debug("UserAccount: {}", userAccount); } } } else { throw new UserManagementException("No users were located with the provided information"); } } catch (SQLException sqx) { throw new UserManagementException(sqx.getMessage(), sqx); } finally { try { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if (!(sqlConn == null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } catch (SQLException sqx) { throw new UserManagementException(sqx.getMessage(), sqx); } } return userAccount; }
From source file:net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter.java
/** * @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String, * net.mlw.vlh.ValueListInfo)/*from www. j a va 2s.c o m*/ */ public ValueList getValueList(String name, ValueListInfo info) { if (info.getSortingColumn() == null) { info.setPrimarySortColumn(getDefaultSortColumn()); info.setPrimarySortDirection(getDefaultSortDirectionInteger()); } int numberPerPage = info.getPagingNumberPer(); if (numberPerPage == Integer.MAX_VALUE) { numberPerPage = getDefaultNumberPerPage(); info.setPagingNumberPer(numberPerPage); } Connection connection = null; PreparedStatement statement = null; ResultSet result = null; try { boolean doSqlPaging = ((getAdapterType() & DO_PAGE) == 0); connection = connectionCreator.createConnection(); StringBuffer query = (sqlPagingSupport != null) ? sqlPagingSupport.getPagedQuery(sql) : new StringBuffer(sql); statement = statementBuilder.generate(connection, query, info.getFilters(), sqlPagingSupport == null && doSqlPaging); if (LOGGER.isDebugEnabled()) { LOGGER.debug(query.toString()); } if (showSql) { System.out.println("sql: " + query.toString()); } result = getResultSet(statement, info); if (sqlPagingSupport != null) { PreparedStatement countStatement = null; ResultSet countResult = null; try { StringBuffer countQuery = sqlPagingSupport.getCountQuery(sql); countStatement = statementBuilder.generate(connection, countQuery, info.getFilters(), false); if (showSql) { System.out.println("count sql: " + countQuery.toString()); } countResult = countStatement.executeQuery(); if (countResult.next()) { info.setTotalNumberOfEntries(countResult.getInt(1)); } } finally { JdbcUtil.close(countResult, countStatement, null); } } else if (doSqlPaging) { result.last(); int totalRows = result.getRow(); info.setTotalNumberOfEntries(totalRows); if (numberPerPage == 0) { numberPerPage = getDefaultNumberPerPage(); } int pageNumber = info.getPagingPage(); if (pageNumber > 1) { if ((pageNumber - 1) * numberPerPage > totalRows) { pageNumber = ((totalRows - 1) / numberPerPage) + 1; info.setPagingPage(pageNumber); } } if (pageNumber > 1) { result.absolute((pageNumber - 1) * numberPerPage); } else { result.beforeFirst(); } } List list = processResultSet(name, result, (doSqlPaging) ? numberPerPage : Integer.MAX_VALUE, info); if (!doSqlPaging) { info.setTotalNumberOfEntries(list.size()); } return new DefaultListBackedValueList(list, info); } catch (Exception e) { LOGGER.error(e); throw new RuntimeException(e); } finally { connectionCreator.close(result, statement, connection); } }
From source file:net.sourceforge.msscodefactory.cfcrm.v2_1.CFCrmDb2LUW.CFCrmDb2LUWAuditActionTable.java
public void deleteAuditActionByIdIdx(CFCrmAuthorization Authorization, short argAuditActionId) { final String S_ProcName = "deleteAuditActionByIdIdx"; ResultSet resultSet = null; try {//from w w w. j a v a2s . c om Connection cnx = schema.getCnx(); final String sql = "CALL sp_delete_auditaction_by_ididx( ?, ?, ?, ?, ?" + ", " + "?" + " )"; if (stmtDeleteByIdIdx == null) { stmtDeleteByIdIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByIdIdx.setShort(argIdx++, argAuditActionId); resultSet = stmtDeleteByIdIdx.executeQuery(); if (resultSet.next()) { int deleteFlag = resultSet.getInt(1); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } } else { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected 1 record result set to be returned by delete, not 0 rows"); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } resultSet = null; } } }
From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskDb2LUW.CFAsteriskDb2LUWSysClusterTable.java
public void deleteSysClusterByIdIdx(CFSecurityAuthorization Authorization, int argSingletonId) { final String S_ProcName = "deleteSysClusterByIdIdx"; ResultSet resultSet = null; try {/*from ww w . ja v a2 s . c o m*/ Connection cnx = schema.getCnx(); final String sql = "CALL sp_delete_sysclus_by_ididx( ?, ?, ?, ?, ?" + ", " + "?" + " )"; if (stmtDeleteByIdIdx == null) { stmtDeleteByIdIdx = cnx.prepareStatement(sql); } int argIdx = 1; stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecUserId().toString()); stmtDeleteByIdIdx.setString(argIdx++, (Authorization == null) ? "" : Authorization.getSecSessionId().toString()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecClusterId()); stmtDeleteByIdIdx.setLong(argIdx++, (Authorization == null) ? 0 : Authorization.getSecTenantId()); stmtDeleteByIdIdx.setInt(argIdx++, argSingletonId); resultSet = stmtDeleteByIdIdx.executeQuery(); if (resultSet.next()) { int deleteFlag = resultSet.getInt(1); if (resultSet.next()) { resultSet.last(); throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Did not expect multi-record response, " + resultSet.getRow() + " rows selected"); } } else { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Expected 1 record result set to be returned by delete, not 0 rows"); } } catch (SQLException e) { throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e); } finally { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { } resultSet = null; } } }