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:org.efaps.admin.datamodel.Attribute.java

/**
 * @param _attrId   id of an attribute/*from w ww . j  a v  a 2  s  .  c  om*/
 * @return the id of a Type
 * @throws CacheReloadException on error
 */
protected static long getTypeID(final long _attrId) throws CacheReloadException {
    long ret = 0;
    ConnectionResource con = null;
    try {
        con = Context.getThreadContext().getConnectionResource();
        PreparedStatement stmt = null;
        try {
            stmt = con.getConnection().prepareStatement(Attribute.SQL_ATTR);
            stmt.setObject(1, _attrId);
            final ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                ret = rs.getLong(1);
            }
            rs.close();
        } finally {
            if (stmt != null) {
                stmt.close();
            }
        }
        con.commit();
    } catch (final SQLException e) {
        throw new CacheReloadException("Cannot read a type for an attribute.", e);
    } catch (final EFapsException e) {
        throw new CacheReloadException("Cannot read a type for an attribute.", e);
    } finally {
        if (con != null && con.isOpened()) {
            try {
                con.abort();
            } catch (final EFapsException e) {
                throw new CacheReloadException("Cannot read a type for an attribute.", e);
            }
        }
    }
    return ret;
}

From source file:com.ea.core.orm.handle.impl.HibernateSqlORMHandle.java

private void setParam(PreparedStatement ps, int index, Object param) throws SQLException {
    ps.setObject(index, param);
}

From source file:org.mayocat.store.rdbms.dbi.argument.MapAsJsonArgumentFactory.java

@Override
public Argument build(Class<?> expectedType, final Map value, StatementContext ctx) {
    try {//from w  ww .j a  va 2  s . c om
        final PGobject jsonObject = new PGobject();
        final ObjectMapper mapper = new ObjectMapper();

        jsonObject.setType("json");
        jsonObject.setValue(mapper.writeValueAsString(value));

        return new Argument() {
            @Override
            public void apply(int position, PreparedStatement statement, StatementContext ctx)
                    throws SQLException {
                statement.setObject(position, jsonObject);
            }
        };
    } catch (JsonProcessingException | SQLException e) {
        throw new RuntimeException("Failed to convert map argument to JSON", e);
    }
}

From source file:org.mayocat.store.rdbms.dbi.argument.JsonArgumentAsJsonArgumentFactory.java

@Override
public Argument build(Class<?> expectedType, final JsonArgument value, StatementContext ctx) {
    try {/*from w ww.ja  v a2 s. c  o  m*/
        final PGobject jsonObject = new PGobject();
        final ObjectMapper mapper = new ObjectMapper();

        jsonObject.setType("json");
        jsonObject.setValue(mapper.writeValueAsString(value.getWrapped()));

        return new Argument() {
            @Override
            public void apply(int position, PreparedStatement statement, StatementContext ctx)
                    throws SQLException {
                statement.setObject(position, jsonObject);
            }
        };
    } catch (JsonProcessingException e) {
        throw new IllegalStateException("Failed to map JSON argument", e);
    } catch (SQLException e) {
        throw new IllegalStateException("Failed to map JSON argument", e);
    }
}

From source file:de.langmi.spring.batch.examples.complex.jdbc.generic.support.MapPreparedStatementSetter.java

@Override
public void setValues(Map<String, Object> item, PreparedStatement ps) throws SQLException {
    for (int i = 0; i < item.size(); i++) {
        // PreparedStatements start with 1
        ps.setObject(i + 1, item.get(String.valueOf(i)));
    }/*from  w w  w .ja v a2  s.c o  m*/
}

From source file:de.langmi.spring.batch.examples.writers.jdbc.generic.FieldSetItemPreparedStatementSetter.java

/** {@inheritDoc} */
@Override//from  w  w w.j a v a  2s  .  c om
public void setValues(FieldSet item, PreparedStatement ps) throws SQLException {
    for (int i = 0; i < item.getValues().length; i++) {
        // PreparedStatements start with 1
        ps.setObject(i + 1, item.getValues()[i]);
    }
}

From source file:com.manydesigns.portofino.persistence.QueryUtils.java

/**
 * Runs a SQL query against a session. The query can contain placeholders for the parameters, as supported by
 * {@link PreparedStatement}.//from   ww  w  . j  av  a2s.c om
 * 
 * @param session the session
 * @param queryString the query
 * @param parameters parameters to substitute in the query
 * @return the results of the query as an Object[] (an array cell per column)
 */
