Example usage for java.sql PreparedStatement setObject

List of usage examples for java.sql PreparedStatement setObject

Introduction

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

Prototype

void setObject(int parameterIndex, Object x) throws SQLException;

Source Link

Document

Sets the value of the designated parameter using the given object.

Usage

From source file:com.marvinformatics.hibernate.json.JsonUserType.java

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    PGobject dataObject = new PGobject();
    dataObject.setType("json");

    if (value != null)
        dataObject.setValue(convertObjectToJson(value));

    st.setObject(index, dataObject);
}

From source file:nl.tudelft.stocktrader.dal.ConfigServiceDAOImpl.java

public boolean updateConfigService(String url) {
    int updateStatus;
    try {//w  w  w. j a va 2s  .  com
        PreparedStatement statement = connection.prepareStatement(SQL_UPDATE_URL_INTO_CONFIGSERVICE);
        statement.setObject(1, url);
        updateStatus = statement.executeUpdate();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            if (connection != null && !connection.isClosed()) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            logger.error(e, e);
        }
    }
    return updateStatus > 0;
}

From source file:org.netxilia.spi.impl.storage.db.sql.DefaultConnectionWrapper.java

public int queryForInt(String sql, Object... params) {
    PreparedStatement st = null;
    ResultSet rs = null;/*from   w w  w  . j  ava 2s.  c o  m*/
    if (log.isDebugEnabled()) {
        log.debug("QUERY INT:" + sql + " with " + ArrayUtils.toString(params));
    }
    try {
        st = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; ++i) {
            st.setObject(i + 1, params[i]);
        }
        rs = st.executeQuery();
        if (rs.next()) {
            return rs.getInt(1);
        }
        return 0;
    } catch (SQLException e) {
        throw new StorageException(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // silent
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                // silent
            }
        }
    }
}

From source file:fr.aliacom.obm.common.addition.CommitedOperationDaoJdbcImpl.java

@Override
public Contact findAsContact(AccessToken token, String clientId) throws SQLException {

    if (clientId == null) {
        return null;
    }/*  w  w  w  .  j  ava2s  .  c  o m*/

    checkClientIdFormat(clientId);

    String q = "SELECT c.contactentity_contact_id FROM CommitedOperation a "
            + "INNER JOIN ContactEntity c ON a.commitedoperation_entity_id=contactentity_entity_id "
            + "WHERE commitedoperation_hash_client_id = ? AND commitedoperation_kind = ?";

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        con = obmHelper.getConnection();
        ps = con.prepareStatement(q);

        int idx = 1;
        ps.setString(idx++, clientId);
        ps.setObject(idx++, getJdbcObject(Kind.VCONTACT));
        rs = ps.executeQuery();

        Contact ret = null;
        if (rs.next()) {
            int id = rs.getInt(1);
            try {
                ret = contactDao.findContact(token, id);
            } catch (ContactNotFoundException e) {
            }
        }
        return ret;

    } finally {
        obmHelper.cleanup(con, ps, rs);
    }
}

From source file:fr.aliacom.obm.common.addition.CommitedOperationDaoJdbcImpl.java

@Override
public Event findAsEvent(AccessToken token, String clientId) throws SQLException, ServerFault {

    if (clientId == null) {
        return null;
    }/*from   w  ww. ja  v  a2 s  .  c o  m*/

    checkClientIdFormat(clientId);

    String q = "SELECT e.evententity_event_id FROM CommitedOperation a "
            + "INNER JOIN EventEntity e ON a.commitedoperation_entity_id=evententity_entity_id "
            + "WHERE commitedoperation_hash_client_id = ? AND commitedoperation_kind = ?";

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        con = obmHelper.getConnection();
        ps = con.prepareStatement(q);

        int idx = 1;
        ps.setString(idx++, clientId);
        ps.setObject(idx++, getJdbcObject(Kind.VEVENT));
        rs = ps.executeQuery();

        Event ret = null;
        if (rs.next()) {
            int id = rs.getInt(1);
            EventObmId eventObmId = new EventObmId(id);
            try {
                ret = calendarDao.findEventById(token, eventObmId);
            } catch (EventNotFoundException e) {
            }
        }
        return ret;

    } finally {
        obmHelper.cleanup(con, ps, rs);
    }
}

From source file:com.ineunet.knife.persist.dao.support.JdbcDaoSupport.java

