Example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate execute

List of usage examples for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate execute

Introduction

In this page you can find the example usage for org.springframework.jdbc.core.namedparam NamedParameterJdbcTemplate execute.

Prototype

@Override
    @Nullable
    public <T> T execute(String sql, Map<String, ?> paramMap, PreparedStatementCallback<T> action)
            throws DataAccessException 

Source Link

Usage

From source file:com.yahoo.sql4d.sql4ddriver.sql.MysqlAccessor.java

/**
 * Suitable for CRUD operations where no result set is expected.
 * @param params//  w  w w.  ja v a2 s .  co m
 * @param query 
 * @return  
 */
public boolean execute(Map<String, String> params, String query) {
    final AtomicBoolean result = new AtomicBoolean(false);
    Tuple2<DataSource, Connection> conn = null;
    try {
        conn = getConnection();
        NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(conn._1());
        jdbcTemplate.execute(query, params, new PreparedStatementCallback<Void>() {
            @Override
            public Void doInPreparedStatement(PreparedStatement ps) {
                try {
                    result.set(ps.execute());
                } catch (SQLException e) {
                    result.set(false);
                }
                return null;
            }
        });
    } catch (Exception ex) {
        Logger.getLogger(MysqlAccessor.class.getName()).log(Level.SEVERE, null, ex);
        result.set(false);
    } finally {
        returnConnection(conn);
    }
    return result.get();
}

From source file:com.yahoo.sql4d.indexeragent.sql.DBAccessor.java

/**
 * Suitable for CRUD operations where no result set is expected.
 * @param params/*  w  ww .ja  v  a  2s  .  c  o m*/
 * @param query 
 * @return  
 */
public boolean execute(Map<String, String> params, String query) {
    final AtomicBoolean result = new AtomicBoolean(false);
    Tuple2<DataSource, Connection> conn = null;
    try {
        conn = getConnection();
        NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(conn._1());
        jdbcTemplate.execute(query, params, new PreparedStatementCallback<Void>() {
            @Override
            public Void doInPreparedStatement(PreparedStatement ps) {
                try {
                    result.set(ps.execute());
                } catch (SQLException e) {
                    result.set(false);
                }
                return null;
            }
        });
    } catch (Exception ex) {
        Logger.getLogger(DBAccessor.class.getName()).log(Level.SEVERE, null, ex);
        result.set(false);
    } finally {
        returnConnection(conn);
    }
    return result.get();
}

From source file:org.paxml.tag.sql.SqlTag.java

protected Object executeSql(String sql, Context context) {

    return executeSql(sql, new ISqlExecutor() {
        @Override//from w w w.ja v  a 2s. c  o m
        public Object update(final String sql) {
            if (param != null) {
                NamedParameterJdbcTemplate t = new NamedParameterJdbcTemplate(jdbcTemplate);
                return t.execute(sql, param, new PreparedStatementCallback<Void>() {

                    @Override
                    public Void doInPreparedStatement(PreparedStatement ps)
                            throws SQLException, DataAccessException {
                        ps.executeUpdate();
                        return null;
                    }

                });
            } else {
                jdbcTemplate.execute(sql);
            }
            return null;
        }

        @Override
        public Object query(String sql, boolean close) {
            if (param != null) {
                NamedParameterJdbcTemplate t = new NamedParameterJdbcTemplate(jdbcTemplate);
                return t.queryForList(sql, param);
            } else {
                return jdbcTemplate.queryForList(sql);
            }
        }

    });

}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testExecute() throws SQLException {
    mockPreparedStatement.setObject(1, new Integer(1));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(1));
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeUpdate();
    ctrlPreparedStatement.setReturnValue(1);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }//from  w w  w .j a va 2  s . c om
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("perfId", new Integer(1));
    params.put("priceId", new Integer(1));
    assertEquals("result", jt.execute(UPDATE_NAMED_PARAMETERS, params, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            assertEquals(mockPreparedStatement, ps);
            ps.executeUpdate();
            return "result";
        }
    }));
}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testExecuteWithTypedParameters() throws SQLException {
    mockPreparedStatement.setObject(1, new Integer(1), Types.DECIMAL);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setObject(2, new Integer(1), Types.INTEGER);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeUpdate();
    ctrlPreparedStatement.setReturnValue(1);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }/*from   w  w  w .ja  v  a2 s. co m*/
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(UPDATE_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("perfId", new SqlParameterValue(Types.DECIMAL, new Integer(1)));
    params.put("priceId", new SqlParameterValue(Types.INTEGER, new Integer(1)));
    assertEquals("result", jt.execute(UPDATE_NAMED_PARAMETERS, params, new PreparedStatementCallback() {
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException {
            assertEquals(mockPreparedStatement, ps);
            ps.executeUpdate();
            return "result";
        }
    }));
}