public static List<Object[]> runSql(Session session, final String queryString, final Object[] parameters) {
    final List<Object[]> result = new ArrayList<Object[]>();

    try {
        session.doWork(new Work() {
            public void execute(Connection connection) throws SQLException {
                PreparedStatement stmt = connection.prepareStatement(queryString);
                ResultSet rs = null;
                try {
                    for (int i = 0; i < parameters.length; i++) {
                        stmt.setObject(i + 1, parameters[i]);
                    }
                    rs = stmt.executeQuery();
                    ResultSetMetaData md = rs.getMetaData();
                    int cc = md.getColumnCount();
                    while (rs.next()) {
                        Object[] current = new Object[cc];
                        for (int i = 0; i < cc; i++) {
                            current[i] = rs.getObject(i + 1);
                        }
                        result.add(current);
                    }
                } finally {
                    if (null != rs) {
                        rs.close();
                    }
                    if (null != stmt) {
                        stmt.close();
                    }
                }
            }
        });
    } catch (HibernateException e) {
        session.getTransaction().rollback();
        session.beginTransaction();
        throw e;
    }

    return result;
}

From source file:com.manydesigns.portofino.persistence.QueryUtils.java

/**
 * Runs a SQL query against a session. The query can contain placeholders for the parameters, as supported by
 * {@link PreparedStatement}.//from www .  j a v a2s.  c  o m
 * 
 * @param session the session
 * @param queryString the query
 * @param parameters parameters to substitute in the query
 * @return the results of the query as an Object[] (an array cell per column)
 */
// hongliangpan add
public static List<Map<String, Object>> runSqlReturnMap(Session session, final String queryString,
        final Object[] parameters) {
    final List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    try {
        session.doWork(new Work() {
            public void execute(Connection connection) throws SQLException {
                PreparedStatement stmt = connection.prepareStatement(queryString);
                ResultSet rs = null;
                try {
                    for (int i = 0; i < parameters.length; i++) {
                        stmt.setObject(i + 1, parameters[i]);
                    }
                    rs = stmt.executeQuery();
                    ResultSetMetaData md = rs.getMetaData();
                    int cc = md.getColumnCount();

                    while (rs.next()) {
                        Map<String, Object> t_row = new LinkedHashMap<String, Object>();
                        for (int i = 0; i < cc; i++) {
                            Object t_value = rs.getObject(i + 1);
                            t_row.put(md.getColumnLabel(i + 1), t_value);
                        }
                        result.add(t_row);
                    }
                } finally {
                    if (null != rs) {
                        rs.close();
                    }
                    if (null != stmt) {
                        stmt.close();
                    }
                }
            }
        });
    } catch (HibernateException e) {
        session.getTransaction().rollback();
        session.beginTransaction();
        throw e;
    }

    return result;
}

From source file:com.krawler.esp.servlets.FileImporterServlet.java

public static void fildoc(String docid, String docname, String projid, String svnName, String userid, long size)
        throws ServiceException {

    PreparedStatement pstmt = null;
    String sql = null;/* w  w  w  .j av a 2 s  .c  o m*/
    Connection conn = null;
    try {
        conn = DbPool.getConnection();
        java.util.Date d = new java.util.Date();
        java.sql.Timestamp docdatemod = new Timestamp(d.getTime());
        String currentStoreIndex = StorageHandler.GetCurrentStorageIndex();
        sql = "INSERT INTO docs(docid, docname, docdatemod, "
                + "userid,svnname,storageindex,docsize) VALUES (?,?,?,?,?,?,?)";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, docid);
        pstmt.setString(2, docname);
        pstmt.setObject(3, docdatemod);
        pstmt.setObject(4, userid);
        pstmt.setString(5, svnName);
        pstmt.setString(6, currentStoreIndex);
        pstmt.setObject(7, size);
        pstmt.executeUpdate();
        sql = "insert into docprerel (docid,userid,permission) values(?,?,?)";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, docid);
        pstmt.setString(2, projid);
        pstmt.setObject(3, "8");
        pstmt.executeUpdate();

        conn.commit();
    } catch (ServiceException ex) {
        DbPool.quietRollback(conn);
        throw ServiceException.FAILURE("FileImporterServlet.fildoc", ex);
    } catch (Exception ex) {
        DbPool.quietRollback(conn);
        throw ServiceException.FAILURE("FileImporterServlet.fildoc", ex);
    } finally {
        DbPool.quietClose(conn);
    }
}

From source file:at.alladin.rmbt.db.fields.UUIDField.java

@Override
public void getField(final PreparedStatement ps, final int idx) throws SQLException {
    if (value == null)
        ps.setNull(idx, Types.OTHER);
    else/*from www. java 2  s . co  m*/
        ps.setObject(idx, value);
}