Example usage for java.sql PreparedStatement setInt

List of usage examples for java.sql PreparedStatement setInt

Introduction

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

Prototype

void setInt(int parameterIndex, int x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java int value.

Usage

From source file:com.fpmislata.banco.persistencia.impl.MovimientoBancarioDAOImplJDBC.java

@Override
public MovimientoBancario insert(MovimientoBancario movimientoBancario) {
    Connection connection = connectionFactory.getConnection();
    String SQL = "INSERT INTO movimientobancario (id, cuentapertenece,tipoMovimiento, importe, saldoTotal, concepto) VALUES (?, ?, ?, ?, ?, ?)";
    try {/*from  w  w  w  . j av a2s  .  c  om*/
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
        preparedStatement.setInt(1, movimientoBancario.getId());
        preparedStatement.setInt(2, movimientoBancario.getCuentaPertenece());
        preparedStatement.setString(3, movimientoBancario.getTipoMovimiento());
        preparedStatement.setString(4, movimientoBancario.getImporte());
        preparedStatement.setString(5, movimientoBancario.getSaldoTotal());
        preparedStatement.setString(6, movimientoBancario.getConcepto());
        preparedStatement.executeUpdate();

        movimientoBancario.setFecha(new Date());
        return get(movimientoBancario.getId());
    } catch (Exception ex) {
        throw new RuntimeException("Error al insertar el movimientos", ex);
    } finally {
        try {
            connection.close();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }

    }
}

From source file:de.whs.poodle.repositories.CourseRepository.java

public void create(Course course, String firstTermName) {
    try {/*from  w ww .  j  a v  a2 s .  c om*/
        int id = jdbc.query(con -> {
            // the function creates the course and the first term (firstTermId)
            PreparedStatement ps = con.prepareStatement("SELECT * FROM create_course(?,?,?,?,?,?,?)");
            ps.setInt(1, course.getInstructor().getId());
            ps.setString(2, course.getName());
            ps.setBoolean(3, course.getVisible());
            if (course.getPassword().trim().isEmpty())
                ps.setNull(4, Types.VARCHAR);
            else
                ps.setString(4, course.getPassword());

            Array otherInstructors = con.createArrayOf("int4", course.getOtherInstructorsIds().toArray());
            ps.setArray(5, otherInstructors);
            Array linkedCourses = con.createArrayOf("int4", course.getLinkedCoursesIds().toArray());
            ps.setArray(6, linkedCourses);
            ps.setString(7, firstTermName);
            return ps;
        }, new ResultSetExtractor<Integer>() {

            @Override
            public Integer extractData(ResultSet rs) throws SQLException, DataAccessException {
                rs.next();
                return rs.getInt(1);
            }
        });

        course.setId(id);
    } catch (DuplicateKeyException e) {
        throw new BadRequestException();
    }
}

From source file:cn.vlabs.duckling.vwb.service.init.provider.BannerInitProvider.java

private void createBanner(final int siteId, Banner banner) {
    final Banner tempBanner = banner;
    if (banner == null)
        return;//  www.  j av  a  2s .com
    banner.setBannerProfile();
    getJdbcTemplate().update(new PreparedStatementCreator() {
        public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
            int i = 0;
            PreparedStatement ps = conn.prepareStatement(createBannerSQL);
            ps.setInt(++i, siteId);
            ps.setString(++i, tempBanner.getDirName());
            ps.setString(++i, tempBanner.getName());
            ps.setInt(++i, tempBanner.getStatus());
            ps.setInt(++i, tempBanner.getType());
            ps.setString(++i, tempBanner.getCreator());
            ps.setTimestamp(++i, new Timestamp(tempBanner.getCreatedTime().getTime()));
            ps.setInt(++i, tempBanner.getLeftPictureClbId());
            ps.setInt(++i, tempBanner.getRightPictureClbId());
            ps.setInt(++i, tempBanner.getMiddlePictureClbId());
            ps.setInt(++i, tempBanner.getCssClbId());
            ps.setInt(++i, tempBanner.getBannerTitle());
            ps.setString(++i, tempBanner.getOwnedtype());
            ps.setString(++i, tempBanner.getBannerProfile());
            return ps;
        }
    });
}

From source file:com.thesoftwareguild.flightmaster.daos.RequestDaoJdbcImpl.java

@Override
@Transactional(propagation = Propagation.REQUIRED)
public List<Flight> addFlights(int requestId, List<Flight> flights) {
    if (flights != null && flights.size() > 0) {
        Date timeOfQuery = flights.get(0).getQueryTime();
        jdbcTemplate.update(SQL_ADD_REQUESTDATA_POINT, requestId, timeOfQuery);
        int requestDataId = jdbcTemplate.queryForObject(SQL_GET_LAST_ID, Integer.class);
        jdbcTemplate.batchUpdate(SQL_ADD_FLIGHT_DATA, new BatchPreparedStatementSetter() {

            @Override/*from w ww . ja v a2 s.  com*/
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setInt(1, requestDataId);
                ps.setDouble(2, flights.get(i).getPrice());
                ps.setString(3, flights.get(i).getCarrier());
            }

            @Override
            public int getBatchSize() {
                return flights.size();
            }
        });
        // Decrement queries_left to keep it consistant with in-memory representation
        jdbcTemplate.update(SQL_DECREMENT_QUERIESLEFT, requestId);
        // Get interval and set next execution time in database
        long interval = jdbcTemplate.queryForObject(SQL_GET_INTERVAL, Long.class, requestId);
        jdbcTemplate.update(SQL_UPDATE_NEXT_EXECUTION, (System.currentTimeMillis() + interval), requestId);
    }
    return flights;
}

From source file:com.fpmislata.banco.persistencia.impl.MovimientoBancarioDAOImplJDBC.java

