Example usage for java.sql PreparedStatement setDate

List of usage examples for java.sql PreparedStatement setDate

Introduction

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

Prototype

void setDate(int parameterIndex, java.sql.Date x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.sql.Date value using the default time zone of the virtual machine that is running the application.

Usage

From source file:org.wso2.carbon.identity.certificateauthority.dao.RevocationDAO.java

/**
 * add revoked certificate to the database
 *
 * @param serialNo serialNo of the revoked certificate
 * @param tenantID/*w  w w . j av a2  s  .  c om*/
 * @param reason   reason for the revoke
 * @throws CaException
 */

public void addRevokedCertificate(String serialNo, int tenantID, int reason) throws CaException {
    Connection connection = null;
    Date requestDate = new Date();
    String sql = null;
    PreparedStatement prepStmt = null;
    try {
        log.debug("adding revoked certificate to database");
        connection = JDBCPersistenceManager.getInstance().getDBConnection();
        sql = "INSERT INTO CA_REVOKED_CERTIFICATES (SERIAL_NO, REVOKED_DATE, TENANT_ID, REASON) VALUES (?,?,?,?) ";
        prepStmt = connection.prepareStatement(sql);
        prepStmt.setString(1, serialNo);
        prepStmt.setDate(2, new java.sql.Date(new Date().getTime()));
        prepStmt.setInt(3, tenantID);
        prepStmt.setInt(4, reason);
        prepStmt.execute();
        connection.commit();
    } catch (IdentityException e) {
        String errorMsg = "Error when getting an Identity Persistence Store instance.";
        log.error(errorMsg, e);
        throw new CaException(errorMsg, e);
    } catch (SQLException e) {
        log.error("Error when executing the SQL : " + sql);
        log.error(e.getMessage(), e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
    }
}

From source file:net.freechoice.model.orm.Map_Profile.java

@Override
public PreparedStatementCreator createUpdate(final FC_Profile entity) {

    return new PreparedStatementCreator() {

        @Override/*ww w  . j  a v  a 2  s  .  co  m*/
        public PreparedStatement createPreparedStatement(Connection con) throws SQLException {

            PreparedStatement ps = con.prepareStatement(
                    "update  FC_Transaction" + " set  id_user_ = ? " + ",      site_personal = ? "
                            + ",     name_first = ? " + ",     name_last = ? " + ",     contact_public = ? "
                            + ",     gender = ? " + ",     date_birth = ? " + " where id = ?");
            ps.setInt(1, entity.id_user_);
            ps.setString(2, entity.site_personal);
            ps.setString(3, entity.name_first);
            ps.setString(4, entity.name_last);
            ps.setString(5, entity.contact_public);
            ps.setBoolean(6, entity.gender);
            ps.setDate(7, entity.date_birth);
            ps.setInt(8, entity.id);
            return ps;
        }
    };
}

From source file:org.kuali.kfs.gl.batch.dataaccess.impl.LedgerPreparedStatementCachingDaoJdbc.java

public void updateEncumbrance(final Encumbrance encumbrance, final Timestamp currentTimestamp) {
    new UpdatingJdbcWrapper<Encumbrance>() {
        @Override//  w w  w. j  a v a  2  s .  c o m
        protected void populateStatement(PreparedStatement preparedStatement) throws SQLException {
            preparedStatement.setString(1, encumbrance.getTransactionEncumbranceDescription());
            preparedStatement.setDate(2, encumbrance.getTransactionEncumbranceDate());
            preparedStatement.setBigDecimal(3, encumbrance.getAccountLineEncumbranceAmount().bigDecimalValue());
            preparedStatement.setBigDecimal(4,
                    encumbrance.getAccountLineEncumbranceClosedAmount().bigDecimalValue());
            preparedStatement.setString(5, encumbrance.getAccountLineEncumbrancePurgeCode());
            preparedStatement.setTimestamp(6, currentTimestamp);
            preparedStatement.setInt(7, encumbrance.getUniversityFiscalYear());
            preparedStatement.setString(8, encumbrance.getChartOfAccountsCode());
            preparedStatement.setString(9, encumbrance.getAccountNumber());
            preparedStatement.setString(10, encumbrance.getSubAccountNumber());
            preparedStatement.setString(11, encumbrance.getObjectCode());
            preparedStatement.setString(12, encumbrance.getSubObjectCode());
            preparedStatement.setString(13, encumbrance.getBalanceTypeCode());
            preparedStatement.setString(14, encumbrance.getDocumentTypeCode());
            preparedStatement.setString(15, encumbrance.getOriginCode());
            preparedStatement.setString(16, encumbrance.getDocumentNumber());
        }
    }.execute(Encumbrance.class);
}

From source file:net.freechoice.dao.impl.DaoPost.java

@DBSpec(dialect = Dialect.PostgreSQL)
@Override/*from   ww  w  .  j a  v a 2 s.c om*/
public List<FC_Post> getPostsOnDate(final Date date, final int length, final int offset, final int limit) {

    return getJdbcTemplate().query(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection arg0) throws SQLException {

            PreparedStatement ps = arg0.prepareStatement(selectFromPost(length,
                    " where is_valud = true and status > 1 " + "and time_posted::date = ?", TIME_DESCEND,
                    offset, limit));
            ps.setDate(1, date);
            return ps;
        }
    }, mapper);
}

