List of usage examples for java.sql PreparedStatement setTimestamp
void setTimestamp(int parameterIndex, java.sql.Timestamp x) throws SQLException;
java.sql.Timestamp
value. From source file:com.mirth.connect.server.migration.ServerMigrator.java
/** * Builds the database schema on the connected database if it does not exist * // w ww. j a va2 s . co m * @throws MigrationException */ private void initDatabase(Connection connection) throws MigrationException { // If missing this table we can assume that they don't have the schema installed if (!DatabaseUtil.tableExists(connection, "CONFIGURATION")) { executeScript("/" + getDatabaseType() + "/" + getDatabaseType() + "-database.sql"); /* * We must update the password date for the initial user. Previously we let the database * set this via CURRENT_TIMESTAMP, however this could create problems if the database is * running on a separate machine in a different timezone. (MIRTH-2902) */ PreparedStatement statement = null; try { statement = getConnection().prepareStatement("UPDATE PERSON_PASSWORD SET PASSWORD_DATE = ?"); statement.setTimestamp(1, new Timestamp(System.currentTimeMillis())); statement.executeUpdate(); } catch (SQLException e) { throw new MigrationException(e); } finally { DbUtils.closeQuietly(statement); } updateVersion(Version.getLatest()); } }
From source file:iddb.runtime.db.model.dao.impl.mysql.UserDAOImpl.java
@Override public void save(User user) { String sql;/*from w w w.ja va 2 s.c om*/ if (user.getKey() == null) { sql = "insert into user (loginid, roles, updated, created, password) values (?,?,?,?,?)"; } else { sql = "update user set loginid = ?," + "roles = ?," + "updated = ? where id = ? limit 1"; } Connection conn = null; try { conn = ConnectionFactory.getMasterConnection(); PreparedStatement st = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); st.setString(1, user.getLoginId()); st.setString(2, Functions.join(user.getRoles(), ",")); st.setTimestamp(3, new Timestamp(new Date().getTime())); if (user.getKey() != null) { st.setLong(4, user.getKey()); } else { st.setTimestamp(4, new Timestamp(new Date().getTime())); st.setString(5, user.getPassword()); } st.executeUpdate(); if (user.getKey() == null) { ResultSet rs = st.getGeneratedKeys(); if (rs != null && rs.next()) { user.setKey(rs.getLong(1)); } else { logger.warn("Couldn't get id for user {}", user.getLoginId()); } } } catch (SQLException e) { logger.error("Save: {}", e); } catch (IOException e) { logger.error("Save: {}", e); } finally { try { if (conn != null) conn.close(); } catch (Exception e) { } } }
From source file:nl.ordina.bag.etl.dao.AbstractBAGMutatiesDAO.java
@Override public void insert(final BAGMutatie mutatie) throws DAOException { try {/*from ww w . j a va 2 s . co m*/ jdbcTemplate.update(new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { try { PreparedStatement ps = connection.prepareStatement("insert into bag_mutatie (" + "id," + "tijdstip_verwerking," + "volgnr_verwerking," + "object_type," + "mutatie_product" + ") values ((select nvl(max(id),0) + 1 from bag_mutatie),?,?,?,?)"); ps.setTimestamp(1, Utils.toTimestamp(mutatie.getTijdstipVerwerking())); ps.setLong(2, mutatie.getVolgnrVerwerking()); ps.setInt(3, mutatie.getObjectType().ordinal()); ps.setString(4, XMLMessageBuilder.getInstance(MutatieProduct.class) .handle(new JAXBElement<MutatieProduct>(new QName( "http://www.kadaster.nl/schemas/bag-verstrekkingen/extract-producten-lvc/v20090901", "Mutatie-product"), MutatieProduct.class, mutatie.getMutatieProduct()))); return ps; } catch (JAXBException e) { throw new DAOException(e); } } }); } catch (DataAccessException e) { throw new DAOException(e); } }
From source file:net.duckling.ddl.service.resource.dao.StarmarkDAOImpl.java
@Override public int create(final Starmark starmark) { GeneratedKeyHolder keyHolder = new GeneratedKeyHolder(); this.getJdbcTemplate().update(new PreparedStatementCreator() { @Override/*from w w w. j ava 2 s . c o m*/ public PreparedStatement createPreparedStatement(Connection conn) throws SQLException { PreparedStatement ps = null; ps = conn.prepareStatement(SQL_CREATE, PreparedStatement.RETURN_GENERATED_KEYS); int i = 0; ps.setInt(++i, starmark.getRid()); ps.setInt(++i, starmark.getTid()); ps.setString(++i, starmark.getUid()); ps.setTimestamp(++i, new Timestamp(starmark.getCreateTime().getTime())); return ps; } }, keyHolder); Number key = keyHolder.getKey(); return (key == null) ? -1 : key.intValue(); }
From source file:Classes.Database.java
private void EscapeSQL(PreparedStatement preparedStatement, Object... arguments) throws SQLException { int i = 0;//ww w. j av a2s . c o m for (Object obj : arguments) { i++; if (obj != null && obj.getClass() == Date.class) // when the argument is a date then convert to a timestamp { preparedStatement.setTimestamp(i, DateToTimestamp((Date) obj)); } else if (obj != null && obj.getClass() == String.class) // when the argument is a string then escap all html4 stuff { preparedStatement.setObject(i, escapeHtml4((String) obj)); } else { preparedStatement.setObject(i, obj); } } }
From source file:edu.ku.brc.specify.plugins.ipadexporter.DataSetDlg.java
/** * @param title/*from ww w . j a va2 s.c o m*/ * @param uri * @param code * @return */ private boolean saveToLocalDB(final String title) { boolean isOK = false; Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement pStmt = null; try { String sql = "UPDATE institution SET Name=?, TimestampModified=? WHERE InstitutionID=?"; pStmt = conn.prepareStatement(sql); pStmt.setString(1, title); pStmt.setTimestamp(2, new Timestamp(Calendar.getInstance().getTimeInMillis())); pStmt.setInt(3, inst.getId()); isOK = pStmt.executeUpdate() == 1; pStmt.close(); DataProviderSessionIFace session = null; try { session = DataProviderFactory.getInstance().createSession(); inst = session.get(Institution.class, inst.getId()); AppContextMgr.getInstance().setClassObject(Institution.class, inst); } catch (Exception e) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(PermissionService.class, e); e.printStackTrace(); } finally { if (session != null) session.close(); } } catch (Exception ex) { ex.printStackTrace(); } return isOK; }
From source file:com.ccoe.build.dal.ProjectJDBCTemplate.java
public int create(final Project project, final String appName) { final String SQL = "insert into RBT_PROJECT (pool_name, name, group_id, artifact_id, type, version, " + "duration, status, start_time) " + "values (?, ?, ?, ?, ?, ?, ?, ?, ?)"; KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplateObject.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(SQL, new String[] { "id" }); ps.setString(1, appName);//www .j av a 2 s. c om ps.setString(2, project.getName()); ps.setString(3, project.getGroupId()); ps.setString(4, project.getArtifactId()); ps.setString(5, project.getType()); ps.setString(6, project.getVersion()); ps.setLong(7, project.getDuration()); ps.setString(8, project.getStatus()); ps.setTimestamp(9, new java.sql.Timestamp(project.getStartTime().getTime())); return ps; } }, keyHolder); return keyHolder.getKey().intValue(); }
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;/*from www. ja v a2 s .c o m*/ 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.stratelia.webactiv.util.DBUtil.java
/** * Centralization in order to sets the parameters on a prepare statement. * @param preparedStatement// w w w . j av a 2 s . c o m * @param parameters * @throws SQLException */ public static <O> void setParameters(PreparedStatement preparedStatement, Collection<O> parameters) throws SQLException { int paramIndex = 1; for (Object parameter : parameters) { if (parameter == null) { preparedStatement.setObject(paramIndex, null); } else if (parameter instanceof String) { preparedStatement.setString(paramIndex, (String) parameter); } else if (parameter instanceof Enum) { preparedStatement.setString(paramIndex, ((Enum) parameter).name()); } else if (parameter instanceof Integer) { preparedStatement.setInt(paramIndex, (Integer) parameter); } else if (parameter instanceof Long) { preparedStatement.setLong(paramIndex, (Long) parameter); } else if (parameter instanceof Timestamp) { preparedStatement.setTimestamp(paramIndex, (Timestamp) parameter); } else if (parameter instanceof Date) { preparedStatement.setDate(paramIndex, new java.sql.Date(((Date) parameter).getTime())); } else if (parameter instanceof UserDetail) { preparedStatement.setString(paramIndex, ((UserDetail) parameter).getId()); } else { throw new IllegalArgumentException("SQL parameter type not handled: " + parameter.getClass()); } paramIndex++; } }
From source file:com.taobao.diamond.server.service.DBPersistService.java
public void addConfigInfo(final Timestamp time, final ConfigInfo configInfo) { this.jt.update( "insert into config_info (data_id,group_id,content,md5,gmt_create,gmt_modified) values(?,?,?,?,?,?)", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { int index = 1; ps.setString(index++, configInfo.getDataId()); ps.setString(index++, configInfo.getGroup()); ps.setString(index++, configInfo.getContent()); ps.setString(index++, configInfo.getMd5()); ps.setTimestamp(index++, time); ps.setTimestamp(index++, time); }//from w w w. jav a 2 s .c o m }); }