Example usage for java.sql Types CHAR

List of usage examples for java.sql Types CHAR

Introduction

In this page you can find the example usage for java.sql Types CHAR.

Prototype

int CHAR

To view the source code for java.sql Types CHAR.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type CHAR.

Usage

From source file:axiom.objectmodel.db.NodeManager.java

/**
 *  Create a new Node from a ResultSet./*  www.java  2s  .c o m*/
 */
public Node createNode(DbMapping dbm, ResultSet rs, DbColumn[] columns, int offset)
        throws SQLException, IOException, ClassNotFoundException {
    HashMap propBuffer = new HashMap();
    String id = null;
    String name = null;
    String protoName = dbm.getTypeName();
    DbMapping dbmap = dbm;

    Node node = new Node();

    for (int i = 0; i < columns.length; i++) {
        // set prototype?
        if (columns[i].isPrototypeField()) {
            protoName = rs.getString(i + 1 + offset);

            if (protoName != null) {
                dbmap = getDbMapping(protoName);

                if (dbmap == null) {
                    // invalid prototype name!
                    app.logError(ErrorReporter.errorMsg(this.getClass(), "createNode")
                            + "Invalid prototype name: " + protoName + " - using default");
                    dbmap = dbm;
                    protoName = dbmap.getTypeName();
                }
            }
        }

        // set id?
        if (columns[i].isIdField()) {
            id = rs.getString(i + 1 + offset);
            // if id == null, the object doesn't actually exist - return null
            if (id == null) {
                return null;
            }
        }

        // set name?
        if (columns[i].isNameField()) {
            name = rs.getString(i + 1 + offset);
        }

        Property newprop = new Property(node);

        switch (columns[i].getType()) {
        case Types.BIT:
            newprop.setBooleanValue(rs.getBoolean(i + 1 + offset));

            break;

        case Types.TINYINT:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            newprop.setIntegerValue(rs.getLong(i + 1 + offset));

            break;

        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE:
            newprop.setFloatValue(rs.getDouble(i + 1 + offset));

            break;

        case Types.DECIMAL:
        case Types.NUMERIC:

            BigDecimal num = rs.getBigDecimal(i + 1 + offset);

            if (num == null) {
                break;
            }

            if (num.scale() > 0) {
                newprop.setFloatValue(num.doubleValue());
            } else {
                newprop.setIntegerValue(num.longValue());
            }

            break;

        case Types.VARBINARY:
        case Types.BINARY:
            //                    newprop.setStringValue(rs.getString(i+1+offset));
            newprop.setJavaObjectValue(rs.getBytes(i + 1 + offset));

            break;

        case Types.LONGVARBINARY: {
            InputStream in = rs.getBinaryStream(i + 1 + offset);
            if (in == null) {
                break;
            }
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            byte[] buffer = new byte[2048];
            int read;
            while ((read = in.read(buffer)) > -1) {
                bout.write(buffer, 0, read);
            }
            newprop.setJavaObjectValue(bout.toByteArray());
        }

            break;
        case Types.LONGVARCHAR:
            try {
                newprop.setStringValue(rs.getString(i + 1 + offset));
            } catch (SQLException x) {
                Reader in = rs.getCharacterStream(i + 1 + offset);
                char[] buffer = new char[2048];
                int read = 0;
                int r;

                while ((r = in.read(buffer, read, buffer.length - read)) > -1) {
                    read += r;

                    if (read == buffer.length) {
                        // grow input buffer
                        char[] newBuffer = new char[buffer.length * 2];

                        System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
                        buffer = newBuffer;
                    }
                }

                newprop.setStringValue(new String(buffer, 0, read));
            }

            break;

        case Types.CHAR:
        case Types.VARCHAR:
        case Types.OTHER:
            newprop.setStringValue(rs.getString(i + 1 + offset));

            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            newprop.setDateValue(rs.getTimestamp(i + 1 + offset));

            break;

        case Types.NULL:
            newprop.setStringValue(null);

            break;

        case Types.CLOB:
            Clob cl = rs.getClob(i + 1 + offset);
            if (cl == null) {
                newprop.setStringValue(null);
                break;
            }
            char[] c = new char[(int) cl.length()];
            Reader isr = cl.getCharacterStream();
            isr.read(c);
            newprop.setStringValue(String.copyValueOf(c));
            break;

        default:
            newprop.setStringValue(rs.getString(i + 1 + offset));

            break;
        }

        if (rs.wasNull()) {
            newprop.setStringValue(null);
        }

        propBuffer.put(columns[i].getName(), newprop);

        // mark property as clean, since it's fresh from the db
        newprop.dirty = false;
    }

    if (id == null) {
        return null;
    }

    Hashtable propMap = new Hashtable();
    DbColumn[] columns2 = dbmap.getColumns();
    for (int i = 0; i < columns2.length; i++) {
        Relation rel = columns2[i].getRelation();

        if (rel != null && (rel.reftype == Relation.PRIMITIVE || rel.reftype == Relation.REFERENCE)) {

            Property prop = (Property) propBuffer.get(columns2[i].getName());

            if (prop == null) {
                continue;
            }
            prop.setName(rel.propName);
            // if the property is a pointer to another node, change the property type to NODE
            if ((rel.reftype == Relation.REFERENCE) && rel.usesPrimaryKey()) {
                // FIXME: References to anything other than the primary key are not supported
                prop.convertToNodeReference(rel.otherType, this.app.getCurrentRequestEvaluator().getLayer());
            }
            propMap.put(rel.propName.toLowerCase(), prop);
        }
    }

    node.init(dbmap, id, name, protoName, propMap, safe);

    return node;
}