From source file:net.freechoice.dao.impl.DaoPost.java

@Override
public List<FC_Post> getPostsAfter(final Date time, final int length, final int offset, final int limit) {

    return getJdbcTemplate().query(new PreparedStatementCreator() {
        @Override// w  ww .j  a v a2 s .com
        public PreparedStatement createPreparedStatement(Connection arg0) throws SQLException {

            PreparedStatement ps = arg0.prepareStatement(
                    selectFromPost(length, " where is_valud = true " + "and status > 1 and time_posted > ?",
                            TIME_DESCEND, offset, limit));
            ps.setDate(1, time);
            return ps;
        }
    }, mapper);
}

From source file:net.freechoice.dao.impl.DaoPost.java

@DBSpec(dialect = Dialect.PostgreSQL)
//   @Deprecated/*from   ww  w. j a  v a  2s  . co m*/
@Override
public List<FC_Post> getPostsBetween(final Date start, final Date end, final int length, final int offset,
        final int limit) {

    return getJdbcTemplate().query(new PreparedStatementCreator() {
        @Override
        public PreparedStatement createPreparedStatement(Connection arg0) throws SQLException {

            PreparedStatement ps = arg0.prepareStatement(selectFromPost(length,
                    " where is_valud = true and status > 1 and (time_posted::date between ? and ?)",
                    TIME_DESCEND, offset, limit));
            ps.setDate(1, start);
            ps.setDate(2, end);
            return ps;
        }
    }, mapper);
}

From source file:biblivre3.acquisition.request.RequestDAO.java

