Example usage for java.sql PreparedStatement setDouble

List of usage examples for java.sql PreparedStatement setDouble

Introduction

In this page you can find the example usage for java.sql PreparedStatement setDouble.

Prototype

void setDouble(int parameterIndex, double x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java double value.

Usage

From source file:net.solarnetwork.node.dao.jdbc.power.JdbcPowerDatumDao.java

@Override
protected void setStoreStatementValues(PowerDatum datum, PreparedStatement ps) throws SQLException {
    int col = 1;/*w  ww . j  av  a 2 s  . co m*/
    ps.setTimestamp(col++, new java.sql.Timestamp(
            datum.getCreated() == null ? System.currentTimeMillis() : datum.getCreated().getTime()));
    ps.setString(col++, datum.getSourceId() == null ? "" : datum.getSourceId());
    if (datum.getLocationId() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getLocationId());
    }
    if (datum.getWatts() == null) {
        ps.setNull(col++, Types.INTEGER);
    } else {
        ps.setInt(col++, datum.getWatts());
    }
    if (datum.getBatteryVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getBatteryVolts());
    }
    if (datum.getBatteryAmpHours() == null) {
        ps.setNull(col++, Types.DOUBLE);
    } else {
        ps.setDouble(col++, datum.getBatteryAmpHours());
    }
    if (datum.getDcOutputVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getDcOutputVolts());
    }
    if (datum.getDcOutputAmps() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getDcOutputAmps());
    }
    if (datum.getAcOutputVolts() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getAcOutputVolts());
    }
    if (datum.getAcOutputAmps() == null) {
        ps.setNull(col++, Types.FLOAT);
    } else {
        ps.setFloat(col++, datum.getAcOutputAmps());
    }
    if (datum.getWattHourReading() == null) {
        ps.setNull(col++, Types.BIGINT);
    } else {
        ps.setLong(col++, datum.getWattHourReading());
    }
    if (datum.getAmpHourReading() == null) {
        ps.setNull(col++, Types.DOUBLE);
    } else {
        ps.setDouble(col++, datum.getAmpHourReading());
    }
}

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

@Override
public void publish(String topic, Object message, BrokerConfiguration brokerConfiguration)
        throws BrokerEventProcessingException {
    try {/*  w w  w.j a v  a 2 s .c o m*/
        String[] topicItems = topic.split("\\.");
        String databaseName = topicItems[0].trim();
        String tableName = topicItems[1].trim();

        ConcurrentHashMap<String, Connection> connectionMap = jdbcConnections.get(brokerConfiguration);

        if (connectionMap == null) {
            connectionMap = new ConcurrentHashMap<String, Connection>();
            jdbcConnections.put(brokerConfiguration, connectionMap);
        }
        Connection connection = connectionMap.get(databaseName);

        if (connection == null) {
            Class.forName(driverClassName);
            connection = DriverManager.getConnection(urlPrefix + "://"
                    + brokerConfiguration.getProperties().get(BROKER_CONF_RDBMS_PROPERTY_HOSTNAME_REFERENCE)
                    + ":" + brokerConfiguration.getProperties().get(BROKER_CONF_RDBMS_PROPERTY_PORT_REFERENCE)
                    + "/" + databaseName + "?user="
                    + brokerConfiguration.getProperties().get(BROKER_CONF_RDBMS_PROPERTY_USER_NAME_REFERENCE)
                    + "&password="
                    + brokerConfiguration.getProperties().get(BROKER_CONF_RDBMS_PROPERTY_PASSWORD_REFERENCE));
            connectionMap.put(databaseName, connection);
        }

        ConcurrentHashMap<String, TableInfo> tableInfoMap = tables.get(brokerConfiguration);
        TableInfo tableInfo;
        if (tableInfoMap == null || tableInfoMap.get(topic) == null) {
            tableInfo = initializeTableInfo(databaseName, tableName, message, brokerConfiguration, connection);
            if (tableInfoMap == null) {
                tableInfoMap = new ConcurrentHashMap<String, TableInfo>();
                tables.put(brokerConfiguration, tableInfoMap);
            }
        } else {
            tableInfo = tableInfoMap.get(topic);
        }

        PreparedStatement preparedStatement = tableInfo.getPreparedStatement();
        Map<String, Object> map = (Map<String, Object>) message;
        Attribute attribute;
        for (int i = 0; i < tableInfo.getColumnOrder().size(); i++) {
            attribute = tableInfo.getColumnOrder().get(i);
            switch (attribute.getType()) {
            case INT:
                preparedStatement.setInt(i + 1, (Integer) map.get(attribute.getName()));
                break;
            case LONG:
                preparedStatement.setLong(i + 1, (Long) map.get(attribute.getName()));
                break;
            case FLOAT:
                preparedStatement.setFloat(i + 1, (Float) map.get(attribute.getName()));
                break;
            case DOUBLE:
                preparedStatement.setDouble(i + 1, (Double) map.get(attribute.getName()));
                break;
            case STRING:
                preparedStatement.setString(i + 1, (String) map.get(attribute.getName()));
                break;
            case BOOL:
                preparedStatement.setBoolean(i + 1, (Boolean) map.get(attribute.getName()));
                break;
            }
        }
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        log.error(e);
    } catch (ClassNotFoundException e) {
        log.error(e);
    }
}