From source file:axiom.objectmodel.db.NodeManager.java

private void setStatementValues(PreparedStatement stmt, int stmtNumber, Property p, int columnType)
        throws SQLException {
    if (p.getValue() == null) {
        stmt.setNull(stmtNumber, columnType);
    } else {/*from  w w  w.  j a va 2s .c om*/
        switch (columnType) {
        case Types.BIT:
        case Types.TINYINT:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            stmt.setLong(stmtNumber, p.getIntegerValue());

            break;

        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE:
        case Types.NUMERIC:
        case Types.DECIMAL:
            stmt.setDouble(stmtNumber, p.getFloatValue());

            break;

        case Types.VARBINARY:
        case Types.BINARY:
        case Types.BLOB:
            stmt.setString(stmtNumber, p.getStringValue());

            break;

        case Types.LONGVARBINARY:
        case Types.LONGVARCHAR:
            try {
                stmt.setString(stmtNumber, p.getStringValue());
            } catch (SQLException x) {
                String str = p.getStringValue();
                Reader r = new StringReader(str);

                stmt.setCharacterStream(stmtNumber, r, str.length());
            }

            break;

        case Types.CLOB:
            String val = p.getStringValue();
            Reader isr = new StringReader(val);
            stmt.setCharacterStream(stmtNumber, isr, val.length());

            break;

        case Types.CHAR:
        case Types.VARCHAR:
        case Types.OTHER:
            stmt.setString(stmtNumber, p.getStringValue());

            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            stmt.setTimestamp(stmtNumber, p.getTimestampValue());

            break;

        case Types.NULL:
            stmt.setNull(stmtNumber, 0);

            break;

        default:
            stmt.setString(stmtNumber, p.getStringValue());

            break;
        }
    }
}

From source file:org.apache.ddlutils.io.TestDatabaseIO.java

/**
 * Tests a complex database model with multiple tables, foreign keys, indices and uniques.
 *//*from   w w w.  java  2 s. c  o  m*/