@Override
public MovimientoBancario get(int id) {
    MovimientoBancario movimientoBancario = new MovimientoBancario();
    Connection connection = connectionFactory.getConnection();
    String SQL = "SELECT * FROM movimientobancario WHERE id = ? ";
    try {/*from w  w w  .  j av a 2  s .c  om*/
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
        preparedStatement.setInt(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        resultSet.next();
        movimientoBancario.setId(resultSet.getInt("id"));
        movimientoBancario.setCuentaPertenece(resultSet.getInt("cuentapertenece"));
        movimientoBancario.setImporte(resultSet.getString("importe"));
        movimientoBancario.setFecha(resultSet.getDate("fecha"));
        movimientoBancario.setSaldoTotal(resultSet.getString("saldototal"));
        movimientoBancario.setTipoMovimiento(resultSet.getString("tipomovimiento"));
        movimientoBancario.setConcepto(resultSet.getString("concepto"));

        return movimientoBancario;
    } catch (Exception ex) {
        throw new RuntimeException("Error al hacer la consulta", ex);
    } finally {
        try {
            connection.close();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }

}

From source file:com.adanac.module.blog.dao.RecordDao.java

public Boolean delete(Integer id) {
    return execute(new TransactionalOperation<Boolean>() {
        @Override//from  w w w  .j  a  va  2 s. co  m
        public Boolean doInConnection(Connection connection) {
            String sql = "delete from records where id=?";
            try {
                PreparedStatement statement = connection.prepareStatement(sql);
                statement.setInt(1, id);
                int result = statement.executeUpdate();
                if (result > 0) {
                    return true;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            return false;
        }
    });
}

From source file:com.fpmislata.banco.persistencia.impl.SucursalBancariaDAOImplJDBC.java

@Override
public SucursalBancaria insert(SucursalBancaria sucursalBancaria) {
    Connection connection = connectionFactory.getConnection();

    String SQL = "INSERT INTO sucursalbancaria VALUES(?,?,?,?)";

    try {//from   w w  w .  jav  a  2  s .co  m
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
        preparedStatement.setInt(1, sucursalBancaria.getId());
        preparedStatement.setInt(2, sucursalBancaria.getEntidadPertenece());
        preparedStatement.setString(3, sucursalBancaria.getCodigoSucursal());
        preparedStatement.setString(4, sucursalBancaria.getNombreSucursal());

        preparedStatement.execute();

        return get(sucursalBancaria.getId());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            connection.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:com.fpmislata.banco.persistencia.impl.SucursalBancariaDAOImplJDBC.java

@Override
public SucursalBancaria update(SucursalBancaria sucursalBancaria) {
    Connection connection = connectionFactory.getConnection();

    String SQL = "UPDATE sucursalbancaria SET entidadPertenece=?, codigoSucursal=?, nombreSucursal=? WHERE id=?";

    try {//from   w w  w .  j  a  va2 s  .  c o m
        PreparedStatement preparedStatement = connection.prepareStatement(SQL);
        preparedStatement.setInt(1, sucursalBancaria.getEntidadPertenece());
        preparedStatement.setString(2, sucursalBancaria.getCodigoSucursal());
        preparedStatement.setString(3, sucursalBancaria.getNombreSucursal());
        preparedStatement.setInt(4, sucursalBancaria.getId());

        preparedStatement.execute();

        return get(sucursalBancaria.getId());
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            connection.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:dk.netarkivet.common.utils.DBUtils.java

/**
 * Prepare a statement given a query string and some args.
 *
 * NB: the provided connection is not closed.
 *
 * @param c a Database connection// w ww.j av a2  s .c  om
 * @param query a query string  (must not be null or empty)
 * @param args some args to insert into this query string (must not be null)
 * @return a prepared statement
 * @throws SQLException If unable to prepare a statement
 * @throws ArgumentNotValid If unable to handle type of one the args, or
 * the arguments are either null or an empty String.
 */
public static PreparedStatement prepareStatement(Connection c, String query, Object... args)
        throws SQLException {
    ArgumentNotValid.checkNotNull(c, "Connection c");
    ArgumentNotValid.checkNotNullOrEmpty(query, "String query");
    ArgumentNotValid.checkNotNull(args, "Object... args");
    PreparedStatement s = c.prepareStatement(query);
    int i = 1;
    for (Object arg : args) {
        if (arg instanceof String) {
            s.setString(i, (String) arg);
        } else if (arg instanceof Integer) {
            s.setInt(i, (Integer) arg);
        } else if (arg instanceof Long) {
            s.setLong(i, (Long) arg);
        } else if (arg instanceof Boolean) {
            s.setBoolean(i, (Boolean) arg);
        } else if (arg instanceof Date) {
            s.setTimestamp(i, new Timestamp(((Date) arg).getTime()));
        } else {
            throw new ArgumentNotValid("Cannot handle type '" + arg.getClass().getName()
                    + "'. We can only handle string, " + "int, long, date or boolean args for query: " + query);
        }
        i++;
    }
    return s;
}

From source file:oobbit.orm.Comments.java

/**
 * Returns one comment without User object attached to it.
 *
 * @param commentId//from  ww w  .  j av  a 2s  .c  om
 *
 * @return
 *
 * @throws SQLException             PreparedStatement failed to execute
 * @throws NothingWasFoundException
 */
public Comment getOne(int commentId) throws SQLException, NothingWasFoundException {
    PreparedStatement statement = getConnection()
            .prepareStatement("SELECT * FROM `comments` WHERE `comment_id` = ?;");
    statement.setInt(1, commentId);

    ResultSet query = statement.executeQuery();

    if (query.next()) {
        Comment comment = new Comment();
        comment.parse(query);

        return comment;
    } else {
        throw new NothingWasFoundException();
    }
}