Example usage for java.sql Types FLOAT

List of usage examples for java.sql Types FLOAT

Introduction

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

Prototype

int FLOAT

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

Click Source Link

Document

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

Usage

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Set given value on given PreparedStatement at given index with given SQL type.
 * /*  ww w .  ja  va 2s.co m*/
 * @param stmt The PreparedStatement to set value on.
 * @param index The index of the value in the PreparedStatement.
 * @param value The value to set.
 * @param sqlType The SQL type of the value.
 */
public static void setValue(final PreparedStatement stmt, final int index, final Object value,
        final int sqlType) {
    try {
        if (value == null) {
            stmt.setNull(index, sqlType);
        } else {
            // Special processing for BLOB and CLOB types, because they are mapped
            // by Castor to java.io.InputStream and java.io.Reader, respectively,
            // while JDBC driver expects java.sql.Blob and java.sql.Clob.
            switch (sqlType) {
            case Types.FLOAT:
            case Types.DOUBLE:
                stmt.setDouble(index, ((Double) value).doubleValue());
                break;
            case Types.REAL:
                stmt.setFloat(index, ((Float) value).floatValue());
                break;
            case Types.TIME:
                final Time time = value instanceof java.util.Date ? new Time(((java.util.Date) value).getTime())
                        : null;
                stmt.setTime(index, time != null ? time : (Time) value, getCalendar());
                break;
            case Types.DATE:
                final Date date = value instanceof java.util.Date ? new Date(((java.util.Date) value).getTime())
                        : null;
                stmt.setDate(index, date != null ? date : (Date) value);
                break;
            case Types.TIMESTAMP:
                final Timestamp timestamp = value instanceof java.util.Date
                        ? new Timestamp(((java.util.Date) value).getTime())
                        : null;
                stmt.setTimestamp(index, timestamp != null ? timestamp : (Timestamp) value, getCalendar());
                break;
            case Types.BLOB:
                try {
                    InputStream stream;
                    if (value instanceof byte[]) {
                        stream = new ByteArrayInputStream((byte[]) value);
                    } else {
                        stream = (InputStream) value;
                    }
                    stmt.setBinaryStream(index, stream, stream.available());
                } catch (IOException ex) {
                    throw new SQLException(ex.toString());
                }
                break;
            case Types.CLOB:
                if (value instanceof String) {
                    stmt.setCharacterStream(index, new StringReader((String) value),
                            Math.min(((String) value).length(), Integer.MAX_VALUE));
                } else {
                    stmt.setCharacterStream(index, ((Clob) value).getCharacterStream(),
                            (int) Math.min(((Clob) value).length(), Integer.MAX_VALUE));
                }
                break;
            default:
                stmt.setObject(index, value, sqlType);
                break;
            }
        }
    } catch (SQLException ex) {
        LOG.error("Unexpected SQL exception: ", ex);
    }
}

From source file:org.hibernate.dialect.Dialect.java