public void testComplex() throws Exception {
    // A = id:INTEGER, parentId:INTEGER, name:VARCHAR(32); fk 'parent' -> A (parentId -> id), unique(name)
    // B = id:TIMESTAMP, aid:INTEGER, cid:CHAR(32) fk -> A (aid -> id), fk -> C (cid -> id), index(aid,cid)
    // C = id:CHAR(32), text:LONGVARCHAR; index 'byText' (text)

    Database model = readModel("<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n"
            + "  <table name='A'\n" + "         description='Table A'>\n" + "    <column name='id'\n"
            + "            type='INTEGER'\n" + "            autoIncrement='true'\n"
            + "            primaryKey='true'\n" + "            required='true'\n"
            + "            description='The primary key of table A'/>\n" + "    <column name='parentId'\n"
            + "            type='INTEGER'\n"
            + "            description='The field for the foreign key parent'/>\n" + "    <column name='name'\n"
            + "            type='VARCHAR'\n" + "            size='32'\n" + "            required='true'\n"
            + "            description='The name'/>\n" + "    <foreign-key name='parent' foreignTable='A'>\n"
            + "       <reference local='parentId' foreign='id'/>\n" + "    </foreign-key>\n" + "    <unique>\n"
            + "      <unique-column name='name'/>\n" + "    </unique>\n" + "  </table>\n"
            + "  <table name='B'\n" + "         description='Table B'>\n" + "    <column name='id'\n"
            + "            type='TIMESTAMP'\n" + "            primaryKey='true'\n"
            + "            required='true'\n" + "            description='The primary key of table B'/>\n"
            + "    <column name='aid'\n" + "            type='INTEGER'\n"
            + "            description='The field for the foreign key towards A'/>\n"
            + "    <column name='cid'\n" + "            type='CHAR'\n" + "            size='32'\n"
            + "            description='The field for the foreign key towards C'/>\n"
            + "    <foreign-key foreignTable='A'>\n" + "       <reference local='aid' foreign='id'/>\n"
            + "    </foreign-key>\n" + "    <foreign-key foreignTable='C'>\n"
            + "       <reference local='cid' foreign='id'/>\n" + "    </foreign-key>\n" + "    <index>\n"
            + "      <index-column name='aid'/>\n" + "      <index-column name='cid'/>\n" + "    </index>\n"
            + "  </table>\n" + "  <table name='C'\n" + "         description='Table C'>\n"
            + "    <column name='id'\n" + "            type='CHAR'\n" + "            size='32'\n"
            + "            primaryKey='true'\n" + "            required='true'\n"
            + "            description='The primary key of table C'/>\n" + "    <column name='text'\n"
            + "            type='LONGVARCHAR'\n" + "            description='The text'/>\n"
            + "    <index name='byText'>\n" + "      <index-column name='text'/>\n" + "    </index>\n"
            + "  </table>\n" + "</database>");

    assertEquals("test", model.getName());
    assertEquals(3, model.getTableCount());

    // table A

    Table table = model.getTable(0);

    assertEquals("A", "Table A", 3, 1, 1, 1, 1, table);
    assertEquals("id", Types.INTEGER, 0, 0, null, "The primary key of table A", null, true, true, true,
            table.getColumn(0));
    assertEquals("parentId", Types.INTEGER, 0, 0, null, "The field for the foreign key parent", null, false,
            false, false, table.getColumn(1));
    assertEquals("name", Types.VARCHAR, 32, 0, null, "The name", null, false, true, false, table.getColumn(2));
    assertEquals(table.getColumn(0), table.getAutoIncrementColumns()[0]);

    ForeignKey fk = table.getForeignKey(0);

    assertEquals("parent", CascadeActionEnum.NONE, CascadeActionEnum.NONE, table, 1, fk);
    assertEquals(table.getColumn(1), table.getColumn(0), fk.getFirstReference());

    Index index = table.getIndex(0);

    assertEquals(null, true, 1, index);
    assertEquals(table.getColumn(2), null, index.getColumn(0));

    // table B

    table = model.getTable(1);

    assertEquals("B", "Table B", 3, 1, 0, 2, 1, table);
    assertEquals("id", Types.TIMESTAMP, 0, 0, null, "The primary key of table B", null, true, true, false,
            table.getColumn(0));
    assertEquals("aid", Types.INTEGER, 0, 0, null, "The field for the foreign key towards A", null, false,
            false, false, table.getColumn(1));
    assertEquals("cid", Types.CHAR, 32, 0, null, "The field for the foreign key towards C", null, false, false,
            false, table.getColumn(2));

    fk = table.getForeignKey(0);

    assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, model.getTable(0), 1, fk);
    assertEquals(table.getColumn(1), model.getTable(0).getColumn(0), fk.getFirstReference());

    fk = table.getForeignKey(1);

    assertEquals(null, CascadeActionEnum.NONE, CascadeActionEnum.NONE, model.getTable(2), 1, fk);
    assertEquals(table.getColumn(2), model.getTable(2).getColumn(0), fk.getFirstReference());

    index = table.getIndex(0);

    assertEquals(null, false, 2, index);
    assertEquals(table.getColumn(1), null, index.getColumn(0));
    assertEquals(table.getColumn(2), null, index.getColumn(1));

    // table C

    table = model.getTable(2);

    assertEquals("C", "Table C", 2, 1, 0, 0, 1, table);
    assertEquals("id", Types.CHAR, 32, 0, null, "The primary key of table C", null, true, true, false,
            table.getColumn(0));
    assertEquals("text", Types.LONGVARCHAR, 0, 0, null, "The text", null, false, false, false,
            table.getColumn(1));

    index = table.getIndex(0);

    assertEquals("byText", false, 1, index);
    assertEquals(table.getColumn(1), null, index.getColumn(0));

    assertEquals("<?xml version='1.0' encoding='UTF-8'?>\n" + "<database xmlns=\""
            + DatabaseIO.DDLUTILS_NAMESPACE + "\" name=\"test\">\n"
            + "  <table name=\"A\" description=\"Table A\">\n"
            + "    <column name=\"id\" primaryKey=\"true\" required=\"true\" type=\"INTEGER\" autoIncrement=\"true\" description=\"The primary key of table A\" />\n"
            + "    <column name=\"parentId\" primaryKey=\"false\" required=\"false\" type=\"INTEGER\" autoIncrement=\"false\" description=\"The field for the foreign key parent\" />\n"
            + "    <column name=\"name\" primaryKey=\"false\" required=\"true\" type=\"VARCHAR\" size=\"32\" autoIncrement=\"false\" description=\"The name\" />\n"
            + "    <foreign-key foreignTable=\"A\" name=\"parent\">\n"
            + "      <reference local=\"parentId\" foreign=\"id\" />\n" + "    </foreign-key>\n"
            + "    <unique>\n" + "      <unique-column name=\"name\" />\n" + "    </unique>\n" + "  </table>\n"
            + "  <table name=\"B\" description=\"Table B\">\n"
            + "    <column name=\"id\" primaryKey=\"true\" required=\"true\" type=\"TIMESTAMP\" autoIncrement=\"false\" description=\"The primary key of table B\" />\n"
            + "    <column name=\"aid\" primaryKey=\"false\" required=\"false\" type=\"INTEGER\" autoIncrement=\"false\" description=\"The field for the foreign key towards A\" />\n"
            + "    <column name=\"cid\" primaryKey=\"false\" required=\"false\" type=\"CHAR\" size=\"32\" autoIncrement=\"false\" description=\"The field for the foreign key towards C\" />\n"
            + "    <foreign-key foreignTable=\"A\">\n" + "      <reference local=\"aid\" foreign=\"id\" />\n"
            + "    </foreign-key>\n" + "    <foreign-key foreignTable=\"C\">\n"
            + "      <reference local=\"cid\" foreign=\"id\" />\n" + "    </foreign-key>\n" + "    <index>\n"
            + "      <index-column name=\"aid\" />\n" + "      <index-column name=\"cid\" />\n"
            + "    </index>\n" + "  </table>\n" + "  <table name=\"C\" description=\"Table C\">\n"
            + "    <column name=\"id\" primaryKey=\"true\" required=\"true\" type=\"CHAR\" size=\"32\" autoIncrement=\"false\" description=\"The primary key of table C\" />\n"
            + "    <column name=\"text\" primaryKey=\"false\" required=\"false\" type=\"LONGVARCHAR\" autoIncrement=\"false\" description=\"The text\" />\n"
            + "    <index name=\"byText\">\n" + "      <index-column name=\"text\" />\n" + "    </index>\n"
            + "  </table>\n" + "</database>\n", model);
}

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Retrieves the value of the designated column in the current row
 * of this <code>CachedRowSetImpl</code> object as a
 * <code>java.sql.Date</code> object.
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in the rowset
 * @return the column value as a <code>java.sql.Data</code> object; if
 *        the value is SQL <code>NULL</code>, the
 *        result is <code>null</code>
 * @throws SQLException if the given column index is out of bounds,
 *            the cursor is not on a valid row, or this method fails
 */// w  w  w.java2s. co m
