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.threecrickets.prudence.cache.SqlCache.java
public void prune() { // Note that this will not discard locks try {// ww w. jav a 2 s. co m Connection connection = connect(); if (connection == null) return; try { String sql = "DELETE FROM " + cacheTableName + " WHERE expiration_date<?"; PreparedStatement statement = connection.prepareStatement(sql); try { statement.setTimestamp(1, new Timestamp(System.currentTimeMillis())); if (!statement.execute()) logger.fine("Pruned " + statement.getUpdateCount()); } finally { statement.close(); } } finally { connection.close(); } } catch (SQLException x) { logger.log(Level.WARNING, "Could not prune", x); } }
From source file:com.surfs.storage.common.datasource.jdbc.JdbcDao.java
@Override public Object insert(String poolName, String sql, Object... params) throws Exception { Connection conn = null;/* www .ja v a2 s . co m*/ PreparedStatement ps = null; ResultSet rs = null; try { conn = getConnection(poolName); ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (int i = 0; i < params.length; i++) { if (params[i] instanceof String) ps.setString(i + 1, (String) params[i]); else if (params[i] instanceof Integer) ps.setInt(i + 1, (Integer) params[i]); else if (params[i] instanceof Long) ps.setLong(i + 1, (Long) params[i]); else if (params[i] instanceof Timestamp) ps.setTimestamp(i + 1, (Timestamp) params[i]); } ps.execute(); rs = ps.getGeneratedKeys(); if (rs.next()) return rs.getObject(1); return null; } catch (Exception e) { throw e; } finally { JdbcUtils.closeResultset(rs); JdbcUtils.closeStatement(ps); JdbcUtils.closeConnect(conn); } }
From source file:com.surfs.storage.common.datasource.jdbc.JdbcDao.java
@Override public <T> List<T> queryForList(String poolName, String sql, RowMapper<T> mapper, Object... params) throws Exception { Connection conn = null;/*from ww w. j ava 2s.c om*/ PreparedStatement ps = null; ResultSet rs = null; List<T> list = new ArrayList<>(); try { conn = getConnection(poolName); ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { if (params[i] instanceof String) ps.setString(i + 1, (String) params[i]); else if (params[i] instanceof Integer) ps.setInt(i + 1, (Integer) params[i]); else if (params[i] instanceof Long) ps.setLong(i + 1, (Long) params[i]); else if (params[i] instanceof Timestamp) ps.setTimestamp(i + 1, (Timestamp) params[i]); } rs = ps.executeQuery(); while (rs.next()) { T t = mapper.mapRow(rs); list.add(t); } return list; } catch (Exception e) { throw e; } finally { JdbcUtils.closeResultset(rs); JdbcUtils.closeStatement(ps); JdbcUtils.closeConnect(conn); } }
From source file:no.polaric.aprsdb.MyDBSession.java
/** * Return the mission that was (or is going to be) active for a station at a * given time. // www .j a v a2s. c om * If time is null, return the mission currently active. * * @param src Source callsign (or identifier) * @param at Time when the mission (that we search for) is active. */ public Mission getMission(String src, java.util.Date at) throws java.sql.SQLException { PreparedStatement stmt = getCon().prepareStatement( " SELECT src,alias,icon,start,end,descr FROM \"Mission\"" + " WHERE src=? AND time = ?", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stmt.setString(1, src); stmt.setTimestamp(2, date2ts(at)); ResultSet rs = stmt.executeQuery(); if (rs.next()) return new Mission(rs.getString("src"), rs.getString("alias"), rs.getString("icon"), rs.getTimestamp("start"), rs.getTimestamp("end"), rs.getString("descr")); else return null; }
From source file:com.thinkmore.framework.orm.hibernate.SimpleHibernateDao.java
public void setParameters(PreparedStatement ps, int j, Object value) throws SQLException { if (value != null) { if (value instanceof java.lang.Integer) { ps.setInt(j, (Integer) value); } else if (value instanceof java.lang.Long) { ps.setLong(j, (Long) value); } else if (value instanceof java.util.Date) { ps.setTimestamp(j, new java.sql.Timestamp(((Date) value).getTime())); } else if (value instanceof java.sql.Date) { ps.setDate(j, new java.sql.Date(((Date) value).getTime())); } else if (value instanceof java.lang.String) { ps.setString(j, value.toString()); } else if (value instanceof java.lang.Double) { ps.setDouble(j, (Double) value); } else if (value instanceof java.lang.Byte) { ps.setByte(j, (Byte) value); } else if (value instanceof java.lang.Character) { ps.setString(j, value.toString()); } else if (value instanceof java.lang.Float) { ps.setFloat(j, (Float) value); } else if (value instanceof java.lang.Boolean) { ps.setBoolean(j, (Boolean) value); } else if (value instanceof java.lang.Short) { ps.setShort(j, (Short) value); } else {//from w w w. j av a 2 s . co m ps.setObject(j, value); } } else { ps.setNull(j, Types.NULL); } }
From source file:no.polaric.aprsdb.MyDBSession.java
/** * Get an APRS item at a given point in time. *//*www. j a va 2 s.c o m*/ public AprsPoint getItem(String src, java.util.Date at) throws java.sql.SQLException { _log.debug("MyDbSession", "getItem: " + src + ", " + df.format(at)); PreparedStatement stmt = getCon().prepareStatement( " SELECT * FROM \"PosReport\"" + " WHERE src=? AND time <= ?" + " ORDER BY time DESC LIMIT 1", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stmt.setString(1, src); stmt.setTimestamp(2, new Timestamp(at.getTime())); ResultSet rs = stmt.executeQuery(); String name[] = src.split("@", 2); AprsPoint x = null; if (name.length > 1) { Station owner = _api.getDB().getStation(name[1], null); x = new AprsObject(owner, name[0]); } else x = new Station(src); if (rs.next()) x.update( rs.getDate("time"), new AprsHandler.PosData(getRef(rs, "position"), rs.getInt("course"), rs.getInt("speed"), rs.getString("symbol").charAt(0), rs.getString("symtab").charAt(0)), null, null); return x; }
From source file:net.mindengine.oculus.frontend.service.project.JdbcProjectDAO.java
@Override public Long createProject(Project project) throws Exception { PreparedStatement ps = getConnection().prepareStatement( "insert into projects (name, description, path, parent_id, icon, author_id, date) values (?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); ps.setString(1, project.getName());// w w w .jav a 2 s . c o m ps.setString(2, project.getDescription()); ps.setString(3, project.getPath()); ps.setLong(4, project.getParentId()); ps.setString(5, project.getIcon()); ps.setLong(6, project.getAuthorId()); ps.setTimestamp(7, new Timestamp(project.getDate().getTime())); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); Long projectId = null; if (rs.next()) { projectId = rs.getLong(1); } if (project.getParentId() > 0) { // Increasing the parents project subprojects_count var update("update projects set subprojects_count = subprojects_count+1 where id = :id", "id", project.getParentId()); } return projectId; }
From source file:com.starit.diamond.server.service.DBPersistService.java
public void addConfigInfo(final Timestamp time, final ConfigInfo configInfo) { final Long id = this.jt.queryForObject("SELECT MAX(ID) FROM config_info", Long.class); this.jt.update( "insert into config_info (id, data_id,group_id, username, content,md5,gmt_create,gmt_modified, description) values(?,?,?,?,?,?,?,?,?)", new PreparedStatementSetter() { public void setValues(PreparedStatement ps) throws SQLException { int index = 1; if (id == null) ps.setLong(index++, 1); else ps.setLong(index++, (Long) id + 1); ps.setString(index++, configInfo.getDataId()); ps.setString(index++, configInfo.getGroup()); ps.setString(index++, configInfo.getUserName()); ps.setString(index++, configInfo.getContent()); ps.setString(index++, configInfo.getMd5()); ps.setTimestamp(index++, time); ps.setTimestamp(index++, time); ps.setString(index++, configInfo.getDescription()); }// w w w . ja v a 2 s . c om }); }
From source file:com.glaf.core.execution.FileExecutionHelper.java
public void save(Connection connection, String serviceKey, File file) { String sql = " insert into " + BlobItemDomainFactory.TABLENAME + " (ID_, BUSINESSKEY_, FILEID_, SERVICEKEY_, NAME_, TYPE_, FILENAME_, PATH_, LASTMODIFIED_, LOCKED_, STATUS_, DATA_, CREATEBY_, CREATEDATE_)" + " values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; PreparedStatement psmt = null; try {//ww w . ja v a 2 s . c o m psmt = connection.prepareStatement(sql); psmt.setString(1, UUID32.getUUID()); psmt.setString(2, serviceKey); psmt.setString(3, DigestUtils.md5Hex(file.getAbsolutePath())); psmt.setString(4, serviceKey); psmt.setString(5, file.getName()); psmt.setString(6, "Execution"); psmt.setString(7, file.getAbsolutePath()); psmt.setString(8, file.getAbsolutePath()); psmt.setLong(9, file.lastModified()); psmt.setInt(10, 0); psmt.setInt(11, 1); psmt.setBytes(12, FileUtils.getBytes(file)); psmt.setString(13, "system"); psmt.setTimestamp(14, DateUtils.toTimestamp(new java.util.Date())); psmt.executeUpdate(); } catch (Exception ex) { logger.error(ex); ex.printStackTrace(); throw new RuntimeException(ex); } finally { JdbcUtils.close(psmt); } }
From source file:au.edu.jcu.fascinator.plugin.harvester.directory.DerbyCache.java
private void updateLastModified(String oid, long lastModified) throws Exception { PreparedStatement sql = connection().prepareStatement("UPDATE " + BASIC_TABLE + " SET lastModified = ?, changeFlag = 1" + " WHERE oid = ? and cacheId = ?"); // Prepare and execute sql.setTimestamp(1, new Timestamp(lastModified)); sql.setString(2, oid);/*from www .j a v a 2s .c o m*/ sql.setString(3, cacheId); sql.executeUpdate(); close(sql); }