From source file:no.polaric.aprsdb.MyDBSession.java

/**
 * Get list of signs in a specified geographic area and above a specified scale 
 *//*ww w. ja va2s .  c  o m*/
public DbList<Signs.Item> getSigns(long scale, Reference uleft, Reference lright) throws java.sql.SQLException {
    PreparedStatement stmt = getCon().prepareStatement(
            " SELECT s.id AS sid, position, maxscale, url, description, cl.name, s.icon AS sicon, cl.icon AS cicon "
                    + " FROM \"Signs\" s LEFT JOIN \"SignClass\" cl ON s.class=cl.id"
                    + " WHERE maxscale>=? AND position && ST_MakeEnvelope(?, ?, ?, ?, 4326) AND NOT s.hidden"
                    + " LIMIT 200",
            ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
    stmt.setLong(1, scale);
    LatLng ul = uleft.toLatLng();
    LatLng lr = lright.toLatLng();
    stmt.setDouble(2, ul.getLng());
    stmt.setDouble(3, ul.getLat());
    stmt.setDouble(4, lr.getLng());
    stmt.setDouble(5, lr.getLat());
    stmt.setMaxRows(200);

    return new DbList(stmt.executeQuery(), rs -> {
        String icon = rs.getString("sicon");
        if (icon == null)
            icon = rs.getString("cicon");

        // Item (Reference r, long sc, String ic, String url, String txt)
        return new Signs.Item(rs.getInt("sid"), getRef(rs, "position"), 0, icon, rs.getString("url"),
                rs.getString("description"));
    });
}

From source file:org.jtotus.database.LocalJDBC.java

public void storeData(String stockName, DateTime date, BigDecimal value, String type) {
    PreparedStatement pstm = null;
    Connection connection = null;
    try {/*w w  w  .  j  av a  2 s.c  o m*/

        String table = this.normTableName(stockName);
        connection = this.getConnection();
        //upsert
        this.createTable(connection, table);

        String query = "MERGE INTO " + table + " (ID,DATE," + type + ") VALUES((SELECT ID FROM " + table
                + " ID WHERE DATE=?), ?, ?)";
        pstm = connection.prepareStatement(query);

        java.sql.Date sqlDate = new java.sql.Date(date.getMillis());
        pstm.setDate(1, sqlDate);
        pstm.setDate(2, sqlDate);

        System.out.printf("Inserting :%f :%s time:%s\n", value.doubleValue(), stockName,
                date.toDate().toString());
        pstm.setDouble(3, value.doubleValue());
        pstm.execute();

    } catch (SQLException ex) {
        Logger.getLogger(LocalJDBC.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (pstm != null) {
                pstm.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(LocalJDBC.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

From source file:org.eclipse.ecr.core.storage.sql.jdbc.dialect.DialectOracle.java

@Override
public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column)
        throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
    case Types.CLOB:
        setToPreparedStatementString(ps, index, value, column);
        return;/* w  w w  . j  a  v  a2  s . com*/
    case Types.BIT:
        ps.setBoolean(index, ((Boolean) value).booleanValue());
        return;
    case Types.TINYINT:
    case Types.SMALLINT:
        ps.setInt(index, ((Long) value).intValue());
        return;
    case Types.INTEGER:
    case Types.BIGINT:
        ps.setLong(index, ((Long) value).longValue());
        return;
    case Types.DOUBLE:
        ps.setDouble(index, ((Double) value).doubleValue());
        return;
    case Types.TIMESTAMP:
        setToPreparedStatementTimestamp(ps, index, value, column);
        return;
    default:
        throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
    }
}

From source file:com.thinkmore.framework.orm.hibernate.SimpleHibernateDao.java

public void setParameters(PreparedStatement ps, int j, Object value) throws SQLException {
    if (value != null) {
        if (value instanceof java.lang.Integer) {
            ps.setInt(j, (Integer) value);
        } else if (value instanceof java.lang.Long) {
            ps.setLong(j, (Long) value);
        } else if (value instanceof java.util.Date) {
            ps.setTimestamp(j, new java.sql.Timestamp(((Date) value).getTime()));
        } else if (value instanceof java.sql.Date) {
            ps.setDate(j, new java.sql.Date(((Date) value).getTime()));
        } else if (value instanceof java.lang.String) {
            ps.setString(j, value.toString());
        } else if (value instanceof java.lang.Double) {
            ps.setDouble(j, (Double) value);
        } else if (value instanceof java.lang.Byte) {
            ps.setByte(j, (Byte) value);
        } else if (value instanceof java.lang.Character) {
            ps.setString(j, value.toString());
        } else if (value instanceof java.lang.Float) {
            ps.setFloat(j, (Float) value);
        } else if (value instanceof java.lang.Boolean) {
            ps.setBoolean(j, (Boolean) value);
        } else if (value instanceof java.lang.Short) {
            ps.setShort(j, (Short) value);
        } else {// w  ww. ja v  a 2 s.  c  o  m
            ps.setObject(j, value);
        }
    } else {
        ps.setNull(j, Types.NULL);
    }
}

From source file:org.wso2.carbon.event.output.adaptor.mysql.MysqlEventAdaptorType.java

private boolean populateStatement(Map<String, Object> map, PreparedStatement stmt,
        ArrayList<Attribute> colOrder, boolean terminateOnNullValues) throws SQLException {
    Attribute attribute;//from w  w w .  j ava 2  s.c  o  m
    for (int i = 0; i < colOrder.size(); i++) {
        attribute = colOrder.get(i);
        Object value = map.get(attribute.getName());
        if (value != null) {
            switch (attribute.getType()) {
            case INT:
                stmt.setInt(i + 1, (Integer) value);
                break;
            case LONG:
                stmt.setLong(i + 1, (Long) value);
                break;
            case FLOAT:
                stmt.setFloat(i + 1, (Float) value);
                break;
            case DOUBLE:
                stmt.setDouble(i + 1, (Double) value);
                break;
            case STRING:
                stmt.setString(i + 1, (String) value);
                break;
            case BOOL:
                stmt.setBoolean(i + 1, (Boolean) value);
                break;
            }
        } else if (terminateOnNullValues) {
            return false;
        }
    }
    return true;
}

From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java

private void setArgument(PreparedStatement pstmt, String argument, int targetSqlType, int index)
        throws SQLException {
    switch (targetSqlType) {
    case Types.INTEGER:
        pstmt.setInt(index, Integer.parseInt(argument));
        break;/*from w  w  w .  ja va2 s . c om*/
    case Types.DECIMAL:
    case Types.NUMERIC:
        pstmt.setBigDecimal(index, new BigDecimal(argument));
        break;
    case Types.DOUBLE:
    case Types.FLOAT:
        pstmt.setDouble(index, Double.parseDouble(argument));
        break;
    case Types.CHAR:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
        pstmt.setString(index, argument);
        break;
    case Types.BIT:
    case Types.BOOLEAN:
        pstmt.setBoolean(index, Boolean.parseBoolean(argument));
        break;
    case Types.BIGINT:
        pstmt.setLong(index, Long.parseLong(argument));
        break;
    case Types.DATE:
        pstmt.setDate(index, Date.valueOf(argument));
        break;
    case Types.REAL:
        pstmt.setFloat(index, Float.parseFloat(argument));
        break;
    case Types.TINYINT:
        pstmt.setByte(index, Byte.parseByte(argument));
        break;
    case Types.SMALLINT:
        pstmt.setShort(index, Short.parseShort(argument));
        break;
    case Types.TIMESTAMP:
        pstmt.setTimestamp(index, Timestamp.valueOf(argument));
        break;
    case Types.TIME:
        pstmt.setTime(index, Time.valueOf(argument));
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        pstmt.setBytes(index, argument.getBytes());
        break;
    case Types.NULL:
        pstmt.setNull(index, targetSqlType);
        break;
    default:
        pstmt.setObject(index, argument, targetSqlType);
    }
}

From source file:org.wso2.carbon.device.mgt.core.device.details.mgt.dao.impl.DeviceDetailsDAOImpl.java

@Override
public void addDeviceInformation(int deviceId, int enrolmentId, DeviceInfo deviceInfo)
        throws DeviceDetailsMgtDAOException {

    Connection conn;/*from  w ww .j a va  2 s. c  o m*/
    PreparedStatement stmt = null;
    try {
        conn = this.getConnection();

        stmt = conn.prepareStatement("INSERT INTO DM_DEVICE_DETAIL (DEVICE_ID, DEVICE_MODEL, "
                + "VENDOR, OS_VERSION, OS_BUILD_DATE, BATTERY_LEVEL, INTERNAL_TOTAL_MEMORY, INTERNAL_AVAILABLE_MEMORY, "
                + "EXTERNAL_TOTAL_MEMORY, EXTERNAL_AVAILABLE_MEMORY,  CONNECTION_TYPE, "
                + "SSID, CPU_USAGE, TOTAL_RAM_MEMORY, AVAILABLE_RAM_MEMORY, PLUGGED_IN, UPDATE_TIMESTAMP, ENROLMENT_ID) "
                + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        stmt.setInt(1, deviceId);
        stmt.setString(2, deviceInfo.getDeviceModel());
        stmt.setString(3, deviceInfo.getVendor());
        stmt.setString(4, deviceInfo.getOsVersion());
        stmt.setString(5, deviceInfo.getOsBuildDate());
        stmt.setDouble(6, deviceInfo.getBatteryLevel());
        stmt.setDouble(7, deviceInfo.getInternalTotalMemory());
        stmt.setDouble(8, deviceInfo.getInternalAvailableMemory());
        stmt.setDouble(9, deviceInfo.getExternalTotalMemory());
        stmt.setDouble(10, deviceInfo.getExternalAvailableMemory());
        stmt.setString(11, deviceInfo.getConnectionType());
        stmt.setString(12, deviceInfo.getSsid());
        stmt.setDouble(13, deviceInfo.getCpuUsage());
        stmt.setDouble(14, deviceInfo.getTotalRAMMemory());
        stmt.setDouble(15, deviceInfo.getAvailableRAMMemory());
        stmt.setBoolean(16, deviceInfo.isPluggedIn());
        stmt.setLong(17, System.currentTimeMillis());
        stmt.setInt(18, enrolmentId);

        stmt.execute();

    } catch (SQLException e) {
        throw new DeviceDetailsMgtDAOException("Error occurred while inserting device details to database.", e);
    } finally {
        DeviceManagementDAOUtil.cleanupResources(stmt, null);
    }

}

From source file:org.wso2.carbon.social.sql.SQLActivityPublisher.java

@Override
public int warmUpRatingCache(String targetId) throws SocialActivityException {
    PreparedStatement insertCacheWarmUpStatement;
    Connection connection;//from w  w w  .j  a v  a 2  s  . co  m
    String tenantDomain = SocialUtil.getTenantDomain();
    String errorMessage = "Unable to publish the target: " + targetId + " in to the rating cache.";

    try {
        if (log.isDebugEnabled()) {
            log.debug("Executing: " + INSERT_CACHE_SQL);
        }
        connection = DSConnection.getConnection();
        insertCacheWarmUpStatement = connection.prepareStatement(INSERT_CACHE_SQL);
        insertCacheWarmUpStatement.setString(1, targetId);
        insertCacheWarmUpStatement.setInt(2, 0);
        insertCacheWarmUpStatement.setInt(3, 0);
        insertCacheWarmUpStatement.setDouble(4, 0);
        insertCacheWarmUpStatement.setString(5, tenantDomain);

        int returnVal = insertCacheWarmUpStatement.executeUpdate();
        return returnVal;

    } catch (SQLException e) {
        log.error(errorMessage + e.getMessage(), e);
        throw new SocialActivityException(errorMessage, e);
    } catch (DataSourceException e) {
        log.error(errorMessage + e.getMessage(), e);
        throw new SocialActivityException(errorMessage, e);
    }
}