Example usage for java.sql ResultSet getBigDecimal

List of usage examples for java.sql ResultSet getBigDecimal

Introduction

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

Prototype

BigDecimal getBigDecimal(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.math.BigDecimal with full precision.

Usage

From source file:org.apache.ddlutils.platform.PlatformImplBase.java

/**
 * This is the core method to retrieve a value for a column from a result set. Its  primary
 * purpose is to call the appropriate method on the result set, and to provide an extension
 * point where database-specific implementations can change this behavior.
 * //ww  w  .j ava  2  s  .  c  o  m
 * @param resultSet  The result set to extract the value from
 * @param columnName The name of the column; can be <code>null</code> in which case the
  *                   <code>columnIdx</code> will be used instead
  * @param columnIdx  The index of the column's value in the result set; is only used if
  *                   <code>columnName</code> is <code>null</code>
 * @param jdbcType   The jdbc type to extract
 * @return The value
 * @throws SQLException If an error occurred while accessing the result set
 */
protected Object extractColumnValue(ResultSet resultSet, String columnName, int columnIdx, int jdbcType)
        throws SQLException {
    boolean useIdx = (columnName == null);
    Object value;

    switch (jdbcType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        value = useIdx ? resultSet.getString(columnIdx) : resultSet.getString(columnName);
        break;
    case Types.NUMERIC:
    case Types.DECIMAL:
        value = useIdx ? resultSet.getBigDecimal(columnIdx) : resultSet.getBigDecimal(columnName);
        break;
    case Types.BIT:
    case Types.BOOLEAN:
        value = new Boolean(useIdx ? resultSet.getBoolean(columnIdx) : resultSet.getBoolean(columnName));
        break;
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
        value = new Integer(useIdx ? resultSet.getInt(columnIdx) : resultSet.getInt(columnName));
        break;
    case Types.BIGINT:
        value = new Long(useIdx ? resultSet.getLong(columnIdx) : resultSet.getLong(columnName));
        break;
    case Types.REAL:
        value = new Float(useIdx ? resultSet.getFloat(columnIdx) : resultSet.getFloat(columnName));
        break;
    case Types.FLOAT:
    case Types.DOUBLE:
        value = new Double(useIdx ? resultSet.getDouble(columnIdx) : resultSet.getDouble(columnName));
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        value = useIdx ? resultSet.getBytes(columnIdx) : resultSet.getBytes(columnName);
        break;
    case Types.DATE:
        value = useIdx ? resultSet.getDate(columnIdx) : resultSet.getDate(columnName);
        break;
    case Types.TIME:
        value = useIdx ? resultSet.getTime(columnIdx) : resultSet.getTime(columnName);
        break;
    case Types.TIMESTAMP:
        value = useIdx ? resultSet.getTimestamp(columnIdx) : resultSet.getTimestamp(columnName);
        break;
    case Types.CLOB:
        Clob clob = useIdx ? resultSet.getClob(columnIdx) : resultSet.getClob(columnName);

        if (clob == null) {
            value = null;
        } else {
            long length = clob.length();

            if (length > Integer.MAX_VALUE) {
                value = clob;
            } else if (length == 0) {
                // the javadoc is not clear about whether Clob.getSubString
                // can be used with a substring length of 0
                // thus we do the safe thing and handle it ourselves
                value = "";
            } else {
                value = clob.getSubString(1l, (int) length);
            }
        }
        break;
    case Types.BLOB:
        Blob blob = useIdx ? resultSet.getBlob(columnIdx) : resultSet.getBlob(columnName);

        if (blob == null) {
            value = null;
        } else {
            long length = blob.length();

            if (length > Integer.MAX_VALUE) {
                value = blob;
            } else if (length == 0) {
                // the javadoc is not clear about whether Blob.getBytes
                // can be used with for 0 bytes to be copied
                // thus we do the safe thing and handle it ourselves
                value = new byte[0];
            } else {
                value = blob.getBytes(1l, (int) length);
            }
        }
        break;
    case Types.ARRAY:
        value = useIdx ? resultSet.getArray(columnIdx) : resultSet.getArray(columnName);
        break;
    case Types.REF:
        value = useIdx ? resultSet.getRef(columnIdx) : resultSet.getRef(columnName);
        break;
    default:
        value = useIdx ? resultSet.getObject(columnIdx) : resultSet.getObject(columnName);
        break;
    }
    return resultSet.wasNull() ? null : value;
}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public Field resultToField(ResultSetMetaData md, ResultSet rs, int columnIndex, int maxClobSize,
        int maxBlobSize, DataType userSpecifiedType, UnknownTypeAction unknownTypeAction,
        boolean timestampToString) throws SQLException, IOException, StageException {
    Field field;/*from   w  w w .  ja va2 s.c  o  m*/
    if (userSpecifiedType != DataType.USE_COLUMN_TYPE) {
        // If user specifies the data type, overwrite the column type returned by database.
        field = Field.create(Field.Type.valueOf(userSpecifiedType.getLabel()), rs.getObject(columnIndex));
    } else {
        // All types as of JDBC 2.0 are here:
        // https://docs.oracle.com/javase/8/docs/api/constant-values.html#java.sql.Types.ARRAY
        // Good source of recommended mappings is here:
        // http://www.cs.mun.ca/java-api-1.5/guide/jdbc/getstart/mapping.html
        switch (md.getColumnType(columnIndex)) {
        case Types.BIGINT:
            field = Field.create(Field.Type.LONG, rs.getObject(columnIndex));
            break;
        case Types.BINARY:
        case Types.LONGVARBINARY:
        case Types.VARBINARY:
            field = Field.create(Field.Type.BYTE_ARRAY, rs.getBytes(columnIndex));
            break;
        case Types.BIT:
        case Types.BOOLEAN:
            field = Field.create(Field.Type.BOOLEAN, rs.getObject(columnIndex));
            break;
        case Types.CHAR:
        case Types.LONGNVARCHAR:
        case Types.LONGVARCHAR:
        case Types.NCHAR:
        case Types.NVARCHAR:
        case Types.VARCHAR:
            field = Field.create(Field.Type.STRING, rs.getObject(columnIndex));
            break;
        case Types.CLOB:
        case Types.NCLOB:
            field = Field.create(Field.Type.STRING, getClobString(rs.getClob(columnIndex), maxClobSize));
            break;
        case Types.BLOB:
            field = Field.create(Field.Type.BYTE_ARRAY, getBlobBytes(rs.getBlob(columnIndex), maxBlobSize));
            break;
        case Types.DATE:
            field = Field.create(Field.Type.DATE, rs.getDate(columnIndex));
            break;
        case Types.DECIMAL:
        case Types.NUMERIC:
            field = Field.create(Field.Type.DECIMAL, rs.getBigDecimal(columnIndex));
            field.setAttribute(HeaderAttributeConstants.ATTR_SCALE,
                    String.valueOf(rs.getMetaData().getScale(columnIndex)));
            field.setAttribute(HeaderAttributeConstants.ATTR_PRECISION,
                    String.valueOf(rs.getMetaData().getPrecision(columnIndex)));
            break;
        case Types.DOUBLE:
            field = Field.create(Field.Type.DOUBLE, rs.getObject(columnIndex));
            break;
        case Types.FLOAT:
        case Types.REAL:
            field = Field.create(Field.Type.FLOAT, rs.getObject(columnIndex));
            break;
        case Types.INTEGER:
            field = Field.create(Field.Type.INTEGER, rs.getObject(columnIndex));
            break;
        case Types.ROWID:
            field = Field.create(Field.Type.STRING, rs.getRowId(columnIndex).toString());
            break;
        case Types.SMALLINT:
        case Types.TINYINT:
            field = Field.create(Field.Type.SHORT, rs.getObject(columnIndex));
            break;
        case Types.TIME:
            field = Field.create(Field.Type.TIME, rs.getObject(columnIndex));
            break;
        case Types.TIMESTAMP:
            final Timestamp timestamp = rs.getTimestamp(columnIndex);
            if (timestampToString) {
                field = Field.create(Field.Type.STRING, timestamp == null ? null : timestamp.toString());
            } else {
                field = Field.create(Field.Type.DATETIME, timestamp);
                if (timestamp != null) {
                    final long actualNanos = timestamp.getNanos() % NANOS_TO_MILLIS_ADJUSTMENT;
                    if (actualNanos > 0) {
                        field.setAttribute(FIELD_ATTRIBUTE_NANOSECONDS, String.valueOf(actualNanos));
                    }
                }
            }
            break;
        // Ugly hack until we can support LocalTime, LocalDate, LocalDateTime, etc.
        case Types.TIME_WITH_TIMEZONE:
            OffsetTime offsetTime = rs.getObject(columnIndex, OffsetTime.class);
            field = Field.create(Field.Type.TIME, Date.from(offsetTime.atDate(LocalDate.MIN).toInstant()));
            break;
        case Types.TIMESTAMP_WITH_TIMEZONE:
            OffsetDateTime offsetDateTime = rs.getObject(columnIndex, OffsetDateTime.class);
            field = Field.create(Field.Type.ZONED_DATETIME, offsetDateTime.toZonedDateTime());
            break;
        //case Types.REF_CURSOR: // JDK8 only
        case Types.SQLXML:
        case Types.STRUCT:
        case Types.ARRAY:
        case Types.DATALINK:
        case Types.DISTINCT:
        case Types.JAVA_OBJECT:
        case Types.NULL:
        case Types.OTHER:
        case Types.REF:
        default:
            if (unknownTypeAction == null) {
                return null;
            }
            switch (unknownTypeAction) {
            case STOP_PIPELINE:
                throw new StageException(JdbcErrors.JDBC_37, md.getColumnType(columnIndex),
                        md.getColumnLabel(columnIndex));
            case CONVERT_TO_STRING:
                Object value = rs.getObject(columnIndex);
                if (value != null) {
                    field = Field.create(Field.Type.STRING, rs.getObject(columnIndex).toString());
                } else {
                    field = Field.create(Field.Type.STRING, null);
                }
                break;
            default:
                throw new IllegalStateException("Unknown action: " + unknownTypeAction);
            }
        }
    }

    return field;
}

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

private void assertWrongValueFormatColumn(final ResultSet rs) throws Exception {
    assertFalse(rs.getBoolean(2));//from w w w  .j  av  a2s.c  om
    assertFalse(rs.getBoolean("ename"));

    try {
        rs.getShort(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getShort("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getInt("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getLong("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getFloat("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDouble("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBigDecimal("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBytes("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate(2, Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getDate("ename", Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime(2, Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTime("ename", Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp(2, Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getTimestamp("ename", Calendar.getInstance());
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getAsciiStream("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getBinaryStream("ename");
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream(2);
        fail();
    } catch (SQLException ignore) {
    }

    try {
        rs.getCharacterStream("ename");
        fail();
    } catch (SQLException ignore) {
    }

}

From source file:org.moqui.impl.entity.EntityJavaUtil.java

public static Object getResultSetValue(ResultSet rs, int index, FieldInfo fi, EntityFacade efi)
        throws EntityException {
    if (fi.typeValue == -1)
        throw new EntityException("No typeValue found for " + fi.entityName + "." + fi.name);

    Object value = null;/*  w  w  w. ja v  a2 s  . com*/
    try {
        switch (fi.typeValue) {
        case 1:
            // getMetaData and the column type are somewhat slow (based on profiling), and String values are VERY
            //     common, so only do for text-very-long
            if (fi.isTextVeryLong) {
                ResultSetMetaData rsmd = rs.getMetaData();
                if (Types.CLOB == rsmd.getColumnType(index)) {
                    // if the String is empty, try to get a text input stream, this is required for some databases
                    // for larger fields, like CLOBs
                    Clob valueClob = rs.getClob(index);
                    Reader valueReader = null;
                    if (valueClob != null)
                        valueReader = valueClob.getCharacterStream();
                    if (valueReader != null) {
                        // read up to 4096 at a time
                        char[] inCharBuffer = new char[4096];
                        StringBuilder strBuf = new StringBuilder();
                        try {
                            int charsRead;
                            while ((charsRead = valueReader.read(inCharBuffer, 0, 4096)) > 0) {
                                strBuf.append(inCharBuffer, 0, charsRead);
                            }
                            valueReader.close();
                        } catch (IOException e) {
                            throw new EntityException("Error reading long character stream for field ["
                                    + fi.name + "] of entity [" + fi.entityName + "]", e);
                        }
                        value = strBuf.toString();
                    }
                } else {
                    value = rs.getString(index);
                }
            } else {
                value = rs.getString(index);
            }
            break;
        case 2:
            try {
                value = rs.getTimestamp(index, efi.getCalendarForTzLc());
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace(
                            "Ignoring SQLException for getTimestamp(), leaving null (found this in MySQL with a date/time value of [0000-00-00 00:00:00]): "
                                    + e.toString());
            }
            break;
        case 3:
            value = rs.getTime(index, efi.getCalendarForTzLc());
            break;
        case 4:
            value = rs.getDate(index, efi.getCalendarForTzLc());
            break;
        case 5:
            int intValue = rs.getInt(index);
            if (!rs.wasNull())
                value = intValue;
            break;
        case 6:
            long longValue = rs.getLong(index);
            if (!rs.wasNull())
                value = longValue;
            break;
        case 7:
            float floatValue = rs.getFloat(index);
            if (!rs.wasNull())
                value = floatValue;
            break;
        case 8:
            double doubleValue = rs.getDouble(index);
            if (!rs.wasNull())
                value = doubleValue;
            break;
        case 9:
            BigDecimal bigDecimalValue = rs.getBigDecimal(index);
            if (!rs.wasNull())
                value = bigDecimalValue != null ? bigDecimalValue.stripTrailingZeros() : null;
            break;
        case 10:
            boolean booleanValue = rs.getBoolean(index);
            if (!rs.wasNull())
                value = booleanValue;
            break;
        case 11:
            Object obj = null;
            byte[] originalBytes = rs.getBytes(index);
            InputStream binaryInput = null;
            if (originalBytes != null && originalBytes.length > 0) {
                binaryInput = new ByteArrayInputStream(originalBytes);
            }
            if (originalBytes != null && originalBytes.length <= 0) {
                logger.warn("Got byte array back empty for serialized Object with length ["
                        + originalBytes.length + "] for field [" + fi.name + "] (" + index + ")");
            }
            if (binaryInput != null) {
                ObjectInputStream inStream = null;
                try {
                    inStream = new ObjectInputStream(binaryInput);
                    obj = inStream.readObject();
                } catch (IOException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Unable to read BLOB from input stream for field [" + fi.name + "] ("
                                + index + "): " + ex.toString());
                } catch (ClassNotFoundException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Class not found: Unable to cast BLOB data to an Java object for field ["
                                + fi.name + "] (" + index
                                + "); most likely because it is a straight byte[], so just using the raw bytes: "
                                + ex.toString());
                } finally {
                    if (inStream != null) {
                        try {
                            inStream.close();
                        } catch (IOException e) {
                            throw new EntityException("Unable to close binary input stream for field ["
                                    + fi.name + "] (" + index + "): " + e.toString(), e);
                        }
                    }
                }
            }
            if (obj != null) {
                value = obj;
            } else {
                value = originalBytes;
            }
            break;
        case 12:
            SerialBlob sblob = null;
            try {
                // NOTE: changed to try getBytes first because Derby blows up on getBlob and on then calling getBytes for the same field, complains about getting value twice
                byte[] fieldBytes = rs.getBytes(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(fieldBytes);
                // fieldBytes = theBlob != null ? theBlob.getBytes(1, (int) theBlob.length()) : null
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace("Ignoring exception trying getBytes(), trying getBlob(): " + e.toString());
                Blob theBlob = rs.getBlob(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(theBlob);
            }
            value = sblob;
            break;
        case 13:
            value = new SerialClob(rs.getClob(index));
            break;
        case 14:
        case 15:
            value = rs.getObject(index);
            break;
        }
    } catch (SQLException sqle) {
        logger.error("SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
        throw new EntityException(
                "SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
    }

    return value;
}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccDb2LUW.CFAccDb2LUWAccountTable.java

protected CFAccAccountBuff unpackAccountResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackAccountResultSetToBuff";
    int idxcol = 1;
    CFAccAccountBuff buff = schema.getFactoryAccount().newBuff();
    {/*  w w  w  .  java 2 s.c o  m*/
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedByUserId(null);
        } else {
            buff.setCreatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedAt(null);
        } else {
            buff.setCreatedAt(CFAccDb2LUWSchema.convertTimestampString(colString));
        }
        idxcol++;
        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedByUserId(null);
        } else {
            buff.setUpdatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedAt(null);
        } else {
            buff.setUpdatedAt(CFAccDb2LUWSchema.convertTimestampString(colString));
        }
        idxcol++;
    }
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredAccountCode(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredDescription(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredCurrencyId(resultSet.getShort(idxcol));
    idxcol++;
    buff.setRequiredBalance(resultSet.getBigDecimal(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupTenantId(null);
        } else {
            buff.setOptionalRollupTenantId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupAccountId(null);
        } else {
            buff.setOptionalRollupAccountId(colVal);
        }
    }
    idxcol++;

    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccPgSql.CFAccPgSqlAccountTable.java

protected CFAccAccountBuff unpackAccountResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackAccountResultSetToBuff";
    int idxcol = 1;
    CFAccAccountBuff buff = schema.getFactoryAccount().newBuff();
    {// ww  w  .  j  av a 2s . c  o  m
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedByUserId(null);
        } else {
            buff.setCreatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedAt(null);
        } else {
            buff.setCreatedAt(CFAccPgSqlSchema.convertTimestampString(colString));
        }
        idxcol++;
        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedByUserId(null);
        } else {
            buff.setUpdatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedAt(null);
        } else {
            buff.setUpdatedAt(CFAccPgSqlSchema.convertTimestampString(colString));
        }
        idxcol++;
    }
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredAccountCode(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredDescription(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredCurrencyId(resultSet.getShort(idxcol));
    idxcol++;
    buff.setRequiredBalance(resultSet.getBigDecimal(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupTenantId(null);
        } else {
            buff.setOptionalRollupTenantId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupAccountId(null);
        } else {
            buff.setOptionalRollupAccountId(colVal);
        }
    }
    idxcol++;

    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccMSSql.CFAccMSSqlAccountTable.java

protected CFAccAccountBuff unpackAccountResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackAccountResultSetToBuff";
    int idxcol = 1;
    CFAccAccountBuff buff = schema.getFactoryAccount().newBuff();
    {//from   w  w  w  .jav a2 s.c  o m
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedByUserId(null);
        } else {
            buff.setCreatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedAt(null);
        } else {
            buff.setCreatedAt(CFAccMSSqlSchema.convertTimestampString(colString));
        }
        idxcol++;
        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedByUserId(null);
        } else {
            buff.setUpdatedByUserId(UUID.fromString(colString));
        }
        idxcol++;

        colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedAt(null);
        } else {
            buff.setUpdatedAt(CFAccMSSqlSchema.convertTimestampString(colString));
        }
        idxcol++;
    }
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredAccountCode(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredDescription(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredCurrencyId(resultSet.getShort(idxcol));
    idxcol++;
    buff.setRequiredBalance(resultSet.getBigDecimal(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupTenantId(null);
        } else {
            buff.setOptionalRollupTenantId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupAccountId(null);
        } else {
            buff.setOptionalRollupAccountId(colVal);
        }
    }
    idxcol++;
    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:net.sourceforge.msscodefactory.cfacc.v2_0.CFAccMySql.CFAccMySqlAccountTable.java

protected CFAccAccountBuff unpackAccountResultSetToBuff(ResultSet resultSet) throws SQLException {
    final String S_ProcName = "unpackAccountResultSetToBuff";
    int idxcol = 1;
    CFAccAccountBuff buff = schema.getFactoryAccount().newBuff();
    {/*ww w.  ja v a 2s  . c o  m*/
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedAt(null);
        } else {
            buff.setCreatedAt(CFAccMySqlSchema.convertTimestampString(colString));
        }
    }
    idxcol++;
    {
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setCreatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setCreatedByUserId(null);
        } else {
            buff.setCreatedByUserId(UUID.fromString(colString));
        }
    }
    idxcol++;
    {
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedAt(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedAt(null);
        } else {
            buff.setUpdatedAt(CFAccMySqlSchema.convertTimestampString(colString));
        }
    }
    idxcol++;
    {
        String colString = resultSet.getString(idxcol);
        if (resultSet.wasNull()) {
            buff.setUpdatedByUserId(null);
        } else if ((colString == null) || (colString.length() <= 0)) {
            buff.setUpdatedByUserId(null);
        } else {
            buff.setUpdatedByUserId(UUID.fromString(colString));
        }
    }
    idxcol++;
    buff.setRequiredTenantId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredId(resultSet.getLong(idxcol));
    idxcol++;
    buff.setRequiredAccountCode(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredDescription(resultSet.getString(idxcol));
    idxcol++;
    buff.setRequiredCurrencyId(resultSet.getShort(idxcol));
    idxcol++;
    buff.setRequiredBalance(resultSet.getBigDecimal(idxcol));
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupTenantId(null);
        } else {
            buff.setOptionalRollupTenantId(colVal);
        }
    }
    idxcol++;
    {
        long colVal = resultSet.getLong(idxcol);
        if (resultSet.wasNull()) {
            buff.setOptionalRollupAccountId(null);
        } else {
            buff.setOptionalRollupAccountId(colVal);
        }
    }
    idxcol++;

    buff.setRequiredRevision(resultSet.getInt(idxcol));
    return (buff);
}

From source file:org.voltdb.HsqlBackend.java

public VoltTable runDML(String dml) {
    dml = dml.trim();//from   w  w w . j av a  2  s .  c o  m
    String indicator = dml.substring(0, 1).toLowerCase();
    if (indicator.equals("s") || // "s" is for "select ..."
            indicator.equals("(")) { // "(" is for "(select ... UNION ...)" et. al.
        try {
            Statement stmt = dbconn.createStatement();
            sqlLog.l7dlog(Level.DEBUG, LogKeys.sql_Backend_ExecutingDML.name(), new Object[] { dml }, null);
            sqlLog.debug("Executing " + dml);
            ResultSet rs = stmt.executeQuery(dml);
            ResultSetMetaData rsmd = rs.getMetaData();

            // note the index values here carefully
            VoltTable.ColumnInfo[] columns = new VoltTable.ColumnInfo[rsmd.getColumnCount()];
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                String colname = rsmd.getColumnLabel(i);
                String type = rsmd.getColumnTypeName(i);
                //LOG.fine("Column type: " + type);
                if (type.equals("VARCHAR"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.STRING);
                else if (type.equals("TINYINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.TINYINT);
                else if (type.equals("SMALLINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.SMALLINT);
                else if (type.equals("INTEGER"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.INTEGER);
                else if (type.equals("BIGINT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.BIGINT);
                else if (type.equals("DECIMAL"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.DECIMAL);
                else if (type.equals("FLOAT"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.FLOAT);
                else if (type.equals("TIMESTAMP"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.TIMESTAMP);
                else if (type.equals("VARBINARY"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.VARBINARY);
                else if (type.equals("CHARACTER"))
                    columns[i - 1] = new VoltTable.ColumnInfo(colname, VoltType.STRING);
                else
                    throw new ExpectedProcedureException(
                            "Trying to create a column in Backend with a (currently) unsupported type: "
                                    + type);
            }
            VoltTable table = new VoltTable(columns);
            while (rs.next()) {
                Object[] row = new Object[table.getColumnCount()];
                for (int i = 0; i < table.getColumnCount(); i++) {
                    if (table.getColumnType(i) == VoltType.STRING)
                        row[i] = rs.getString(i + 1);
                    else if (table.getColumnType(i) == VoltType.TINYINT)
                        row[i] = rs.getByte(i + 1);
                    else if (table.getColumnType(i) == VoltType.SMALLINT)
                        row[i] = rs.getShort(i + 1);
                    else if (table.getColumnType(i) == VoltType.INTEGER)
                        row[i] = rs.getInt(i + 1);
                    else if (table.getColumnType(i) == VoltType.BIGINT)
                        row[i] = rs.getLong(i + 1);
                    else if (table.getColumnType(i) == VoltType.DECIMAL)
                        row[i] = rs.getBigDecimal(i + 1);
                    else if (table.getColumnType(i) == VoltType.FLOAT)
                        row[i] = rs.getDouble(i + 1);
                    else if (table.getColumnType(i) == VoltType.VARBINARY)
                        row[i] = rs.getBytes(i + 1);
                    else if (table.getColumnType(i) == VoltType.TIMESTAMP) {
                        Timestamp t = rs.getTimestamp(i + 1);
                        if (t == null) {
                            row[i] = null;
                        } else {
                            // convert from millisecond to microsecond granularity
                            row[i] = new org.voltdb.types.TimestampType(t.getTime() * 1000);
                        }
                    } else {
                        throw new ExpectedProcedureException(
                                "Trying to read a (currently) unsupported type from a JDBC resultset.");
                    }
                    if (rs.wasNull()) {
                        // JDBC returns 0/0.0 instead of null. Put null into the row.
                        row[i] = null;
                    }
                }

                table.addRow(row);
            }
            stmt.close();
            rs.close();
            return table;
        } catch (Exception e) {
            if (e instanceof ExpectedProcedureException) {
                throw (ExpectedProcedureException) e;
            }
            sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
            throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
        }
    } else {
        try {
            Statement stmt = dbconn.createStatement();
            sqlLog.debug("Executing: " + dml);
            long ucount = stmt.executeUpdate(dml);
            sqlLog.debug("  result: " + String.valueOf(ucount));
            VoltTable table = new VoltTable(new VoltTable.ColumnInfo("", VoltType.BIGINT));
            table.addRow(ucount);
            return table;
        } catch (SQLException e) {
            // glorious hack to determine if the error is a constraint failure
            if (e.getMessage().contains("constraint")) {
                sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_ConvertingHSQLExtoCFEx.name(), e);
                final byte messageBytes[] = e.getMessage().getBytes();
                ByteBuffer b = ByteBuffer.allocate(25 + messageBytes.length);
                b.putInt(messageBytes.length);
                b.put(messageBytes);
                b.put(e.getSQLState().getBytes());
                b.putInt(0); // ConstraintFailure.type
                try {
                    FastSerializer.writeString("HSQL", b);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                b.putInt(0);//Table size is 0
                b.rewind();
                throw new ConstraintFailureException(b);
            } else {
                sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
                throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
            }

        } catch (Exception e) {
            // rethrow an expected exception
            sqlLog.l7dlog(Level.TRACE, LogKeys.sql_Backend_DmlError.name(), e);
            throw new ExpectedProcedureException("HSQLDB Backend DML Error ", e);
        }
    }
}

From source file:org.wso2.carbon.dataservices.core.odata.RDBMSDataHandler.java

private String getValueFromResultSet(int columnType, String column, ResultSet resultSet) throws SQLException {
    String paramValue;/*from   ww w  . j a va  2  s.  com*/
    switch (columnType) {
    case Types.INTEGER:
        /* fall through */
    case Types.TINYINT:
        /* fall through */
    case Types.SMALLINT:
        paramValue = ConverterUtil.convertToString(resultSet.getInt(column));
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.DOUBLE:
        paramValue = ConverterUtil.convertToString(resultSet.getDouble(column));
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.VARCHAR:
        /* fall through */
    case Types.CHAR:
        /* fall through */
    case Types.CLOB:
        /* fall through */
    case Types.LONGVARCHAR:
        paramValue = resultSet.getString(column);
        break;
    case Types.BOOLEAN:
        /* fall through */
    case Types.BIT:
        paramValue = ConverterUtil.convertToString(resultSet.getBoolean(column));
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.BLOB:
        Blob sqlBlob = resultSet.getBlob(column);
        if (sqlBlob != null) {
            paramValue = this.getBase64StringFromInputStream(sqlBlob.getBinaryStream());
        } else {
            paramValue = null;
        }
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.BINARY:
        /* fall through */
    case Types.LONGVARBINARY:
        /* fall through */
    case Types.VARBINARY:
        InputStream binInStream = resultSet.getBinaryStream(column);
        if (binInStream != null) {
            paramValue = this.getBase64StringFromInputStream(binInStream);
        } else {
            paramValue = null;
        }
        break;
    case Types.DATE:
        Date sqlDate = resultSet.getDate(column);
        if (sqlDate != null) {
            paramValue = ConverterUtil.convertToString(sqlDate);
        } else {
            paramValue = null;
        }
        break;
    case Types.DECIMAL:
        /* fall through */
    case Types.NUMERIC:
        BigDecimal bigDecimal = resultSet.getBigDecimal(column);
        if (bigDecimal != null) {
            paramValue = ConverterUtil.convertToString(bigDecimal);
        } else {
            paramValue = null;
        }
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.FLOAT:
        paramValue = ConverterUtil.convertToString(resultSet.getFloat(column));
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.TIME:
        Time sqlTime = resultSet.getTime(column);
        if (sqlTime != null) {
            paramValue = this.convertToTimeString(sqlTime);
        } else {
            paramValue = null;
        }
        break;
    case Types.LONGNVARCHAR:
        /* fall through */
    case Types.NCHAR:
        /* fall through */
    case Types.NCLOB:
        /* fall through */
    case Types.NVARCHAR:
        paramValue = resultSet.getNString(column);
        break;
    case Types.BIGINT:
        paramValue = ConverterUtil.convertToString(resultSet.getLong(column));
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    case Types.TIMESTAMP:
        Timestamp sqlTimestamp = resultSet.getTimestamp(column);
        if (sqlTimestamp != null) {
            paramValue = this.convertToTimestampString(sqlTimestamp);
        } else {
            paramValue = null;
        }
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    /* handle all other types as strings */
    default:
        paramValue = resultSet.getString(column);
        paramValue = resultSet.wasNull() ? null : paramValue;
        break;
    }
    return paramValue;
}