public java.sql.Date getDate(int columnIndex) throws SQLException {
    Object value;

    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    setLastValueNull(false);
    value = getCurrentRow().getColumnObject(columnIndex);

    // check for SQL NULL
    if (value == null) {
        setLastValueNull(true);
        return null;
    }

    /*
     * The object coming back from the db could be
     * a date, a timestamp, or a char field variety.
     * If it's a date type return it, a timestamp
     * we turn into a long and then into a date,
     * char strings we try to parse. Yuck.
     */
    switch (RowSetMD.getColumnType(columnIndex)) {
    case java.sql.Types.DATE: {
        long sec = ((java.sql.Date) value).getTime();
        return new java.sql.Date(sec);
    }
    case java.sql.Types.TIMESTAMP: {
        long sec = ((java.sql.Timestamp) value).getTime();
        return new java.sql.Date(sec);
    }
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
    case java.sql.Types.LONGVARCHAR: {
        java.util.Date parsedDate = parseDate(columnIndex, value);
        return new java.sql.Date(parsedDate.getTime());
    }
    default: {
        throw new SQLException(
                MessageFormat.format(resBundle.handleGetObject("cachedrowsetimpl.datefail").toString(),
                        new Object[] { value.toString().trim(), columnIndex }));
    }
    }
}

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Retrieves the value of the designated column in the current row
 * of this <code>CachedRowSetImpl</code> object as a
 * <code>java.sql.Time</code> object.
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in the rowset
 * @return the column value; if the value is SQL <code>NULL</code>, the
 *         result is <code>null</code>
 * @throws SQLException if the given column index is out of bounds,
 *         the cursor is not on a valid row, or this method fails
 *///w  ww. j ava  2 s  .  co m
