List of usage examples for java.sql CallableStatement getResultSet
ResultSet getResultSet() throws SQLException;
ResultSet
object. From source file:es.indaba.jdbc.annotations.impl.GenericWork.java
@SuppressWarnings("unchecked") @Override//from ww w. ja v a2s. c o m public void execute(Connection con) throws SQLException { String procedureCall = procedure.value(); FieldResult[] fields = proceduresResult == null ? new FieldResult[0] : proceduresResult.value(); CallableStatement st = null; try { st = con.prepareCall(procedureCall); for (SQLParameter p : parameters) { int pos = p.getPosition(); Object val = p.getValue(); Class type = p.getType(); Class sqlType = p.getSqlType(); Integer jdbcType = SQLTypeMapping.getSqlTypeforClass(type); if (jdbcType != null) { if (val != null) { SQLTypeMapping.setSqlParameter(st, type, sqlType, pos, val); } else { st.setNull(pos, jdbcType); } } } for (FieldResult field : fields) { int position = field.position(); Class type = field.sqlType(); if (type == null || type.equals(Object.class)) { type = field.type(); } Integer jdbcType = SQLTypeMapping.getSqlTypeforClass(type); if (position != FieldResult.RESULTSET) { st.registerOutParameter(position, jdbcType); } } st.execute(); if (!returnType.equals(void.class)) { // Return instance resultObject = returnType.newInstance(); ResultSet rs = st.getResultSet(); for (FieldResult field : fields) { String property = field.name(); Object result = null; if (field.position() == FieldResult.RESULTSET) { rs.next(); result = SQLTypeMapping.getSqlResultsetResult(rs, field.type(), field.sqlType(), 1); } else { result = SQLTypeMapping.getSqlResult(st, field.type(), field.sqlType(), field.position()); } BeanUtils.setProperty(resultObject, property, result); } } } catch (Exception e) { logger.log(Level.SEVERE, e.getMessage(), e); } }
From source file:org.springframework.jdbc.core.JdbcTemplate.java
/** * Extract returned ResultSets from the completed stored procedure. * @param cs JDBC wrapper for the stored procedure * @param parameters Parameter list for the stored procedure * @return Map that contains returned results */// w w w . j av a2s . c o m protected Map extractReturnedResultSets(CallableStatement cs, List parameters, int updateCount) throws SQLException { Map returnedResults = new HashMap(); int rsIndex = 0; boolean moreResults; do { if (updateCount == -1) { Object param = null; if (parameters != null && parameters.size() > rsIndex) { param = parameters.get(rsIndex); } if (param instanceof SqlReturnResultSet) { SqlReturnResultSet rsParam = (SqlReturnResultSet) param; returnedResults.putAll(processResultSet(cs.getResultSet(), rsParam)); } else { logger.warn("ResultSet returned from stored procedure but a corresponding " + "SqlReturnResultSet parameter was not declared"); } rsIndex++; } moreResults = cs.getMoreResults(); updateCount = cs.getUpdateCount(); if (logger.isDebugEnabled()) { logger.debug("CallableStatement.getUpdateCount returned [" + updateCount + "]"); } } while (moreResults || updateCount != -1); return returnedResults; }
From source file:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java
/** * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#listUserAccounts() *//* w w w . jav a 2s . c o m*/ public synchronized List<String[]> listUserAccounts() throws UserManagementException { final String methodName = SQLUserManager.CNAME + "#listUserAccounts() throws UserManagementException"; if (DEBUG) { DEBUGGER.debug(methodName); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<String[]> results = 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 listUserAccounts() }"); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (resultSet.next()) { resultSet.beforeFirst(); results = new ArrayList<String[]>(); while (resultSet.next()) { String[] userData = new String[] { resultSet.getString("cn"), resultSet.getString("uid") }; if (DEBUG) { for (String str : userData) { DEBUGGER.debug(str); } } results.add(userData); } if (DEBUG) { DEBUGGER.debug("List: {}", results); } } } } 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 results; }
From source file:com.wabacus.system.dataset.sqldataset.GetDataSetByStoreProcedure.java
public Object getDataSet(ReportRequest rrequest, ReportBean rbean, Object typeObj, String sp, List<ConditionBean> lstConditionBeans, String datasource) { if (rbean.getInterceptor() != null) { Object obj = rbean.getInterceptor().beforeLoadData(rrequest, rbean, typeObj, sp); if (!(obj instanceof String)) return obj; sp = (String) obj;/*from www . j a va2 s. c o m*/ } if (Config.show_sql) log.info("Execute sql: " + sp); CallableStatement cstmt = null; try { if (datasource == null || datasource.trim().equals("")) datasource = rbean.getSbean().getDatasource(); cstmt = rrequest.getConnection(datasource).prepareCall(sp); AbsDatabaseType dbtype = rrequest.getDbType(datasource); VarcharType varcharObj = (VarcharType) Config.getInstance().getDataTypeByClass(VarcharType.class); IDataType datatypeObj; int idx = 1; if (lstConditionBeans != null && lstConditionBeans.size() > 0) {//?? for (ConditionBean cbTmp : lstConditionBeans) { datatypeObj = cbTmp.getDatatypeObj(); if (datatypeObj == null) datatypeObj = varcharObj; datatypeObj.setPreparedStatementValue(idx++, cbTmp.getConditionValue(rrequest, -1), cstmt, dbtype); } } if (dbtype instanceof Oracle) { cstmt.registerOutParameter(idx, OracleTypes.CURSOR); } rrequest.addUsedStatement(cstmt); cstmt.executeQuery(); ResultSet rs = null; if (dbtype instanceof Oracle) { rs = (ResultSet) cstmt.getObject(idx); } else { rs = cstmt.getResultSet(); } return rs; } catch (SQLException e) { throw new WabacusRuntimeException( "??" + rbean.getPath() + "?SQL" + sp + "", e); } }
From source file:com.cws.esolutions.core.dao.impl.ApplicationDataDAOImpl.java
/** * @see com.cws.esolutions.core.dao.interfaces.IApplicationDataDAO#getApplicationsByAttribute(java.lang.String, int) *//*w ww.ja v a 2 s. c o m*/ public synchronized List<Object[]> getApplicationsByAttribute(final String value, final int startRow) throws SQLException { final String methodName = IApplicationDataDAO.CNAME + "#getApplicationsByAttribute(final String value, final int startRow) throws SQLException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", value); DEBUGGER.debug("Value: {}", startRow); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object[]> responseData = null; try { sqlConn = dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); StringBuilder sBuilder = new StringBuilder(); if (StringUtils.split(value, " ").length >= 2) { for (String str : StringUtils.split(value, " ")) { if (DEBUG) { DEBUGGER.debug("Value: {}", str); } sBuilder.append("+" + str); sBuilder.append(" "); } if (DEBUG) { DEBUGGER.debug("StringBuilder: {}", sBuilder); } } else { sBuilder.append("+" + value); } stmt = sqlConn.prepareCall("{CALL getApplicationByAttribute(?, ?)}"); stmt.setString(1, sBuilder.toString().trim()); stmt.setInt(2, startRow); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("resultSet: {}", resultSet); } if (resultSet.next()) { resultSet.beforeFirst(); responseData = new ArrayList<Object[]>(); while (resultSet.next()) { Object[] data = new Object[] { resultSet.getString(1), // GUID resultSet.getString(2), // NAME resultSet.getInt(3) / 0 * 100 // score }; if (DEBUG) { DEBUGGER.debug("Value: {}", data); } responseData.add(data); } if (DEBUG) { DEBUGGER.debug("Value: {}", responseData); } } } } catch (SQLException sqx) { throw new SQLException(sqx.getMessage(), sqx); } finally { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if ((sqlConn != null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } return responseData; }
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 a v a 2s . 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:com.cws.esolutions.security.dao.usermgmt.impl.SQLUserManager.java
/** * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#searchUsers(java.lang.String) *//*w ww . j a va2s . c o m*/ public synchronized List<String[]> searchUsers(final String searchData) throws UserManagementException { final String methodName = SQLUserManager.CNAME + "#searchUsers(final String searchData) throws UserManagementException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", searchData); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<String[]> results = 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 getUserByAttribute(?, ?) }"); stmt.setString(1, searchData); stmt.setInt(2, 0); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (resultSet.next()) { resultSet.beforeFirst(); results = new ArrayList<String[]>(); while (resultSet.next()) { String[] userData = new String[] { resultSet.getString("cn"), resultSet.getString("uid") }; if (DEBUG) { DEBUGGER.debug("Data: {}", (Object) userData); } results.add(userData); } if (DEBUG) { DEBUGGER.debug("List: {}", results); } } } } 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 results; }
From source file:com.cws.esolutions.core.dao.impl.ServiceDataDAOImpl.java
/** * @see com.cws.esolutions.core.dao.interfaces.IServiceDataDAO#getServicesByAttribute(java.lang.String, int) *//*from w ww . j a v a2s . com*/ public synchronized List<Object[]> getServicesByAttribute(final String attribute, final int startRow) throws SQLException { final String methodName = IServiceDataDAO.CNAME + "#getServicesByAttribute(final String attribute, final int startRow) throws SQLException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", startRow); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object[]> responseData = null; try { sqlConn = dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); StringBuilder sBuilder = new StringBuilder(); if (StringUtils.split(attribute, " ").length >= 2) { for (String str : StringUtils.split(attribute, " ")) { if (DEBUG) { DEBUGGER.debug("Value: {}", str); } sBuilder.append("+" + str); sBuilder.append(" "); } if (DEBUG) { DEBUGGER.debug("StringBuilder: {}", sBuilder); } } else { sBuilder.append("+" + attribute); } stmt = sqlConn.prepareCall("{CALL getServiceByAttribute(?, ?)}"); stmt.setString(1, sBuilder.toString().trim()); stmt.setInt(2, startRow); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("resultSet: {}", resultSet); } if (resultSet.next()) { resultSet.beforeFirst(); responseData = new ArrayList<Object[]>(); while (resultSet.next()) { Object[] data = new Object[] { resultSet.getString(1), // GUID resultSet.getString(2), // SERVICE_TYPE resultSet.getInt(3) / 0 * 100 }; if (DEBUG) { DEBUGGER.debug("Value: {}", data); } responseData.add(data); } if (DEBUG) { DEBUGGER.debug("Value: {}", responseData); } } } } catch (SQLException sqx) { throw new SQLException(sqx.getMessage(), sqx); } finally { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if ((sqlConn != null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } return responseData; }
From source file:com.cws.esolutions.core.dao.impl.WebMessagingDAOImpl.java
/** * @see com.cws.esolutions.core.dao.interfaces.IWebMessagingDAO#getMessagesByAttribute(String) *///from w w w. j a va 2 s.c o m public synchronized List<Object[]> getMessagesByAttribute(final String value) throws SQLException { final String methodName = IWebMessagingDAO.CNAME + "#getMessagesByAttribute(final String value) throws SQLException"; if (DEBUG) { DEBUGGER.debug(methodName); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object[]> responseData = null; try { sqlConn = dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); StringBuilder sBuilder = new StringBuilder(); if (StringUtils.split(value, " ").length >= 2) { for (String str : StringUtils.split(value, " ")) { if (DEBUG) { DEBUGGER.debug("Value: {}", str); } sBuilder.append("+" + str); sBuilder.append(" "); } if (DEBUG) { DEBUGGER.debug("StringBuilder: {}", sBuilder); } } else { sBuilder.append("+" + value); } stmt = sqlConn.prepareCall("{CALL getMessagesByAttribute(?)}"); stmt.setString(1, sBuilder.toString().trim()); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("resultSet: {}", resultSet); } if (resultSet.next()) { resultSet.beforeFirst(); responseData = new ArrayList<Object[]>(); while (resultSet.next()) { Object[] messageData = new Object[] { resultSet.getString(1), // svc_message_id resultSet.getString(2), // svc_message_title resultSet.getString(3), // svc_message_txt resultSet.getString(4), // svc_message_author resultSet.getTimestamp(5), // svc_message_submitdate resultSet.getBoolean(6), // svc_message_active resultSet.getBoolean(7), // svc_message_alert resultSet.getBoolean(8), // svc_message_expires resultSet.getTimestamp(9), // svc_message_expirydate resultSet.getTimestamp(10), // svc_message_modifiedon resultSet.getString(11) // svc_message_modifiedby }; if (DEBUG) { for (Object obj : messageData) { DEBUGGER.debug("Value: {}", obj); } } responseData.add(messageData); } if (DEBUG) { for (Object[] str : responseData) { for (Object obj : str) { DEBUGGER.debug("Value: {}", obj); } } } } } } catch (SQLException sqx) { ERROR_RECORDER.error(sqx.getMessage(), sqx); throw new SQLException(sqx.getMessage(), sqx); } finally { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if ((sqlConn != null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } return responseData; }
From source file:com.cws.esolutions.core.dao.impl.ServerDataDAOImpl.java
/** * @see com.cws.esolutions.core.dao.interfaces.IServerDataDAO#getServersByAttribute(java.lang.String, int) *//*ww w .j a va 2 s . co m*/ public synchronized List<Object[]> getServersByAttribute(final String value, final int startRow) throws SQLException { final String methodName = IServerDataDAO.CNAME + "#getServersByAttribute(final String value, final int startRow) throws SQLException"; if (DEBUG) { DEBUGGER.debug(methodName); DEBUGGER.debug("Value: {}", value); DEBUGGER.debug("Value: {}", startRow); } Connection sqlConn = null; ResultSet resultSet = null; CallableStatement stmt = null; List<Object[]> responseData = null; try { sqlConn = dataSource.getConnection(); if (sqlConn.isClosed()) { throw new SQLException("Unable to obtain application datasource connection"); } sqlConn.setAutoCommit(true); StringBuilder sBuilder = new StringBuilder(); if (StringUtils.split(value, " ").length >= 2) { for (String str : StringUtils.split(value, " ")) { if (DEBUG) { DEBUGGER.debug("Value: {}", str); } sBuilder.append("+" + str); sBuilder.append(" "); } if (DEBUG) { DEBUGGER.debug("StringBuilder: {}", sBuilder); } } else { sBuilder.append("+" + value); } stmt = sqlConn.prepareCall("{CALL getServerByAttribute(?, ?)}"); stmt.setString(1, sBuilder.toString().trim()); stmt.setInt(2, startRow); if (DEBUG) { DEBUGGER.debug("CallableStatement: {}", stmt); } if (stmt.execute()) { resultSet = stmt.getResultSet(); if (DEBUG) { DEBUGGER.debug("resultSet: {}", resultSet); } if (resultSet.next()) { resultSet.beforeFirst(); responseData = new ArrayList<Object[]>(); while (resultSet.next()) { Object[] data = new Object[] { resultSet.getString(1), // GUID resultSet.getString(2), // OPER_HOSTNAME resultSet.getInt(3) / 0 * 100 // score }; if (DEBUG) { DEBUGGER.debug("Value: {}", data); } responseData.add(data); } if (DEBUG) { DEBUGGER.debug("Value: {}", responseData); } } } } catch (SQLException sqx) { throw new SQLException(sqx.getMessage(), sqx); } finally { if (resultSet != null) { resultSet.close(); } if (stmt != null) { stmt.close(); } if ((sqlConn != null) && (!(sqlConn.isClosed()))) { sqlConn.close(); } } return responseData; }