protected Dialect() {
    log.info("Using dialect: " + this);
    sqlFunctions.putAll(STANDARD_AGGREGATE_FUNCTIONS);

    // standard sql92 functions (can be overridden by subclasses)
    registerFunction("substring", new SQLFunctionTemplate(Hibernate.STRING, "substring(?1, ?2, ?3)"));
    registerFunction("locate", new SQLFunctionTemplate(Hibernate.INTEGER, "locate(?1, ?2, ?3)"));
    registerFunction("trim", new SQLFunctionTemplate(Hibernate.STRING, "trim(?1 ?2 ?3 ?4)"));
    registerFunction("length", new StandardSQLFunction("length", Hibernate.INTEGER));
    registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.INTEGER));
    registerFunction("coalesce", new StandardSQLFunction("coalesce"));
    registerFunction("nullif", new StandardSQLFunction("nullif"));
    registerFunction("abs", new StandardSQLFunction("abs"));
    registerFunction("mod", new StandardSQLFunction("mod", Hibernate.INTEGER));
    registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE));
    registerFunction("upper", new StandardSQLFunction("upper"));
    registerFunction("lower", new StandardSQLFunction("lower"));
    registerFunction("cast", new CastFunction());
    registerFunction("extract", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(?1 ?2 ?3)"));

    //map second/minute/hour/day/month/year to ANSI extract(), override on subclasses
    registerFunction("second", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(second from ?1)"));
    registerFunction("minute", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(minute from ?1)"));
    registerFunction("hour", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(hour from ?1)"));
    registerFunction("day", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(day from ?1)"));
    registerFunction("month", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(month from ?1)"));
    registerFunction("year", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(year from ?1)"));

    registerFunction("str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as char)"));

    // register hibernate types for default use in scalar sqlquery type auto detection
    registerHibernateType(Types.BIGINT, Hibernate.BIG_INTEGER.getName());
    registerHibernateType(Types.BINARY, Hibernate.BINARY.getName());
    registerHibernateType(Types.BIT, Hibernate.BOOLEAN.getName());
    registerHibernateType(Types.CHAR, Hibernate.CHARACTER.getName());
    registerHibernateType(Types.DATE, Hibernate.DATE.getName());
    registerHibernateType(Types.DOUBLE, Hibernate.DOUBLE.getName());
    registerHibernateType(Types.FLOAT, Hibernate.FLOAT.getName());
    registerHibernateType(Types.INTEGER, Hibernate.INTEGER.getName());
    registerHibernateType(Types.SMALLINT, Hibernate.SHORT.getName());
    registerHibernateType(Types.TINYINT, Hibernate.BYTE.getName());
    registerHibernateType(Types.TIME, Hibernate.TIME.getName());
    registerHibernateType(Types.TIMESTAMP, Hibernate.TIMESTAMP.getName());
    registerHibernateType(Types.VARCHAR, Hibernate.STRING.getName());
    registerHibernateType(Types.VARBINARY, Hibernate.BINARY.getName());
    registerHibernateType(Types.NUMERIC, Hibernate.BIG_DECIMAL.getName());
    registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());
    registerHibernateType(Types.BLOB, Hibernate.BLOB.getName());
    registerHibernateType(Types.CLOB, Hibernate.CLOB.getName());
    registerHibernateType(Types.REAL, Hibernate.FLOAT.getName());
}

From source file:org.apache.ddlutils.model.Column.java

/**
 * Tries to parse the default value of the column and returns it as an object of the
 * corresponding java type. If the value could not be parsed, then the original
 * definition is returned./*  w ww  .  jav  a 2 s  .co  m*/
 * 
 * @return The parsed default value
 */
public Object getParsedDefaultValue() {
    if ((_defaultValue != null) && (_defaultValue.length() > 0)) {
        try {
            switch (_typeCode) {
            case Types.TINYINT:
            case Types.SMALLINT:
                return new Short(_defaultValue);
            case Types.INTEGER:
                return new Integer(_defaultValue);
            case Types.BIGINT:
                return new Long(_defaultValue);
            case Types.DECIMAL:
            case Types.NUMERIC:
                return new BigDecimal(_defaultValue);
            case Types.REAL:
                return new Float(_defaultValue);
            case Types.DOUBLE:
            case Types.FLOAT:
                return new Double(_defaultValue);
            case Types.DATE:
                return Date.valueOf(_defaultValue);
            case Types.TIME:
                return Time.valueOf(_defaultValue);
            case Types.TIMESTAMP:
                return Timestamp.valueOf(_defaultValue);
            case Types.BIT:
            case Types.BOOLEAN:
                return ConvertUtils.convert(_defaultValue, Boolean.class);
            }
        } catch (NumberFormatException ex) {
            return null;
        } catch (IllegalArgumentException ex) {
            return null;
        }
    }
    return _defaultValue;
}

From source file:org.jumpmind.symmetric.db.derby.DerbyFunctions.java

public static String getPrimaryKeyWhereString(String[] pkColumnNames, ResultSet rs) throws SQLException {
    final String AND = " and ";
    ResultSetMetaData metaData = rs.getMetaData();
    StringBuilder b = new StringBuilder();
    for (int i = 0; i < pkColumnNames.length; i++) {
        String columnName = pkColumnNames[i];
        int index = findColumnIndex(metaData, columnName);
        int type = metaData.getColumnType(index);
        if (type != Types.BINARY && type != Types.BLOB && type != Types.LONGVARBINARY
                && type != Types.VARBINARY) {
            b.append("\"").append(columnName).append("\"=");
            switch (type) {
            case Types.BIT:
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
            case Types.BIGINT:
            case Types.FLOAT:
            case Types.REAL:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.DECIMAL:
            case Types.BOOLEAN:
                b.append(rs.getObject(index));
                break;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                b.append("\"").append(rs.getString(index)).append("\"");
                break;
            case Types.DATE:
            case Types.TIMESTAMP:
                b.append("{ts '");
                b.append(rs.getString(index));
                b.append("'}");
                break;
            }/*  w w  w.  jav a  2  s .  com*/
            b.append(AND);
        }
    }
    b.replace(b.length() - AND.length(), b.length(), "");
    return b.toString();
}

From source file:com.nextep.designer.sqlclient.ui.connectors.SQLResultConnector.java

private void initializeTable(ISQLQuery query, long executionTime, INextepMetadata md) {
    colMaxWidth = new int[md.getColumnCount() + 1];
    // Checking columns to display
    int displayedColumns = query.getDisplayedColumnsCount();
    if (displayedColumns <= 0) {
        displayedColumns = md.getColumnCount();
    }/*from  w  w w. jav a2s.co  m*/
    for (int i = 1; i <= displayedColumns; i++) {
        final String colName = md.getColumnName(i);
        // final int colPrecision = md.getPrecision(index);
        final int colType = md.getColumnType(i);

        final int colIndex = i - 1;
        if (!sqlView.isDisposed()) {
            TableColumn c = new TableColumn(sqlView, SWT.NONE);
            c.setText(colName);
            c.setWidth(colName.length() * 8);
            c.addListener(SWT.Selection, comparator);
            colMaxWidth[colIndex] = c.getWidth();
            SQLResultLabelProvider lblProvider = new SQLResultLabelProvider(this, colIndex);
            final TableViewerColumn viewerCol = new TableViewerColumn(sqlViewer, c);
            viewerCol.setLabelProvider(lblProvider);
            // Registering column comparator
            switch (colType) {
            case Types.BIGINT:
            case Types.BIT:
            case Types.DECIMAL:
            case Types.DOUBLE:
            case Types.FLOAT:
            case Types.INTEGER:
            case Types.NUMERIC:
            case Types.REAL:
            case Types.SMALLINT:
            case Types.TINYINT:
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new NumberComparator());
                // Setting our deserializer which can produce number from string
                c.setData(KEY_DESERIALIZER, NUMBER_DESERIALIZER);
                break;
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                // Defines validation so that user will be notified about problems
                c.setData(KEY_CELL_VALIDATOR, DATE_VALIDATOR);
                // Setting our deserializer which can produce date from string
                c.setData(KEY_DESERIALIZER, DATE_DESERIALIZER);
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new DateComparator());
                break;
            default:
                c.setData(KEY_DESERIALIZER, STRING_DESERIALIZER);
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new StringComparator());
            }
        }
    }
    registerCellEditors();
    setStatusLabel(MessageFormat.format(SQLClientMessages.getString("sql.executionTime"), executionTime),
            false);
    sqlViewer.setInput(query.getResult());
}

From source file:com.squid.core.domain.operators.ExtendedType.java

private String getTypeName(int SQLType) {
    switch (SQLType) {
    case Types.ARRAY:
        return "ARRAY";
    case Types.BIGINT:
        return "INTEGER";
    case Types.BINARY:
        return "BINARY";
    case Types.BIT:
        return "BIT";
    case Types.BLOB:
        return "BLOB";
    case Types.BOOLEAN:
        return "BOOLEAN";
    case Types.CHAR:
        return "CHAR";
    case Types.CLOB:
        return "CLOB";
    case Types.DATALINK:
        return "DATALINK";
    case Types.DATE:
        return "DATE";
    case Types.DECIMAL:
        return "DECIMAL";
    case Types.DOUBLE:
        return "DOUBLE";
    case Types.FLOAT:
        return "FLOAT";
    case Types.INTEGER:
        return "INTEGER";
    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";
    case Types.LONGNVARCHAR:
        return "LONGNVARCHAR";
    case Types.LONGVARBINARY:
        return "LONGVARBINARY";
    case Types.NCHAR:
        return "NCHAR";
    case Types.NCLOB:
        return "NCLOB";
    case Types.NULL:
        return "UNDEFINED";//
    case Types.NUMERIC:
        return "NUMERIC";
    case Types.NVARCHAR:
        return "NVARCHAR";
    case Types.OTHER:
        return "UNDEFINED";//
    case Types.REAL:
        return "REAL";
    case Types.REF:
        return "REF";
    case Types.ROWID:
        return "ROWID";
    case Types.SMALLINT:
        return "SMALLINT";
    case Types.SQLXML:
        return "SQLXML";
    case Types.STRUCT:
        return "STRUCT";
    case Types.TIME:
        return "TIME";
    case Types.TIMESTAMP:
        return "TIMESTAMP";
    case Types.TINYINT:
        return "TINYINT";
    case Types.VARBINARY:
        return "VARBINARY";
    case Types.VARCHAR:
        return "VARCHAR";
    default:/*  w ww . jav  a2s . c  o m*/
        return "UNDEFINED";//
    }
}

From source file:org.wso2.cep.broker.RDBMSBrokerType.java

private TableInfo initializeTableInfo(String databaseName, String tableName, Object message,
        BrokerConfiguration brokerConfiguration, Connection connection) throws SQLException {
    TableInfo tableInfo = new TableInfo();
    tableInfo.setTableName(tableName);/*from  w ww .j av  a2  s  .c om*/

    // create the table.
    StringBuilder stringBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS ");
    stringBuilder.append(tableName);
    stringBuilder.append(" (");
    boolean appendComma = false;
    for (Map.Entry<String, Object> entry : (((Map<String, Object>) message).entrySet())) {
        if (appendComma) {
            stringBuilder.append(",");
        } else {
            appendComma = true;
        }
        stringBuilder.append(entry.getKey()).append("  ");
        if (entry.getValue() instanceof Integer) {
            stringBuilder.append("INT");
        } else if (entry.getValue() instanceof Long) {
            stringBuilder.append("BIGINT");
        } else if (entry.getValue() instanceof Float) {
            stringBuilder.append("FLOAT");
        } else if (entry.getValue() instanceof Double) {
            stringBuilder.append("DOUBLE");
        } else if (entry.getValue() instanceof String) {
            stringBuilder.append("VARCHAR(255)");
        } else if (entry.getValue() instanceof Boolean) {
            stringBuilder.append("BOOL");
        }
    }
    stringBuilder.append(")");
    Statement statement = connection.createStatement();
    statement.executeUpdate(stringBuilder.toString());
    statement.close();

    ArrayList<Attribute> tableColumnList = new ArrayList<Attribute>();
    stringBuilder = new StringBuilder("INSERT INTO ");
    stringBuilder.append(tableName);
    stringBuilder.append(" ( ");
    StringBuilder values = new StringBuilder("");

    appendComma = false;
    DatabaseMetaData databaseMetaData = connection.getMetaData();
    ResultSet rs = databaseMetaData.getColumns(null, null, databaseName + "." + tableName, null);
    while (rs.next()) {
        AttributeType type = null;
        int colType = rs.getInt("DATA_TYPE");
        switch (colType) {
        case Types.VARCHAR:
            type = AttributeType.STRING;
            break;
        case Types.INTEGER:
            type = AttributeType.INT;
            break;
        case Types.BIGINT:
            type = AttributeType.LONG;
            break;
        case Types.DOUBLE:
            type = AttributeType.DOUBLE;
            break;
        case Types.FLOAT:
            type = AttributeType.FLOAT;
            break;
        case Types.BOOLEAN:
            type = AttributeType.BOOL;
            break;

        }
        Attribute attribute = new Attribute(rs.getString("COLUMN_NAME"), type);
        tableColumnList.add(attribute);

        if (appendComma) {
            stringBuilder.append(",");
            values.append(",");
        } else {
            appendComma = true;
        }
        stringBuilder.append(attribute.getName());
        values.append("?");

    }

    stringBuilder.append(") VALUES (");
    stringBuilder.append(values);
    stringBuilder.append(")");
    tableInfo.setColumnOrder(tableColumnList);
    tableInfo.setPreparedStatement(connection.prepareStatement(stringBuilder.toString()));
    return tableInfo;
}

From source file:oscar.util.SqlUtils.java

private static Object getNewType(ResultSet rs, int colNum) {
    int type = 0;
    try {/*from w ww  . jav a 2s.  com*/
        type = rs.getMetaData().getColumnType(colNum);
        switch (type) {
        case Types.LONGVARCHAR:
        case Types.CHAR:
        case Types.VARCHAR:
            return oscar.Misc.getString(rs, colNum);
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            return new Integer(rs.getInt(colNum));
        case Types.BIGINT:
            return new Long(rs.getLong(colNum));
        case Types.FLOAT:
        case Types.DECIMAL:
        case Types.REAL:
        case Types.DOUBLE:
        case Types.NUMERIC:
            return new Double(rs.getDouble(colNum));
        // case Types.B
        case Types.BIT:
            return new Boolean(rs.getBoolean(colNum));
        case Types.TIMESTAMP:
        case Types.DATE:
        case Types.TIME:
            return rs.getDate(colNum);
        default:
            return rs.getObject(colNum);
        }
    } catch (Exception e) {
        MiscUtils.getLogger().error("Error", e);
    }
    return null;
}

From source file:org.sakaiproject.webservices.SakaiReport.java

protected String toJsonString(ResultSet rs) throws SQLException, JSONException {
    ResultSetMetaData rsmd = rs.getMetaData();
    JSONArray array = new JSONArray();
    int numColumns = rsmd.getColumnCount();

    while (rs.next()) {

        JSONObject obj = new JSONObject();
        for (int i = 1; i < numColumns + 1; i++) {

            String column_label = rsmd.getColumnLabel(i);

            LOG.debug("Column Name=" + column_label + ",type=" + rsmd.getColumnType(i));

            switch (rsmd.getColumnType(i)) {
            case Types.ARRAY:
                obj.put(column_label, rs.getArray(i));
                break;
            case Types.BIGINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.BOOLEAN:
                obj.put(column_label, rs.getBoolean(i));
                break;
            case Types.BLOB:
                obj.put(column_label, rs.getBlob(i));
                break;
            case Types.DOUBLE:
                obj.put(column_label, rs.getDouble(i));
                break;
            case Types.FLOAT:
                obj.put(column_label, rs.getFloat(i));
                break;
            case Types.INTEGER:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.NVARCHAR:
                obj.put(column_label, rs.getNString(i));
                break;
            case Types.VARCHAR:
                obj.put(column_label, rs.getString(i));
                break;
            case Types.TINYINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.SMALLINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.DATE:
                obj.put(column_label, rs.getDate(i));
                break;
            case Types.TIMESTAMP:
                obj.put(column_label, rs.getTimestamp(i));
                break;
            default:
                obj.put(column_label, rs.getObject(i));
                break;
            }/*from w w w.  j a  v a2s  .  c o  m*/

        }
        array.put(obj);

    }
    return array.toString();
}

From source file:org.brucalipto.sqlutil.SQLManager.java

/**
 * Method useful for SQL SELECT//from w  ww  .jav a 2  s. co m
 * @param preparedStatement The prepared statement to execute
 * @param params List of {@link SQLParameter} to use to complete the prepared statement
 * @param outputSQLType A java.sql.Types type of return value
 * @return The {@link SPParameter} containing the returned value
 */
public SQLParameter simpleSelect(final String preparedStatement, SQLParameter[] params,
        final int outputSQLType) {
    final SQLParameter[] parameters;
    if (params == null) {
        parameters = new SQLParameter[0];
        log.debug("Going to execute a query without parameters.");
    } else {
        parameters = (SQLParameter[]) params.clone();
    }
    Connection dbConn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        if (this.dataSource != null) {
            dbConn = this.dataSource.getConnection();
        } else {
            dbConn = this.connection;
        }
        pstmt = dbConn.prepareStatement(preparedStatement);
        for (int i = 0; i < parameters.length; i++) {
            final SQLParameter param = parameters[i];
            log.debug((i + 1) + ") Going to add parameter " + param);
            final int sqlType = param.getSqlType();
            final Object paramValue = param.getValue();
            if (paramValue == null) {
                pstmt.setNull(i + 1, sqlType);
                continue;
            }
            switch (sqlType) {
            case Types.VARCHAR:
                pstmt.setString(i + 1, (String) paramValue);
                break;
            case Types.INTEGER:
                if (paramValue instanceof Integer) {
                    pstmt.setInt(i + 1, ((Integer) paramValue).intValue());
                } else if (paramValue instanceof Long) {
                    pstmt.setLong(i + 1, ((Long) paramValue).longValue());
                }
                break;
            case Types.DATE:
                pstmt.setDate(i + 1, (Date) paramValue);
                break;
            case Types.BOOLEAN:
                pstmt.setBoolean(i + 1, ((Boolean) paramValue).booleanValue());
                break;
            case Types.CHAR:
                pstmt.setString(i + 1, ((Character) paramValue).toString());
                break;
            case Types.DOUBLE:
                pstmt.setDouble(i + 1, ((Double) paramValue).doubleValue());
                break;
            case Types.FLOAT:
                pstmt.setFloat(i + 1, ((Float) paramValue).floatValue());
                break;
            case Types.TIMESTAMP:
                pstmt.setTimestamp(i + 1, (Timestamp) paramValue);
                break;
            default:
                pstmt.setObject(i + 1, paramValue);
                break;
            }
        }

        rs = pstmt.executeQuery();
        log.debug("Prepared statement '" + preparedStatement + "' succesfully executed!");
        while (rs.next()) {
            return new SQLParameter(outputSQLType, (Serializable) rs.getObject(1));
        }
        log.info("Prepared statement '" + preparedStatement + "' returned '0' rows");
    } catch (SQLException e) {
        log.error("Error executing prepared statement '" + preparedStatement + "'", e);
    } catch (Exception e) {
        log.error("Error executing prepared statement '" + preparedStatement + "'", e);
    } finally {
        closeResources(rs, pstmt, dbConn);
    }

    return new SQLParameter(outputSQLType, null);
}