public java.sql.Time getTime(int columnIndex) throws SQLException {
    Object value;

    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    setLastValueNull(false);
    value = getCurrentRow().getColumnObject(columnIndex);

    // check for SQL NULL
    if (value == null) {
        setLastValueNull(true);
        return null;
    }

    /*
     * The object coming back from the db could be
     * a date, a timestamp, or a char field variety.
     * If it's a date type return it, a timestamp
     * we turn into a long and then into a date,
     * char strings we try to parse. Yuck.
     */
    switch (RowSetMD.getColumnType(columnIndex)) {
    case java.sql.Types.TIME: {
        return (java.sql.Time) value;
    }
    case java.sql.Types.TIMESTAMP: {
        long sec = ((java.sql.Timestamp) value).getTime();
        return new java.sql.Time(sec);
    }
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
    case java.sql.Types.LONGVARCHAR: {
        java.util.Date parsedDate = parseDate(columnIndex, value);
        return new java.sql.Time(parsedDate.getTime());
    }
    default: {
        throw new SQLException(
                MessageFormat.format(resBundle.handleGetObject("cachedrowsetimpl.timefail").toString(),
                        new Object[] { value.toString().trim(), columnIndex }));
    }
    }
}

From source file:lasige.steeldb.jdbc.BFTRowSet.java

