List of usage examples for java.sql CallableStatement getString
String getString(String parameterName) throws SQLException;
CHAR
, VARCHAR
, or LONGVARCHAR
parameter as a String
in the Java programming language. From source file:com.wabacus.system.component.application.report.configbean.editablereport.StoreProcedureActionBean.java
public void updateData(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues) throws SQLException { AbsDatabaseType dbtype = rrequest.getDbType(this.ownerGroupBean.getDatasource()); Connection conn = rrequest.getConnection(this.ownerGroupBean.getDatasource()); CallableStatement cstmt = null; try {//from w w w. j av a2 s . co m if (Config.show_sql) log.info("Execute sql:" + sql); cstmt = conn.prepareCall(sql); if (lstParams != null && lstParams.size() > 0) { int idx = 1; IDataType varcharTypeObj = Config.getInstance().getDataTypeByClass(VarcharType.class); EditableReportParamBean paramBeanTmp; for (Object paramObjTmp : this.lstParams) { if (paramObjTmp instanceof EditableReportParamBean) { paramBeanTmp = (EditableReportParamBean) paramObjTmp; paramBeanTmp.getDataTypeObj().setPreparedStatementValue(idx++, getParamValue(mRowData, mParamValues, rbean, rrequest, paramBeanTmp), cstmt, dbtype); } else { varcharTypeObj.setPreparedStatementValue(idx++, paramObjTmp == null ? "" : String.valueOf(paramObjTmp), cstmt, dbtype); } } } int outputindex = -1; if (this.returnValueParamname != null && !this.returnValueParamname.trim().equals("")) { outputindex = this.lstParams == null ? 1 : this.lstParams.size() + 1; cstmt.registerOutParameter(outputindex, java.sql.Types.VARCHAR); } cstmt.execute(); if (outputindex > 0) { String rtnVal = cstmt.getString(outputindex); storeReturnValue(rrequest, mParamValues, rtnVal); } } finally { WabacusAssistant.getInstance().release(null, cstmt); } }
From source file:nl.nn.adapterframework.jdbc.JdbcQuerySenderBase.java
protected String executeOtherQuery(Connection connection, String correlationID, PreparedStatement statement, String message, ParameterResolutionContext prc, ParameterList newParamList) throws SenderException { ResultSet resultset = null;/*w w w. j av a2 s .co m*/ try { int numRowsAffected = 0; if (StringUtils.isNotEmpty(getRowIdSessionKey())) { CallableStatement cstmt = getCallWithRowIdReturned(connection, correlationID, message); int ri = 1; if (prc != null && paramList != null) { ParameterValueList parameters = prc.getValues(newParamList); applyParameters(cstmt, parameters); ri = parameters.size() + 1; } cstmt.registerOutParameter(ri, Types.VARCHAR); log.debug(getLogPrefix() + "executing a SQL command"); numRowsAffected = cstmt.executeUpdate(); String rowId = cstmt.getString(ri); if (prc != null) prc.getSession().put(getRowIdSessionKey(), rowId); } else { log.debug(getLogPrefix() + "executing a SQL command"); numRowsAffected = statement.executeUpdate(); } if (StringUtils.isNotEmpty(getResultQuery())) { Statement resStmt = null; try { resStmt = connection.createStatement(); log.debug("obtaining result from [" + getResultQuery() + "]"); ResultSet rs = resStmt.executeQuery(getResultQuery()); return getResult(rs); } finally { if (resStmt != null) { resStmt.close(); } } } if (getColumnsReturnedList() != null) { return getResult(getReturnedColumns(getColumnsReturnedList(), statement)); } if (isScalar()) { return numRowsAffected + ""; } return "<result><rowsupdated>" + numRowsAffected + "</rowsupdated></result>"; } catch (SQLException sqle) { throw new SenderException(getLogPrefix() + "got exception executing a SQL command", sqle); } catch (JdbcException e) { throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e); } catch (IOException e) { throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e); } catch (JMSException e) { throw new SenderException(getLogPrefix() + "got exception executing a SQL command", e); } catch (ParameterException e) { throw new SenderException(getLogPrefix() + "got exception evaluating parameters", e); } finally { try { if (resultset != null) { resultset.close(); } } catch (SQLException e) { log.warn(new SenderException(getLogPrefix() + "got exception closing resultset", e)); } } }
From source file:com.wabacus.system.dataset.update.action.rationaldb.SPUpdateAction.java
public void updateData(ReportRequest rrequest, Map<String, String> mRowData, Map<String, String> mParamValues) throws SQLException { String realsql = getExecuteSql(rrequest, mRowData, mParamValues); AbsDatabaseType dbtype = rrequest.getDbType(this.datasource); Connection conn = rrequest.getConnection(this.datasource); CallableStatement cstmt = null; try {/*from w w w.ja va2 s. c o m*/ ReportBean rbean = this.ownerUpdateBean.getOwner().getReportBean(); if (Config.show_sql) log.info("Execute sql:" + realsql); cstmt = conn.prepareCall(realsql); if (lstParams != null && lstParams.size() > 0) { int idx = 1; IDataType varcharTypeObj = Config.getInstance().getDataTypeByClass(VarcharType.class); EditableReportParamBean paramBeanTmp; for (Object paramObjTmp : this.lstParams) { if (paramObjTmp instanceof EditableReportParamBean) { paramBeanTmp = (EditableReportParamBean) paramObjTmp; paramBeanTmp.getDataTypeObj() .setPreparedStatementValue(idx++, paramBeanTmp.getRuntimeParamValue(rrequest, rbean, mRowData, mParamValues, this.datasource, ownerUpdateBean.isAutoReportdata()), cstmt, dbtype); } else { varcharTypeObj.setPreparedStatementValue(idx++, paramObjTmp == null ? "" : String.valueOf(paramObjTmp), cstmt, dbtype); } } } int outputindex = -1; if (this.returnValueParamname != null && !this.returnValueParamname.trim().equals("")) {// outputindex = this.lstParams == null ? 1 : this.lstParams.size() + 1; cstmt.registerOutParameter(outputindex, java.sql.Types.VARCHAR); } cstmt.execute(); if (outputindex > 0) { String rtnVal = cstmt.getString(outputindex); storeReturnValue(rrequest, mParamValues, rtnVal); } } finally { WabacusAssistant.getInstance().release(null, cstmt); } }
From source file:pingpong.db.DBAccess.java
/** * ResultSet? ? ? .// w ww . j a v a2s .c om * @param sql * @param args * @return procedure? rtn_cod, rtn_msg String[] */ public String[] executeProcedure(String sql, Object args[]) throws SQLException { String result[] = { "", "" }; Connection local_con = null; CallableStatement cstmt = null; ResultSet rs = null; int idxParam = (args == null) ? 0 : args.length; log.debug(sql); if (args != null) { for (int i = 0; i < args.length; i++) { log.debug("args" + i + ":" + args[i]); } } try { if (m_con != null) { cstmt = m_con.prepareCall(sql); } else { local_con = getConnection(); cstmt = local_con.prepareCall(sql); } if (args != null) { for (int i = 0; i < args.length; i++) { if (args[i] instanceof Null) { cstmt.setNull(i + 1, ((Null) args[i]).type); } else { cstmt.setObject(i + 1, args[i]); } } } cstmt.setString(idxParam + 1, ""); cstmt.setString(idxParam + 2, ""); cstmt.registerOutParameter(idxParam + 1, Types.VARCHAR); cstmt.registerOutParameter(idxParam + 2, Types.VARCHAR); cstmt.execute(); result[0] = cstmt.getString(idxParam + 1); result[1] = cstmt.getString(idxParam + 2); } catch (SQLException e) { state = e.getErrorCode(); sqlState = e.getSQLState(); message = e.getMessage(); throw e; } catch (Exception e) { message = e.getMessage(); e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } } if (cstmt != null) { try { cstmt.close(); } catch (Exception e) { } } if (local_con != null) { try { local_con.close(); } catch (Exception e) { } } } return result; }
From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java
private ParamValue getOutparameterValue(CallableStatement cs, String type, int ordinal) throws DataServiceFault { try {/*w w w . j ava2 s .c o m*/ Object elementValue; if (type.equals(DBConstants.DataTypes.STRING)) { elementValue = cs.getString(ordinal); return new ParamValue(elementValue == null ? null : elementValue.toString()); } else if (type.equals(DBConstants.DataTypes.DOUBLE)) { elementValue = cs.getDouble(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Double) elementValue)); } else if (type.equals(DBConstants.DataTypes.BIGINT)) { elementValue = cs.getLong(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Long) elementValue)); } else if (type.equals(DBConstants.DataTypes.INTEGER)) { elementValue = cs.getInt(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Integer) elementValue)); } else if (type.equals(DBConstants.DataTypes.TIME)) { elementValue = cs.getTime(ordinal); return new ParamValue(elementValue == null ? null : this.convertToTimeString((Time) elementValue)); } else if (type.equals(DBConstants.DataTypes.DATE)) { elementValue = cs.getDate(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Date) elementValue)); } else if (type.equals(DBConstants.DataTypes.TIMESTAMP)) { elementValue = cs.getTimestamp(ordinal, calendar); return new ParamValue( elementValue == null ? null : this.convertToTimestampString((Timestamp) elementValue)); } else if (type.equals(DBConstants.DataTypes.BLOB)) { elementValue = cs.getBlob(ordinal); return new ParamValue(elementValue == null ? null : this.getBase64StringFromInputStream(((Blob) elementValue).getBinaryStream())); } else if (type.equals(DBConstants.DataTypes.CLOB)) { elementValue = cs.getClob(ordinal); return new ParamValue(elementValue == null ? null : deriveValueFromClob((Clob) elementValue)); } else if (type.equals(DBConstants.DataTypes.STRUCT)) { elementValue = cs.getObject(ordinal); return new ParamValue(elementValue == null ? null : (Struct) elementValue); } else if (type.equals(DBConstants.DataTypes.ARRAY)) { Array dataArray = cs.getArray(ordinal); ParamValue paramValue = new ParamValue(ParamValue.PARAM_VALUE_ARRAY); if (dataArray != null) { this.processSQLArray(dataArray, paramValue); } return paramValue; } else if (type.equals(DBConstants.DataTypes.NUMERIC)) { elementValue = cs.getBigDecimal(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((BigDecimal) elementValue)); } else if (type.equals(DBConstants.DataTypes.BIT)) { elementValue = cs.getBoolean(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Boolean) elementValue)); } else if (type.equals(DBConstants.DataTypes.TINYINT)) { elementValue = cs.getByte(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Byte) elementValue)); } else if (type.equals(DBConstants.DataTypes.SMALLINT)) { elementValue = cs.getShort(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Short) elementValue)); } else if (type.equals(DBConstants.DataTypes.REAL)) { elementValue = cs.getFloat(ordinal); return new ParamValue( elementValue == null ? null : ConverterUtil.convertToString((Float) elementValue)); } else if (type.equals(DBConstants.DataTypes.BINARY)) { elementValue = cs.getBlob(ordinal); return new ParamValue(elementValue == null ? null : this.getBase64StringFromInputStream(((Blob) elementValue).getBinaryStream())); } else { throw new DataServiceFault("Unsupported data type: " + type); } } catch (SQLException e) { throw new DataServiceFault(e, "Error in getting sql output parameter values."); } }
From source file:pingpong.db.DBAccess.java
/** * ResultSet? ? ? ./*w w w.j a va 2 s . co m*/ * @param args * - 2? arguments . * @return TResultSet? */ public TResultSet executeProcedure2(String sql, Object args[]) throws SQLException { // String result[] = {"", ""}; Connection local_con = null; CallableStatement cstmt = null; ResultSet rs = null; TResultSet ds = null; // sql.replace("?)", "?,?)"); int idxParam = (args == null) ? 0 : args.length; try { if (m_con != null) { cstmt = m_con.prepareCall(sql); } else { local_con = getConnection(); cstmt = local_con.prepareCall(sql); } if (args != null) { for (int i = 0; i < args.length - 2; i++) { if (args[i] instanceof Null) { log.debug("1###########i:" + i + args[i]); cstmt.setNull(i + 1, ((Null) args[i]).type); } else { cstmt.setObject(i + 1, args[i]); log.debug("2###########i:" + i + args[i]); } } cstmt.setString(idxParam - 1, ""); cstmt.setString(idxParam, ""); cstmt.registerOutParameter(idxParam - 1, Types.VARCHAR); cstmt.registerOutParameter(idxParam, Types.VARCHAR); // cstmt.registerOutParameter(idxParam, OracleTypes.CURSOR); log.debug("###########sql:" + sql + "\nargs:" + args + " len:" + args.length); log.debug("###########sql:" + sql + "\nargs:" + args + " len:" + args.length); } rs = cstmt.executeQuery(); ds = new TResultSet(rs); args[idxParam - 2] = cstmt.getString(idxParam - 1); args[idxParam - 1] = cstmt.getString(idxParam); } catch (SQLException e) { state = e.getErrorCode(); sqlState = e.getSQLState(); message = e.getMessage(); throw e; } catch (Exception e) { message = e.getMessage(); e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } } if (cstmt != null) { try { cstmt.close(); } catch (Exception e) { } } if (local_con != null) { try { local_con.close(); } catch (Exception e) { } } } return ds; }
From source file:nl.nn.adapterframework.jdbc.JdbcQuerySenderBase.java
protected String executePackageQuery(Connection connection, PreparedStatement statement, String message) throws SenderException, JdbcException, IOException, JMSException { Object[] paramArray = new Object[10]; String callMessage = fillParamArray(paramArray, message); ResultSet resultset = null;// www . j a va 2 s. c om try { CallableStatement pstmt = connection.prepareCall(callMessage); if (getMaxRows() > 0) { pstmt.setMaxRows(getMaxRows() + (getStartRow() > 1 ? getStartRow() - 1 : 0)); } int var = 1; for (int i = 0; i < paramArray.length; i++) { if (paramArray[i] instanceof Timestamp) { pstmt.setTimestamp(var, (Timestamp) paramArray[i]); var++; } if (paramArray[i] instanceof java.sql.Date) { pstmt.setDate(var, (java.sql.Date) paramArray[i]); var++; } if (paramArray[i] instanceof String) { pstmt.setString(var, (String) paramArray[i]); var++; } if (paramArray[i] instanceof Integer) { int x = Integer.parseInt(paramArray[i].toString()); pstmt.setInt(var, x); var++; } if (paramArray[i] instanceof Float) { float x = Float.parseFloat(paramArray[i].toString()); pstmt.setFloat(var, x); var++; } } if (message.indexOf('?') != -1) { pstmt.registerOutParameter(var, Types.CLOB); // make sure enough space is available for result... } if ("xml".equalsIgnoreCase(getPackageContent())) { log.debug(getLogPrefix() + "executing a package SQL command"); pstmt.executeUpdate(); String pUitvoer = pstmt.getString(var); return pUitvoer; } log.debug(getLogPrefix() + "executing a package SQL command"); int numRowsAffected = pstmt.executeUpdate(); if (StringUtils.isNotEmpty(getResultQuery())) { Statement resStmt = null; try { resStmt = connection.createStatement(); log.debug("obtaining result from [" + getResultQuery() + "]"); ResultSet rs = resStmt.executeQuery(getResultQuery()); return getResult(rs); } finally { if (resStmt != null) { resStmt.close(); } } } if (getColumnsReturnedList() != null) { return getResult(getReturnedColumns(getColumnsReturnedList(), statement)); } if (isScalar()) { return numRowsAffected + ""; } return "<result><rowsupdated>" + numRowsAffected + "</rowsupdated></result>"; } catch (SQLException sqle) { throw new SenderException(getLogPrefix() + "got exception executing a package SQL command", sqle); } finally { try { if (resultset != null) { resultset.close(); } } catch (SQLException e) { log.warn(new SenderException(getLogPrefix() + "got exception closing resultset", e)); } } }
From source file:pingpong.db.DBAccess.java
public TResultSet executeProcedure3(String sql, Object args[]) throws SQLException { Connection local_con = null;/*from w w w . j a v a 2 s. c om*/ CallableStatement cstmt = null; ResultSet rs = null; TResultSet ds = null; sql = sql.replace("?)", "?,?)"); // log.debug("sql repalce:" + sql); int idxParam = (args == null) ? 0 : args.length + 1; try { if (m_con != null) { cstmt = m_con.prepareCall(sql); } else { local_con = getConnection(); cstmt = local_con.prepareCall(sql); } if (args != null) { for (int i = 0; i < args.length - 2; i++) { log.debug("###########i: " + i + "/" + args[i] + "/" + args.length); if (args[i] instanceof Null) { cstmt.setNull(i + 1, ((Null) args[i]).type); } else { cstmt.setObject(i + 1, args[i]); } } cstmt.setString(idxParam - 2, ""); cstmt.setString(idxParam - 1, ""); cstmt.registerOutParameter(idxParam - 2, Types.VARCHAR); cstmt.registerOutParameter(idxParam - 1, Types.VARCHAR); // For Oracle // cstmt.registerOutParameter(idxParam, OracleTypes.CURSOR); // log.debug("###########sql:" + sql + "\nargs:" + args + " len:" + args.length); } //OracleCallableStatement ? getCursor() method REF CURSOR //JDBC ResultSet variable ? . // cstmt.executeQuery(); // rs = (ResultSet) cstmt.getObject(idxParam); rs = cstmt.executeQuery(); ds = new TResultSet(rs); args[idxParam - 3] = cstmt.getString(idxParam - 2); //call by refenene 000 args[idxParam - 2] = cstmt.getString(idxParam - 1); //O.K log.debug(cstmt.getString(idxParam - 2) + "," + cstmt.getString(idxParam - 1)); } catch (SQLException e) { state = e.getErrorCode(); sqlState = e.getSQLState(); message = e.getMessage(); throw e; } catch (Exception e) { message = e.getMessage(); e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) { } } if (cstmt != null) { try { cstmt.close(); } catch (Exception e) { } } if (local_con != null) { try { local_con.close(); } catch (Exception e) { } } } return ds; }
From source file:com.MainFiles.Functions.java
public String generateUniqueReferenceNumber() throws SQLException { String strReferenceNumber = ""; Connection dbConnection = connections.getDBConnection(ECSERVER, ECDB, ECUSER, ECPASSWORD); try {//from www . j a va 2 s . c o m NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "tz")); String strStoredProcedure = "{call SP_GET_AGENCY_NEXT_SEQ(?)}"; CallableStatement callableStatement = dbConnection.prepareCall(strStoredProcedure); callableStatement.registerOutParameter(1, java.sql.Types.VARCHAR); callableStatement.executeUpdate(); String response = callableStatement.getString(1); strReferenceNumber = "POS" + response; } catch (Exception ex) { this.log("INFO :: Error on generateUniqueReferenceNumber " + ex.getMessage() + "\n" + this.StackTraceWriter(ex), "ERROR"); } finally { dbConnection.close(); } return strReferenceNumber; }
From source file:com.hangum.tadpole.engine.sql.util.executer.procedure.OracleProcedureExecuter.java
@Override public boolean exec(List<InOutParameterDAO> parameterList) throws Exception { initResult();// w w w.j a v a 2s . c o m java.sql.Connection javaConn = null; java.sql.CallableStatement cstmt = null; java.sql.PreparedStatement pstmt = null; OracleDbmsOutputUtil dbmsOutput = null; try { if (listOutParamValues == null) getOutParameters(); SqlMapClient client = TadpoleSQLManager.getInstance(userDB); javaConn = client.getDataSource().getConnection(); try { dbmsOutput = new OracleDbmsOutputUtil(javaConn); dbmsOutput.enable(1000000); } catch (SQLException e) { logger.error("dbmsoutput exception", e); } // make the script String strExecuteScript = getMakeExecuteScript(); if (StringUtils.startsWithIgnoreCase(strExecuteScript, "SELECT")) { // function execute... pstmt = javaConn.prepareStatement(strExecuteScript); for (InOutParameterDAO inOutParameterDAO : parameterList) { pstmt.setObject(inOutParameterDAO.getOrder(), inOutParameterDAO.getValue()); } // Set the OUT Parameter for (int i = 0; i < listOutParamValues.size(); i++) { InOutParameterDAO dao = listOutParamValues.get(i); //pstmt.registerOutParameter(dao.getOrder(), RDBTypeToJavaTypeUtils.getJavaType(dao.getRdbType())); pstmt.setObject(dao.getOrder(), ""); } ResultSet rs = pstmt.executeQuery(); setResultCursor(rs); } else { // set prepare call cstmt = javaConn.prepareCall(strExecuteScript); // Set input value for (InOutParameterDAO inOutParameterDAO : parameterList) { cstmt.setObject(inOutParameterDAO.getOrder(), inOutParameterDAO.getValue()); } // Set the OUT Parameter for (int i = 0; i < listOutParamValues.size(); i++) { InOutParameterDAO dao = listOutParamValues.get(i); if (logger.isDebugEnabled()) logger.debug("Out Parameter " + dao.getOrder() + " JavaType is " + RDBTypeToJavaTypeUtils.getJavaType(dao.getRdbType())); cstmt.registerOutParameter(dao.getOrder(), RDBTypeToJavaTypeUtils.getJavaType(dao.getRdbType())); } cstmt.execute(); // // set // // cursor list // boolean is cursor boolean isCursor = false; for (int i = 0; i < listOutParamValues.size(); i++) { InOutParameterDAO dao = listOutParamValues.get(i); if (logger.isDebugEnabled()) logger.debug("Execute Procedure result " + dao.getName() + "=" + cstmt.getString(dao.getOrder())); Object obj = cstmt.getObject(dao.getOrder()); // String? Type Cast .... String ... if (obj != null) { if ("SYS_REFCURSOR".equals(dao.getRdbType())) { isCursor = true; ResultSet rs = (ResultSet) obj; setResultCursor(rs); // cursor? ? ? 1. } else { dao.setValue(obj.toString()); } } } if (!isCursor) { List<Map<Integer, Object>> sourceDataList = new ArrayList<Map<Integer, Object>>(); Map<Integer, Object> tmpRow = null; for (int i = 0; i < listOutParamValues.size(); i++) { InOutParameterDAO dao = listOutParamValues.get(i); tmpRow = new HashMap<Integer, Object>(); tmpRow.put(0, "" + dao.getOrder()); tmpRow.put(1, "" + dao.getName()); tmpRow.put(2, "" + dao.getType()); tmpRow.put(3, "" + dao.getRdbType()); tmpRow.put(4, "" + dao.getLength()); tmpRow.put(5, "" + dao.getValue()); sourceDataList.add(tmpRow); } setResultNoCursor(new TadpoleResultSet(sourceDataList)); } } try { dbmsOutput.show(); } catch (SQLException e) { logger.error("dbmsoutput exception", e); } setStrOutput(dbmsOutput.getOutput()); return true; } catch (Exception e) { logger.error("ProcedureExecutor executing error", e); throw e; } finally { try { if (pstmt != null) pstmt.close(); } catch (Exception e) { } try { if (cstmt != null) cstmt.close(); } catch (Exception e) { } try { if (dbmsOutput != null) dbmsOutput.close(); } catch (Exception e) { } try { if (javaConn != null) javaConn.close(); } catch (Exception e) { } } }