List of usage examples for java.sql PreparedStatement setDate
void setDate(int parameterIndex, java.sql.Date x) throws SQLException;
java.sql.Date
value using the default time zone of the virtual machine that is running the application. From source file:com.microsoftopentechnologies.azchat.web.dao.UserPreferenceDAOImpl.java
/** * This method populates Prepared Statement from userPreferenceEntity * object./*from w w w . j av a 2 s . com*/ * * @param preparedStatement * @param userPreferenceEntity * @return * @throws SQLException */ public PreparedStatement generatePreparedStatement(PreparedStatement preparedStatement, UserPreferenceEntity userPreferenceEntity) throws SQLException { preparedStatement.setInt(1, userPreferenceEntity.getUserId()); preparedStatement.setInt(2, userPreferenceEntity.getPreferenceId()); preparedStatement.setDate(3, new Date(userPreferenceEntity.getDateCreated().getTime())); preparedStatement.setDate(4, new Date(userPreferenceEntity.getCreatedBy().getTime())); preparedStatement.setDate(5, new Date(userPreferenceEntity.getDateModified().getTime())); preparedStatement.setDate(6, new Date(userPreferenceEntity.getModifiedBy().getTime())); return preparedStatement; }
From source file:nl.ordina.bag.etl.dao.postgres.BAGMutatiesDAOImpl.java
@Override public long insertMutatiesFile(final java.util.Date dateFrom, final java.util.Date dateTo, final byte[] content) throws DAOException { try {/* www . j a va 2 s. c o m*/ return jdbcTemplate.query(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement("insert into bag_mutaties_file (" + " id," + " date_from," + " date_to," + " content" + ") values ((select coalesce(max(id),0) + 1 from bag_mutaties_file),date_trunc('day', ?::timestamp),date_trunc('day', ?::timestamp),?)" + " returning id"); ps.setDate(1, new Date(dateFrom.getTime())); ps.setDate(2, new Date(dateTo.getTime())); ps.setBytes(3, content); return ps; } }, new IdExtractor()); } catch (DataAccessException e) { throw new DAOException(e); } }
From source file:mx.edu.um.escuela.dao.MaestroDaoJdbc.java
@Override public Maestro crea(final Maestro maestro) { log.debug("Creando al maestro {}", maestro); KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(new PreparedStatementCreator() { @Override/*from w ww . j a v a2 s .co m*/ public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(CREAR_MAESTRO, new String[] { "id" }); ps.setString(1, maestro.getNomina()); ps.setString(2, maestro.getNombre()); ps.setString(3, maestro.getApellido()); if (maestro.getFechaNacimiento() != null) { ps.setDate(4, new java.sql.Date(maestro.getFechaNacimiento().getTime())); } else { ps.setNull(4, Types.DATE); } ps.setBoolean(5, maestro.getEsHombre()); if (maestro.getCorreo() != null) { ps.setString(6, maestro.getCorreo()); } else { ps.setNull(6, Types.VARCHAR); } return ps; } }, keyHolder); maestro.setId(keyHolder.getKey().longValue()); maestros.add(maestro); return maestro; }
From source file:cz.muni.fi.javaseminar.kafa.bookregister.BookManagerImpl.java
@Override @Transactional(readOnly = false)/* w w w. j a v a 2 s.co m*/ public void createBook(Book book) { validate(book); if (book.getId() != null) { throw new IllegalArgumentException("book id is already set"); } KeyHolder keyHolder = new GeneratedKeyHolder(); int updated = jdbcTemplate.update((Connection connection) -> { PreparedStatement ps = connection.prepareStatement( "INSERT INTO BOOK (name,isbn,published,author_id) VALUES (?,?,?,?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1, book.getName()); ps.setString(2, book.getIsbn()); Date date = Date.valueOf(book.getPublished()); ps.setDate(3, date); if (book.getAuthorId() == null) { ps.setNull(4, Types.BIGINT); } else { ps.setLong(4, book.getAuthorId()); } return ps; }, keyHolder); book.setId(keyHolder.getKey().longValue()); DBUtils.checkUpdatesCount(updated, book, true); }
From source file:org.rti.zcore.dar.report.CDRROIReport.java
/** * Retrieve all Encounter records for this form 132 - Patient dispensary * @param conn// ww w . j a va 2 s . c o m * @param siteID * @param beginDate * @param endDate * @return * @throws ServletException */ protected static ResultSet getEncounters(Connection conn, int siteID, Date beginDate, Date endDate) throws ServletException { ResultSet rs = null; String dateRange = "AND date_visit >= ? AND date_visit <= ? "; if (endDate == null) { dateRange = "AND date_visit = ?"; } try { if (siteID == 0) { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); } rs = ps.executeQuery(); } else { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "AND encounter.site_id = ? " + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); ps.setInt(3, siteID); } else { ps.setInt(2, siteID); } rs = ps.executeQuery(); } } catch (Exception ex) { log.error(ex); } return rs; }
From source file:com.cedarsoftware.ncube.NCubeManager.java
/** * Return an array [] of Strings containing all unique App names. *//*from ww w . j av a2 s .c om*/ public static Object[] getAppNames(Connection connection, Date sysDate) { validateConnection(connection); if (sysDate == null) { sysDate = new Date(); } PreparedStatement stmt = null; try { java.sql.Date systemDate = new java.sql.Date(sysDate.getTime()); stmt = connection.prepareStatement( "SELECT DISTINCT app_cd FROM n_cube WHERE sys_effective_dt <= ? AND (sys_expiration_dt IS NULL OR sys_expiration_dt >= ?)"); stmt.setDate(1, systemDate); stmt.setDate(2, systemDate); ResultSet rs = stmt.executeQuery(); List<String> records = new ArrayList<String>(); while (rs.next()) { records.add(rs.getString(1)); } Collections.sort(records); return records.toArray(); } catch (Exception e) { String s = "Unable to fetch all ncube app names from database"; LOG.error(s, e); throw new RuntimeException(s, e); } finally { jdbcCleanup(stmt); } }
From source file:org.rti.zcore.dar.report.CDRROIReport.java
/** * Get ART regimen records (regimen code field) for this period/site * @param conn//from w ww .j av a2s . c o m * @param siteID * @param beginDate * @param endDate * @return * @throws ServletException */ protected static ResultSet getArtRegimens(Connection conn, int siteID, Date beginDate, Date endDate) throws ServletException { ResultSet rs = null; String dateRange = "AND date_visit >= ? AND date_visit <= ? "; if (endDate == null) { dateRange = "AND date_visit = ?"; } try { if (siteID == 0) { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, encounter.created_by AS created_by, " + "regimen.code AS code, age_at_first_visit AS age, encounter.created " + "FROM art_regimen, encounter, regimen, patient " + "WHERE encounter.id = art_regimen.id " + "AND art_regimen.regimen_1 = regimen.id " + "AND encounter.patient_id = patient.id " + dateRange + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); } rs = ps.executeQuery(); } else { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, encounter.created_by AS created_by, " + "regimen.code AS code, age_at_first_visit AS age, encounter.created " + "FROM art_regimen, encounter, regimen, patient " + "WHERE encounter.id = art_regimen.id " + "AND art_regimen.regimen_1 = regimen.id " + "AND encounter.patient_id = patient.id " + dateRange + "AND encounter.site_id = ? " + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); ps.setInt(3, siteID); } else { ps.setInt(2, siteID); } rs = ps.executeQuery(); } } catch (Exception ex) { log.error(ex); } return rs; }
From source file:com.cedarsoftware.ncube.NCubeManager.java
/** * Update the notes associated to an NCube * * @return true if the update succeeds, false otherwise *//*from w ww .j ava 2 s . c o m*/ public static boolean updateNotes(Connection connection, String app, String name, String version, String notes) { validate(connection, app, version); validateCubeName(name); synchronized (cubeList) { PreparedStatement stmt = null; try { stmt = connection.prepareStatement( "UPDATE n_cube SET notes_bin = ?, update_dt = ? WHERE app_cd = ? AND n_cube_nm = ? AND version_no_cd = ?"); stmt.setBytes(1, notes == null ? null : notes.getBytes("UTF-8")); stmt.setDate(2, new java.sql.Date(System.currentTimeMillis())); stmt.setString(3, app); stmt.setString(4, name); stmt.setString(5, version); int count = stmt.executeUpdate(); if (count > 1) { throw new IllegalStateException("Only one (1) row's notes should be updated."); } if (count == 0) { throw new IllegalStateException( "No NCube matching app: " + app + ", name: " + name + ", version: " + version); } return true; } catch (IllegalStateException e) { throw e; } catch (Exception e) { throw new RuntimeException( "Unable to update notes for NCube: " + name + ", app: " + app + ", version: " + version, e); } finally { jdbcCleanup(stmt); } } }
From source file:org.rti.zcore.dar.dao.InventoryDAO.java
protected static ResultSet getEncountersForDrug(Connection conn, int siteID, Date beginDate, Date endDate) throws ServletException { ResultSet rs = null;//from w ww.j a v a 2 s.c om String dateRange = "AND date_visit >= ? AND date_visit <= ? "; if (endDate == null) { dateRange = "AND date_visit = ?"; } try { if (siteID == 0) { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, age_category, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); } rs = ps.executeQuery(); } else { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, age_category, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "AND encounter.site_id = ? " + "ORDER BY created, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); ps.setInt(3, siteID); } else { ps.setInt(2, siteID); } rs = ps.executeQuery(); } } catch (Exception ex) { log.error(ex); } return rs; }
From source file:org.rti.zcore.dar.dao.InventoryDAO.java
/** * Retrieve all Encounter records for this form 132 - Patient dispensary * Order by date_visit./*from w w w . j av a2 s .c om*/ * @param conn * @param siteID * @param beginDate * @param endDate - may be null. * @return * @throws ServletException */ public static ResultSet getPatientDispensaryEncounters(Connection conn, int siteID, Date beginDate, Date endDate) throws ServletException { ResultSet rs = null; String dateRange = "AND date_visit >= ? AND date_visit <= ? "; if (endDate == null) { dateRange = "AND date_visit = ?"; } try { if (siteID == 0) { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, age_category, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "ORDER BY date_visit, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); } rs = ps.executeQuery(); } else { String sql = "SELECT encounter.id AS id, date_visit, patient_id, district_patient_id, " + "first_name, surname, encounter.site_id, age_at_first_visit, age_category, encounter.created_by AS created_by, encounter.created " + "FROM encounter, patient " + "WHERE encounter.patient_id = patient.id " + "AND form_id = 132\n" + dateRange + "AND encounter.site_id = ? " + "ORDER BY date_visit, surname"; PreparedStatement ps = conn.prepareStatement(sql); ps.setDate(1, beginDate); if (endDate != null) { ps.setDate(2, endDate); ps.setInt(3, siteID); } else { ps.setInt(2, siteID); } rs = ps.executeQuery(); } } catch (Exception ex) { log.error(ex); } return rs; }