Example usage for java.sql Types DOUBLE

List of usage examples for java.sql Types DOUBLE

Introduction

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

Prototype

int DOUBLE

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

Click Source Link

Document

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

Usage

From source file:com.bigdata.etl.util.DwUtil.java

public static void bulkInsert(String tableName, List<Map<String, String>> lst) {

    ResultSet rs = null;//from ww w  .ja  v a  2s. co m
    java.sql.Statement stmt = null;

    try (java.sql.Connection conn = DataSource.getConnection()) {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select top 0 * from " + tableName);
        try (SQLServerBulkCopy bulk = new SQLServerBulkCopy(url + "user=" + user + ";password=" + password)) {
            SQLServerBulkCopyOptions sqlsbc = new SQLServerBulkCopyOptions();
            sqlsbc.setBulkCopyTimeout(60 * 60 * 1000);
            bulk.setBulkCopyOptions(sqlsbc);
            bulk.setDestinationTableName(tableName);
            ResultSetMetaData rsmd = rs.getMetaData();
            if (lst == null) {
                return;
            }
            // System.out.println(LocalTime.now() + " "+Thread.currentThread().getId()+" "+lst.size());
            try (CachedRowSetImpl x = new CachedRowSetImpl()) {
                x.populate(rs);
                for (int k = 0; k < lst.size(); k++) {
                    Map<String, String> map = lst.get(k);
                    x.last();
                    x.moveToInsertRow();
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        String name = rsmd.getColumnName(i).toUpperCase();
                        int type = rsmd.getColumnType(i);//package java.sql.Type?

                        try {
                            switch (type) {
                            case Types.VARCHAR:
                            case Types.NVARCHAR:
                                int len = rsmd.getColumnDisplaySize(i);
                                String v = map.get(name);
                                if (map.containsKey(name)) {
                                    x.updateString(i, v.length() > len ? v.substring(0, len) : v);
                                } else {
                                    x.updateNull(i);
                                }
                                break;
                            case Types.BIGINT:
                                if (map.containsKey(name) && map.get(name).matches("\\d{1,}")) {
                                    x.updateLong(i, Long.valueOf(map.get(name)));
                                } else {
                                    //   x.updateLong(i, 0);
                                    x.updateNull(i);
                                }
                                break;
                            case Types.FLOAT:
                                if (map.containsKey(name) && map.get(name).matches("([+-]?)\\d*\\.\\d+$")) {
                                    x.updateFloat(i, Float.valueOf(map.get(name)));
                                } else {
                                    x.updateNull(i);

                                }
                                break;
                            case Types.DOUBLE:
                                if (map.containsKey(name) && map.get(name).trim().length() > 0
                                        && StringUtils.isNumeric(map.get(name))) {
                                    x.updateDouble(i, Double.valueOf(map.get(name)));
                                } else {
                                    x.updateNull(i);
                                }
                                break;

                            case Types.INTEGER:
                                if (map.containsKey(name) && map.get(name).matches("\\d{1,}")) {
                                    x.updateInt(i, Integer.valueOf(map.get(name)));
                                } else {
                                    x.updateNull(i);
                                }
                                break;

                            default:
                                throw new RuntimeException("? " + type);
                            }
                            /*
                            if(map.containsKey("SYS_TELECOM"))
                            System.err.println(map.get("SYS_TELECOM"));
                             */
                        } catch (RuntimeException | SQLException e) {
                            Logger.getLogger(DwUtil.class.getName()).log(Level.SEVERE,
                                    "? name=" + name + " v=" + map.get(name), e);
                        }

                    }
                    x.insertRow();
                    x.moveToCurrentRow();
                    //x.acceptChanges();
                }

                long start = System.currentTimeMillis();
                bulk.writeToServer(x);
                long end = System.currentTimeMillis();
                System.out.println(LocalTime.now() + " " + Thread.currentThread().getId() + " "
                        + (end - start) + "ms" + " " + x.size());
            }
        }

    } catch (SQLException e) {
        Logger.getLogger(DwUtil.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(DwUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

From source file:com.draagon.meta.manager.db.driver.MSSQLDriver.java

/**
 * Creates a table in the database/*  ww w  .  ja v  a  2 s .  c om*/
 */
@Override
public void createTable(Connection c, TableDef table) throws SQLException {
    String query = "CREATE TABLE [" + table + "] (\n";

    boolean multi = (table.getPrimaryKeys().size() > 1);

    boolean hasIdentity = false;

    // Create the individual table fields
    int found = 0;
    for (ColumnDef col : table.getColumns()) {
        String name = col.getName();
        if (name == null || name.length() == 0) {
            throw new IllegalArgumentException("No name defined for column [" + col + "]");
        }

        if (found > 0)
            query += ",\n";
        found++;

        String flags = "";
        if (col.isPrimaryKey() && !multi)
            flags = "PRIMARY KEY ";
        else if (col.isUnique())
            flags = "UNIQUE ";
        //else if (getManager().isIndex(mf)) flags = "NONCLUSTERED ";

        switch (col.getSQLType()) {
        case Types.BOOLEAN:
        case Types.BIT:
            query += "[" + name + "] [bit] " + flags;
            break;
        case Types.TINYINT:
            query += "[" + name + "] [tinyint] " + flags;
            break;
        case Types.SMALLINT:
            query += "[" + name + "] [smallint] " + flags;
            break;
        case Types.INTEGER:
            query += "[" + name + "] [int] " + flags;
            break;
        case Types.BIGINT:
            query += "[" + name + "] [bigint] " + flags;
            break;
        case Types.FLOAT:
            query += "[" + name + "] [float] " + flags;
            break;
        case Types.DOUBLE:
            query += "[" + name + "] [decimal](19,4) " + flags;
            break;
        case Types.TIMESTAMP:
            query += "[" + name + "] [datetime] " + flags;
            break;
        case Types.VARCHAR:
            query += "[" + name + "] [varchar](" + col.getLength() + ") " + flags;
            break;

        default:
            throw new IllegalArgumentException("Table [" + table + "] with Column [" + col
                    + "] is of SQL type (" + col.getSQLType() + ") which is not support by this database");
        }

        // Create the identity columns
        if (col.isAutoIncrementor()) {
            if (hasIdentity)
                throw new MetaException(
                        "Table [" + table + "] cannot have multiple identity (auto id) columns!");

            query += "NOT NULL IDENTITY( " + col.getSequence().getStart() + ", "
                    + col.getSequence().getIncrement() + " ) ";

            hasIdentity = true;
        }
    }

    query += "\n)";

    // This means there were no columns defined for the table
    if (found == 0)
        return;

    if (log.isDebugEnabled()) {
        log.debug("Creating table [" + table + "]: " + query);
    }
    //ystem.out.println( ">>>> Creating table [" + table + "]: " + query);

    Statement s = c.createStatement();
    try {
        s.execute(query);
    } finally {
        s.close();
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectDB2.java

@Override
public JDBCInfo getJDBCTypeAndString(ColumnType type) {
    switch (type.spec) {
    case STRING:/*  w ww.j  a  v  a2 s .c o  m*/
        if (type.isUnconstrained()) {
            return jdbcInfo("VARCHAR(255)", Types.VARCHAR);
        } else if (type.isClob() || type.length > 2000) {
            return jdbcInfo("CLOB", Types.CLOB);
        } else {
            return jdbcInfo("VARCHAR(%d)", type.length, Types.VARCHAR);
        }
    case BOOLEAN:
        return jdbcInfo("SMALLINT", Types.BIT);
    case LONG:
        return jdbcInfo("BIGINT", Types.BIGINT);
    case DOUBLE:
        return jdbcInfo("DOUBLE", Types.DOUBLE);
    case TIMESTAMP:
        return jdbcInfo("TIMESTAMP", Types.TIMESTAMP);
    case BLOBID:
        return jdbcInfo("VARCHAR(40)", Types.VARCHAR);
    // -----
    case NODEID:
    case NODEIDFK:
    case NODEIDFKNP:
    case NODEIDFKMUL:
    case NODEIDFKNULL:
    case NODEIDPK:
    case NODEVAL:
        return jdbcInfo("VARCHAR(36)", Types.VARCHAR);
    case SYSNAME:
    case SYSNAMEARRAY:
        return jdbcInfo("VARCHAR(250)", Types.VARCHAR);
    case TINYINT:
        return jdbcInfo("SMALLINT", Types.TINYINT);
    case INTEGER:
        return jdbcInfo("INTEGER", Types.INTEGER);
    case AUTOINC:
        return jdbcInfo("INTEGER", Types.INTEGER); // TODO
    case FTINDEXED:
        return jdbcInfo("CLOB", Types.CLOB);
    case FTSTORED:
        return jdbcInfo("CLOB", Types.CLOB);
    case CLUSTERNODE:
        return jdbcInfo("VARCHAR(25)", Types.VARCHAR);
    case CLUSTERFRAGS:
        return jdbcInfo("VARCHAR(4000)", Types.VARCHAR);
    default:
        throw new AssertionError(type);
    }
}

From source file:io.lightlink.oracle.AbstractOracleType.java

protected ARRAY createArray(Connection con, Object value, String typeName) throws SQLException {

    if (value == null)
        return null;

    ArrayDescriptor arrayStructDesc = safeCreateArrayDescriptor(typeName, con);

    if (value == null)
        return null;

    if (value.getClass().isArray()) {
        value = Arrays.asList((Object[]) value);
    }/*from  ww w. j  a  v a2  s .c  o m*/

    List records = (List) value;
    String baseName = arrayStructDesc.getBaseName();
    int baseType = arrayStructDesc.getBaseType();
    if (baseType == Types.VARCHAR || baseType == Types.CHAR || baseType == Types.CLOB
            || baseType == Types.NUMERIC || baseType == Types.INTEGER || baseType == Types.BIGINT
            || baseType == Types.FLOAT || baseType == Types.DOUBLE || baseType == Types.DECIMAL
            || baseType == Types.NCHAR || baseType == Types.NVARCHAR || baseType == Types.NCLOB) {
        return new ARRAY(arrayStructDesc, con, records.toArray()); // primitive array
    } else {

        Object[] structArray = new Object[records.size()];

        for (int i = 0; i < structArray.length; i++) {
            Object record = records.get(i);

            if (baseType == OracleTypes.JAVA_STRUCT || baseType == OracleTypes.JAVA_OBJECT
                    || baseType == OracleTypes.STRUCT || baseType == OracleTypes.JAVA_STRUCT) {
                record = createStruct(con, record, baseName);
            } else if (baseType == OracleTypes.ARRAY) {
                record = createArray(con, record, baseName);
            }
            structArray[i] = record;
        }

        return new ARRAY(arrayStructDesc, con, structArray);
    }
}

From source file:com.nabla.wapp.server.json.SqlColumn.java

public void write(final ResultSet rs, int column, final JSONObject record) throws SQLException {
    switch (type) {
    case Types.BIGINT:
    case Types.INTEGER:
    case Types.SMALLINT:
    case Types.TINYINT:
        record.put(label, rs.getInt(column));
        break;/*from w ww .  ja v a  2s.  c  o m*/
    case Types.BOOLEAN:
    case Types.BIT:
        record.put(label, rs.getBoolean(column));
        break;
    case Types.DATE:
        final Date dt = rs.getDate(column);
        if (rs.wasNull())
            record.put(label, null);
        else
            record.put(label, new JSonDate(dt));
        return;
    case Types.TIMESTAMP:
        final Timestamp tm = rs.getTimestamp(column);
        if (rs.wasNull())
            record.put(label, null);
        else
            record.put(label, timeStampFormat.format(tm));
        return;
    case Types.DOUBLE:
        record.put(label, rs.getDouble(column));
        break;
    case Types.FLOAT:
        record.put(label, rs.getFloat(column));
        break;
    case Types.NULL:
        record.put(label, null);
        return;
    default:
        record.put(label, rs.getString(column));
        break;
    }
    if (rs.wasNull())
        record.put(label, null);
}

From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java

public String formatJDBCObject(final Object jdbcObject, final int colType) throws SQLException {
    String value = "";

    switch (colType) {
    case Types.BIT:
        if (jdbcObject != null) {
            value = String.valueOf(jdbcObject);
        }/*from  w  w w.jav  a2  s  .c o m*/
        break;
    case Types.BOOLEAN:
        if (jdbcObject != null) {
            value = new Boolean(jdbcObject.toString()).toString();
        }
        break;
    case Types.BIGINT:
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.REAL:
    case Types.NUMERIC:
        if (jdbcObject != null) {
            value = "" + formatter.formatDecimal(jdbcObject);
        }
        break;
    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
        if (jdbcObject != null) {
            value = "" + formatter.formatDecimal(jdbcObject);
        }
        break;
    case Types.CLOB:
        if (jdbcObject != null) {
            try {
                value = read((Clob) jdbcObject);
            } catch (SQLException sqle) {
                value = "";
            } catch (IOException ioe) {
                value = "";
            }
        }
        break;
    case Types.ARRAY:
        try {
            ResultSet rs = ((Array) jdbcObject).getResultSet();
            int arrayType = ((Array) jdbcObject).getBaseType();
            boolean isNotNew = false;
            while (rs.next()) {
                String current = formatJDBCObject(rs.getObject(2), arrayType);
                if ("".equals(current.trim()) == false) {
                    if (isNotNew) {
                        value = value + ",";
                    } else {
                        isNotNew = !isNotNew;
                    }
                    value = value + current;
                }
            }
            if ("".equals(value) == false) {
                value = "{" + value + "}";
            }
        } catch (SQLException sqle) {
            value = "";
        }
        break;
    case Types.JAVA_OBJECT:
        if (jdbcObject != null) {
            value = String.valueOf(jdbcObject);
        }
        break;
    case Types.DATE:
        if (jdbcObject != null) {
            value = formatter.formatDate(jdbcObject);
        }
        break;
    case Types.TIME:
        if (jdbcObject != null) {
            value = ((Time) jdbcObject).toString();
        }
        break;
    case Types.TIMESTAMP:
        if (jdbcObject != null) {
            value = formatter.formatTimestamp(jdbcObject);
        }
        break;
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.CHAR:
        if (jdbcObject != null) {
            value = jdbcObject.toString();
        } else {
            value = "NULL";
        }
        break;
    case Types.BINARY:
        value = new String((byte[]) jdbcObject);
        break;
    default:
        value = "";
    }
    if (value == null) {
        value = "";
    }
    return value;
}

From source file:org.jumpmind.db.platform.h2.H2DdlBuilder.java

public H2DdlBuilder() {

    super(DatabaseNamesConstants.H2);

    databaseInfo.setNonPKIdentityColumnsSupported(false);
    databaseInfo.setIdentityOverrideAllowed(false);
    databaseInfo.setSystemForeignKeyIndicesAlwaysNonUnique(true);
    databaseInfo.setNullAsDefaultValueRequired(false);
    databaseInfo.addNativeTypeMapping(Types.ARRAY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DISTINCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.NULL, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.REF, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.STRUCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DATALINK, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BIT, "BOOLEAN", Types.BIT);
    databaseInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
    databaseInfo.addNativeTypeMapping(Types.BINARY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BLOB, "BLOB", Types.BLOB);
    databaseInfo.addNativeTypeMapping(Types.CLOB, "CLOB", Types.CLOB);
    databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "VARCHAR(" + Integer.MAX_VALUE + ")", Types.VARCHAR);
    databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
    databaseInfo.addNativeTypeMapping(Types.JAVA_OBJECT, "OTHER");

    databaseInfo.setDefaultSize(Types.CHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARCHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.BINARY, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARBINARY, Integer.MAX_VALUE);

    databaseInfo.setNonBlankCharColumnSpacePadded(false);
    databaseInfo.setBlankCharColumnSpacePadded(false);
    databaseInfo.setCharColumnSpaceTrimmed(true);
    databaseInfo.setEmptyStringNulled(false);
    databaseInfo.setNullAsDefaultValueRequired(true);

}

From source file:org.schedoscope.export.jdbc.outputformat.JdbcOutputWritable.java

@Override
public void write(PreparedStatement ps) throws SQLException {

    List<Pair<String, String>> record = fromArrayWritable(value);
    try {//from w  w w.jav  a 2s  .  c  o m
        for (int i = 0; i < record.size(); i++) {

            String type = record.get(i).getLeft().toLowerCase(Locale.getDefault());

            if (type.equals(JdbcOutputWritable.STRING)) {
                if (!record.get(i).getRight().equals("NULL")) {
                    ps.setString(i + 1, record.get(i).getRight());
                } else {
                    ps.setNull(i + 1, Types.VARCHAR);
                }

            } else if (type.equals(JdbcOutputWritable.DOUBLE)) {
                if (!record.get(i).getRight().equals("NULL")) {
                    ps.setDouble(i + 1, Double.parseDouble(record.get(i).getRight()));
                } else {
                    ps.setNull(i + 1, Types.DOUBLE);
                }
            } else if (type.equals(JdbcOutputWritable.BOOLEAN)) {
                if (!record.get(i).getRight().equals("NULL")) {
                    ps.setBoolean(i + 1, Boolean.parseBoolean(record.get(i).getRight()));
                } else {
                    ps.setNull(i + 1, Types.BOOLEAN);
                }
            } else if (type.equals(JdbcOutputWritable.INTEGER)) {
                if (!record.get(i).getRight().equals("NULL")) {
                    ps.setInt(i + 1, Integer.parseInt(record.get(i).getRight()));
                } else {
                    ps.setNull(i + 1, Types.INTEGER);
                }
            } else if (type.equals(JdbcOutputWritable.LONG)) {
                if (!record.get(i).getRight().equals("NULL")) {
                    ps.setLong(i + 1, Long.parseLong(record.get(i).getRight()));
                } else {
                    ps.setNull(i + 1, Types.BIGINT);
                }

            } else {
                LOG.warn("Unknown column type: " + record.get(i).getLeft().toLowerCase(Locale.getDefault()));
                ps.setString(i + 1, record.get(i).getRight());
            }
        }
    } catch (NumberFormatException n) {
        n.printStackTrace();
    }
}

From source file:wzw.sql.ResultSetConverter.java

/**
 * ?/*  w ww.  j av  a2 s  .c o m*/
 * @param rs
 * @param type Listjava.sql.Types 
 * @return
 * @throws SQLException
 */
public static Object toType(ResultSet rs, int type) throws SQLException {

    if (rs.next()) { // run rs.next()
        switch (type) {
        case Types.INTEGER:
            return new Integer(rs.getInt(1));

        case Types.BIGINT:
            return new Long(rs.getLong(1));

        case Types.VARCHAR:
            return rs.getString(1);

        case Types.FLOAT:
            return new Float(rs.getFloat(1));

        case Types.DECIMAL:
        case Types.DOUBLE:
        case Types.NUMERIC:
            return new Double(rs.getDouble(1));

        case Types.TIMESTAMP:
            return rs.getTimestamp(1);

        case Types.DATE:
            return rs.getDate(1);

        case Types.TIME:
            return rs.getTime(1);

        default:
            return null;
        }
    }
    return null;

}

From source file:com.tesora.dve.common.TestDataGenerator.java

protected Object getColumnValue(ColumnMetadata cm) {
    Object cv = null;/*w  w  w.j a v  a 2 s . c  o  m*/
    Calendar cal = Calendar.getInstance();

    switch (cm.getDataType()) {
    case Types.BIT:
    case Types.BOOLEAN:
        cv = Boolean.TRUE;
        break;
    case Types.BIGINT:
        cv = Long.MAX_VALUE;
        break;
    case Types.CHAR:
    case Types.VARCHAR:
        cv = StringUtils.left(baseString, cm.getSize());
        break;
    case Types.SMALLINT:
        cv = Short.MAX_VALUE;
        break;
    case Types.TINYINT:
        cv = Byte.MAX_VALUE;
        break;
    case Types.INTEGER:
        cv = Integer.MAX_VALUE;
        break;
    case Types.DOUBLE:
        cv = new Double(1234.5678); // TODO need to handle s,p
        break;
    case Types.FLOAT:
        cv = new Float(123.56); // TODO need to handle s,p
        break;
    case Types.DECIMAL:
        cv = new BigDecimal("12345.6789"); // TODO need to handle s,p
        break;
    case Types.DATE:
        cal.setTimeInMillis(123456789);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cv = cal.getTime();
        break;
    case Types.TIMESTAMP:
        cal.setTimeInMillis(123456789);
        cv = cal.getTime();
        break;
    case Types.TIME:
        cv = new Time(123456789);
        break;
    default:
        break;
    }

    return cv;
}