public int getSearchCount(RequestDTO example) {
    Connection con = null;/*  ww w. j av a 2 s. c o m*/
    try {
        con = getDataSource().getConnection();
        final StringBuilder sql = new StringBuilder(" SELECT COUNT(*) FROM acquisition_requisition ");
        if (example.getSerial() != null && example.getSerial() != 0) {
            sql.append(" WHERE serial_requisition = ? ");
        } else if (StringUtils.isNotBlank(example.getRequester())) {
            sql.append(" WHERE requester ilike ? ");
        } else if (example.getRequestDate() != null) {
            sql.append(" WHERE requisition_date = ? ");
        } else if (StringUtils.isNotBlank(example.getAuthor())) {
            sql.append(" WHERE author ilike ? ");
        } else if (StringUtils.isNotBlank(example.getTitle())) {
            sql.append(" WHERE item_title ilike ? ");
        }
        if (StringUtils.isNotBlank(example.getStatus())) {
            if (example.getStatus().equals("0") || example.getStatus().equals("1")) {
                if (sql.toString().contains("WHERE")) {
                    sql.append(" AND status = ? ");
                } else {
                    sql.append(" WHERE status = ? ");
                }
            }
        }
        final PreparedStatement pst = con.prepareStatement(sql.toString());
        int i = 1;
        if (example.getSerial() != null && example.getSerial() != 0) {
            pst.setInt(i++, example.getSerial());
        } else if (StringUtils.isNotBlank(example.getRequester())) {
            pst.setString(i++, "%" + example.getRequester() + "%");
        } else if (example.getRequestDate() != null) {
            pst.setDate(i++, new java.sql.Date(example.getRequestDate().getTime()));
        } else if (StringUtils.isNotBlank(example.getAuthor())) {
            pst.setString(i++, "%" + example.getAuthor() + "%");
        } else if (StringUtils.isNotBlank(example.getTitle())) {
            pst.setString(i++, "%" + example.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(example.getStatus())) {
            if (example.getStatus().equals("0") || example.getStatus().equals("1")) {
                pst.setString(i++, example.getStatus());
            }
        }
        final ResultSet rs = pst.executeQuery();
        if (rs == null) {
            return 0;
        }
        while (rs.next()) {
            return rs.getInt(1);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new DAOException(e.getMessage());
    } finally {
        closeConnection(con);
    }
    return 0;
}

From source file:biblivre3.acquisition.request.RequestDAO.java

public ArrayList<RequestDTO> searchRequest(RequestDTO example, int offset, int limit) {
    ArrayList<RequestDTO> requestList = new ArrayList<RequestDTO>();
    Connection con = null;/*from  w w w.  ja  v  a 2s  .com*/
    try {
        con = getDataSource().getConnection();
        final StringBuilder sql = new StringBuilder(" SELECT * FROM acquisition_requisition ");
        if (example.getSerial() != null && example.getSerial() != 0) {
            sql.append(" WHERE serial_requisition = ? ");
        } else if (StringUtils.isNotBlank(example.getRequester())) {
            sql.append(" WHERE requester ilike ? ");
        } else if (example.getRequestDate() != null) {
            sql.append(" WHERE requisition_date = ? ");
        } else if (StringUtils.isNotBlank(example.getAuthor())) {
            sql.append(" WHERE author ilike ? ");
        } else if (StringUtils.isNotBlank(example.getTitle())) {
            sql.append(" WHERE item_title ilike ? ");
        }
        if (StringUtils.isNotBlank(example.getStatus())) {
            if (example.getStatus().equals("0") || example.getStatus().equals("1")) {
                if (sql.toString().contains("WHERE")) {
                    sql.append(" AND status = ? ");
                } else {
                    sql.append(" WHERE status = ? ");
                }
            }
        }

        sql.append(" ORDER BY serial_requisition ASC offset ? limit ? ");
        final PreparedStatement pst = con.prepareStatement(sql.toString());
        int i = 1;
        if (example.getSerial() != null && example.getSerial() != 0) {
            pst.setInt(i++, example.getSerial());
        } else if (StringUtils.isNotBlank(example.getRequester())) {
            pst.setString(i++, "%" + example.getRequester() + "%");
        } else if (example.getRequestDate() != null) {
            pst.setDate(i++, new java.sql.Date(example.getRequestDate().getTime()));
        } else if (StringUtils.isNotBlank(example.getAuthor())) {
            pst.setString(i++, "%" + example.getAuthor() + "%");
        } else if (StringUtils.isNotBlank(example.getTitle())) {
            pst.setString(i++, "%" + example.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(example.getStatus())) {
            if (example.getStatus().equals("0") || example.getStatus().equals("1")) {
                pst.setString(i++, example.getStatus());
            }
        }
        pst.setInt(i++, offset);
        pst.setInt(i++, limit);
        final ResultSet rs = pst.executeQuery();
        if (rs == null) {
            return requestList;
        }
        while (rs.next()) {
            RequestDTO dto = this.populateDto(rs);
            requestList.add(dto);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new DAOException(e.getMessage());
    } finally {
        closeConnection(con);
    }
    return requestList;
}

From source file:org.kawanfw.test.api.client.InsertAndUpdatePrepStatementTest.java

/**
 * Update the values of a row with an increase factor and a new datetime
 * //from www .j a  va 2 s .com
 * @param connection
 * @param customerId
 * @param itemId
 * @throws Exception
 */

private void updateValues(Connection connection, int customerId, int itemId) throws Exception {
    String sql = "update orderlog set " + "   date_placed  = ? " + " , date_shipped = ? "
            + " , cost_price   = ? " + " , is_delivered = ? " + " , quantity     = ? "
            + "     where  customer_id = ? and item_id = ?";

    PreparedStatement prepStatement = connection.prepareStatement(sql);

    long newTime = (new java.util.Date()).getTime();
    Date datePlaced = new Date(newTime);
    dateShippedUpdated = new Timestamp(newTime);

    MessageDisplayer.display("dateShippedUpdated                  : " + dateShippedUpdated);
    MessageDisplayer
            .display("dateShippedUpdated.substring.(0, 19): " + dateShippedUpdated.toString().substring(0, 19));

    int i = 1;
    prepStatement.setDate(i++, datePlaced);
    prepStatement.setTimestamp(i++, dateShippedUpdated);

    // We use the increase factor
    prepStatement.setBigDecimal(i++, new BigDecimal(customerId * increaseFactor));

    SqlUtil sqlUtil = new SqlUtil(connection);
    if (sqlUtil.isIngres() || sqlUtil.isPostgreSQL()) {
        prepStatement.setInt(i++, 1);
    } else {
        prepStatement.setBoolean(i++, true);
    }

    prepStatement.setInt(i++, customerId * increaseFactor * 2);

    // Key value
    prepStatement.setInt(i++, customerId);
    prepStatement.setInt(i++, itemId);

    prepStatement.executeUpdate();
    prepStatement.close();

}

From source file:io.github.sislivros.persistencia.UsuarioBdDao.java

@Override
public boolean alterar(Usuario usuario) {

    try {//from   w w  w .  j a  v a2s .c  o  m

        if (getConnection() == null || getConnection().isClosed()) {
            conectar();
        }
        String sql = "UPDATE usuario SET email = ?, apelido = ?, data_nascimento = ?, cidade = ?"
                + ", estado = ?, nome = ?, foto_perfil = ?, foto_capa = ?, tipo = ? WHERE id = ?";
        PreparedStatement ps = getConnection().prepareStatement(sql);
        Date dataNascimento = null;

        try {
            dataNascimento = new Date(usuario.getDataNascimento().getTime());
        } catch (NullPointerException ex) {
        }

        ps.setString(1, usuario.getEmail());
        ps.setString(2, usuario.getApelido());
        ps.setDate(3, dataNascimento);
        ps.setString(4, usuario.getCidade());
        ps.setString(5, usuario.getEstado());
        ps.setString(6, usuario.getNome());
        ps.setString(7, usuario.getFotoPerfil());
        ps.setString(8, usuario.getFotoCapa());
        ps.setInt(9, usuario.getTipo().id);
        ps.setInt(10, usuario.getId());

        ps.executeUpdate();

        return true;
    } catch (SQLException | URISyntaxException | IOException | ClassNotFoundException ex) {
        ex.printStackTrace();

        return false;
    } finally {
        desconectar();
    }
}