/**
 * Retrieves the value of the designated column in the current row
 * of this <code>CachedRowSetImpl</code> object as a
 * <code>java.sql.Timestamp</code> object.
 *
 * @param columnIndex the first column is <code>1</code>, the second
 *        is <code>2</code>, and so on; must be <code>1</code> or larger
 *        and equal to or less than the number of columns in the rowset
 * @return the column value; if the value is SQL <code>NULL</code>, the
 *         result is <code>null</code>
 * @throws SQLException if the given column index is out of bounds,
 *            the cursor is not on a valid row, or this method fails
 *//*ww w .  jav  a  2 s . com*/
public java.sql.Timestamp getTimestamp(int columnIndex) throws SQLException {
    Object value;

    // sanity check.
    checkIndex(columnIndex);
    // make sure the cursor is on a valid row
    checkCursor();

    setLastValueNull(false);
    value = getCurrentRow().getColumnObject(columnIndex);

    // check for SQL NULL
    if (value == null) {
        setLastValueNull(true);
        return null;
    }

    /*
     * The object coming back from the db could be
     * a date, a timestamp, or a char field variety.
     * If it's a date type return it; a timestamp
     * we turn into a long and then into a date;
     * char strings we try to parse. Yuck.
     */
    switch (RowSetMD.getColumnType(columnIndex)) {
    case java.sql.Types.TIMESTAMP: {
        return (java.sql.Timestamp) value;
    }
    case java.sql.Types.TIME: {
        long sec = ((java.sql.Time) value).getTime();
        return new java.sql.Timestamp(sec);
    }
    case java.sql.Types.DATE: {
        long sec = ((java.sql.Date) value).getTime();
        return new java.sql.Timestamp(sec);
    }
    case java.sql.Types.CHAR:
    case java.sql.Types.VARCHAR:
    case java.sql.Types.LONGVARCHAR: {
        java.util.Date parsedDate = parseDate(columnIndex, value);
        return new Timestamp(parsedDate.getTime());
    }
    default: {
        throw new SQLException(
                MessageFormat.format(resBundle.handleGetObject("cachedrowsetimpl.timefail").toString(),
                        new Object[] { value.toString().trim(), columnIndex }));
    }
    }
}

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.
 * //from  w w  w.  ja v a  2s.c om
 * @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:org.apache.ddlutils.io.TestDatabaseIO.java

/**
 * Tests the Torque/Turbine extensions BOOLEANINT & BOOLEANCHAR.
 *//*from w  w w . j  a v a 2  s  . c  o m*/