@Override
public Long saveIncre(final T model) {
    log.info(model.getInsertSQL());//from  ww w.  j ava2  s  . c  om
    KeyHolder keyHolder = new GeneratedKeyHolder();
    getJdbcTemplate().update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
            PreparedStatement ps = conn.prepareStatement(model.getInsertSQL(), new String[] { "id" });
            for (int i = 0; i < model.getInsertParams().length; i++)
                ps.setObject(i + 1, model.getInsertParams()[i]);
            return ps;
        }
    }, keyHolder);
    return keyHolder.getKey().longValue();
}

From source file:com.github.shynixn.blockball.bukkit.logic.persistence.context.SqlDbContextImpl.java

/**
 * Sets the parameters of the preparedStatement
 *
 * @param preparedStatement preparedStatement
 * @param parameters        parameters/*from   w  w w .j  av a2  s  .  c o m*/
 * @throws SQLException exception
 */
private void setParameters(PreparedStatement preparedStatement, Object[] parameters) throws SQLException {
    for (int i = 0; i < parameters.length; i++) {
        preparedStatement.setObject(i + 1, parameters[i]);
    }
}

From source file:com.github.pires.example.hibernate.user.types.JSONBUserType.java

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    try {/*w  w w  . j a  v a 2 s  .  co  m*/
        final String json = value == null ? null : MAPPER.writeValueAsString(value);
        // otherwise PostgreSQL won't recognize the type
        PGobject pgo = new PGobject();
        pgo.setType(JSONB_TYPE);
        pgo.setValue(json);
        st.setObject(index, pgo);
    } catch (JsonProcessingException ex) {
        throw new HibernateException(ex);
    }
}

From source file:org.apache.hadoop.hive.contrib.genericudf.example.GenericUDFDBOutput.java

/**
 * @return 0 on success -1 on failure//from   w  w w  . j a v a2s .  c o m
 */
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {

    url = ((StringObjectInspector) argumentOI[0]).getPrimitiveJavaObject(arguments[0].get());
    user = ((StringObjectInspector) argumentOI[1]).getPrimitiveJavaObject(arguments[1].get());
    pass = ((StringObjectInspector) argumentOI[2]).getPrimitiveJavaObject(arguments[2].get());

    try {
        connection = DriverManager.getConnection(url, user, pass);
    } catch (SQLException ex) {
        LOG.error("Driver loading or connection issue", ex);
        result.set(2);
    }

    if (connection != null) {
        try {

            PreparedStatement ps = connection.prepareStatement(
                    ((StringObjectInspector) argumentOI[3]).getPrimitiveJavaObject(arguments[3].get()));
            for (int i = 4; i < arguments.length; ++i) {
                PrimitiveObjectInspector poi = ((PrimitiveObjectInspector) argumentOI[i]);
                ps.setObject(i - 3, poi.getPrimitiveJavaObject(arguments[i].get()));
            }
            ps.execute();
            ps.close();
            result.set(0);
        } catch (SQLException e) {
            LOG.error("Underlying SQL exception", e);
            result.set(1);
        } finally {
            try {
                connection.close();
            } catch (Exception ex) {
                LOG.error("Underlying SQL exception during close", ex);
            }
        }
    }

    return result;
}

From source file:com.espertech.esperio.db.core.DMLStatement.java

public void execute(Connection connection, EventBean eventBean) {
    PreparedStatement statement = null;
    try {//from w w  w.  j  a  v  a 2  s  . co  m
        if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
            log.debug("Executing '" + dmlSQL + ")");
        }
        statement = connection.prepareStatement(dmlSQL);
        for (Map.Entry<Integer, BindingEntry> entry : bindings.entrySet()) {
            Object value = entry.getValue().getGetter().get(eventBean);
            statement.setObject(entry.getKey(), value);
        }

        int rows = statement.executeUpdate();
        if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) {
            log.debug("Execution yielded " + rows + " rows");
        }
    } catch (SQLException ex) {
        String message = "Failed to invoke : " + dmlSQL + " :" + ex.getMessage();
        log.error(message, ex);
        storeExceptionHandler.handle(message, ex);
        throw new StoreExceptionDBRel(message, ex);
    } finally {
        try {
            if (statement != null)
                statement.close();
        } catch (SQLException e) {
        }
    }
}