public void testTurbineExtension() throws Exception {
    Database model = readModel("<database xmlns='" + DatabaseIO.DDLUTILS_NAMESPACE + "' name='test'>\n"
            + "  <table name='SomeTable'>\n" + "    <column name='intField'\n"
            + "            type='BOOLEANINT'/>\n" + "    <column name='charField'\n"
            + "            type='BOOLEANCHAR' />\n" + "  </table>\n" + "</database>");

    assertEquals("test", model.getName());
    assertEquals(1, model.getTableCount());

    Table table = model.getTable(0);

    assertEquals("SomeTable", null, 2, 0, 0, 0, 0, table);
    assertEquals("intField", Types.TINYINT, 0, 0, null, null, null, false, false, false, table.getColumn(0));
    assertEquals("charField", Types.CHAR, 0, 0, null, null, null, false, false, false, table.getColumn(1));

    assertEquals("<?xml version='1.0' encoding='UTF-8'?>\n" + "<database xmlns=\""
            + DatabaseIO.DDLUTILS_NAMESPACE + "\" name=\"test\">\n" + "  <table name=\"SomeTable\">\n"
            + "    <column name=\"intField\" primaryKey=\"false\" required=\"false\" type=\"TINYINT\" autoIncrement=\"false\" />\n"
            + "    <column name=\"charField\" primaryKey=\"false\" required=\"false\" type=\"CHAR\" autoIncrement=\"false\" />\n"
            + "  </table>\n" + "</database>\n", model);
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Insert the properties for the Alert definition and retrieve the new
 * database generated ID for this Alert.
 *
 * @param rec_/*from   w  w w . j av  a 2 s  .c  o  m*/
 *        The Alert to be stored in the database.
 * @return 0 if successful, otherwise the database error code.
 * @throws java.sql.SQLException
 *         On error with rollback().
 */
private int insertProperties(AlertRec rec_) throws SQLException {
    // Define the SQL insert. Remember date_created, date_modified, creator
    // and modifier are controlled
    // by triggers. Also (as of 10/21/2004) after the insert the
    // date_modified is still set by the insert
    // trigger.
    String insert = "begin insert into sbrext.sn_alert_view_ext "
            + "(name, auto_freq_unit, al_status, begin_date, end_date, status_reason, auto_freq_value, created_by) "
            + "values (?, ?, ?, ?, ?, ?, ?, ?) return al_idseq into ?; end;";

    CallableStatement pstmt = null;
    int rc = 0;
    cleanRec(rec_);
    try {
        // Set all the SQL arguments.
        pstmt = _conn.prepareCall(insert);
        pstmt.setString(1, rec_.getName());
        pstmt.setString(2, rec_.getFreqString());
        pstmt.setString(3, rec_.getActiveString());
        pstmt.setTimestamp(4, rec_.getStart());
        pstmt.setTimestamp(5, rec_.getEnd());
        pstmt.setString(6, rec_.getInactiveReason(false));
        pstmt.setInt(7, rec_.getDay());
        pstmt.setString(8, _user);
        pstmt.registerOutParameter(9, Types.CHAR);

        // Insert the new record and flag a commit for later.
        pstmt.executeUpdate();

        // We need the record id to populate the foreign keys for other
        // tables.
        rec_.setAlertRecNum(pstmt.getString(9));
    } catch (SQLException ex) {
        // Ooops...
        rec_.setAlertRecNum(null);
        _conn.rollback();
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + insert + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        rc = _errorCode;
    } finally {
        closeCursors(pstmt, null);
    }
    return rc;
}

From source file:gov.nih.nci.cadsr.sentinel.database.DBAlertOracle.java

/**
 * Insert the Report details for the Alert definition into the database and
 * retrieve the new report id.//w  w  w.  j  a v a2s. co m
 *
 * @param rec_
 *        The Alert definition to be stored in the database.
 * @return 0 if successful, otherwise the database error code.
 * @throws java.sql.SQLException
 *         On error with rollback().
 */
private int insertReport(AlertRec rec_) throws SQLException {
    // Add the Report record.
    String insert = "begin insert into sbrext.sn_report_view_ext "
            + "(al_idseq, comments, include_property_ind, style, send, acknowledge_ind, assoc_lvl_num, created_by) "
            + "values (?, ?, ?, ?, ?, ?, ?, ?) return rep_idseq into ?; end;";

    CallableStatement pstmt = null;
    int rc = 0;
    try {
        pstmt = _conn.prepareCall(insert);
        pstmt.setString(1, rec_.getAlertRecNum());
        pstmt.setString(2, rec_.getIntro(false));
        pstmt.setString(3, rec_.getIncPropSectString());
        pstmt.setString(4, rec_.getReportStyleString());
        pstmt.setString(5, rec_.getReportEmptyString());
        pstmt.setString(6, rec_.getReportAckString());
        pstmt.setInt(7, rec_.getIAssocLvl());
        pstmt.setString(8, _user);
        pstmt.registerOutParameter(9, Types.CHAR);
        pstmt.executeUpdate();

        // We need the record id to populate the foreign keys for other
        // tables.
        rec_.setReportRecNum(pstmt.getString(9));
    } catch (SQLException ex) {
        // Ooops...
        rec_.setAlertRecNum(null);
        _conn.rollback();
        _errorCode = DBAlertUtil.getSQLErrorCode(ex);
        _errorMsg = _errorCode + ": " + insert + "\n\n" + ex.toString();
        _logger.error(_errorMsg);
        rc = _errorCode;
    } finally {
        closeCursors(pstmt, null);
    }
    return rc;
}