List of usage examples for java.sql PreparedStatement setLong
void setLong(int parameterIndex, long x) throws SQLException;
long
value. From source file:net.mindengine.oculus.frontend.service.project.build.JdbcBuildDAO.java
@Override public long createBuild(Build build) throws Exception { String sql = "insert into builds (name, description, date, project_id) values (?,?,?,?)"; PreparedStatement ps = getConnection().prepareStatement(sql); ps.setString(1, build.getName());/*from ww w . j ava2 s . c om*/ ps.setString(2, build.getDescription()); ps.setTimestamp(3, new Timestamp(build.getDate().getTime())); ps.setLong(4, build.getProjectId()); logger.info(ps); ps.execute(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { return rs.getLong(1); } return 0; }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Prepare a statement for iteration given a query string, fetch size * and some args./*from ww w . j a v a2 s . c o m*/ * * NB: the provided connection is not closed. * * @param c a Database connection * @param fetchSize hint to JDBC driver on number of results to cache * @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, int fetchSize, String query, Object... args) throws SQLException { ArgumentNotValid.checkNotNull(c, "Connection c"); ArgumentNotValid.checkPositive(fetchSize, "int fetchSize"); ArgumentNotValid.checkNotNullOrEmpty(query, "String query"); ArgumentNotValid.checkNotNull(args, "Object... args"); c.setAutoCommit(false); PreparedStatement s = c.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); s.setFetchSize(fetchSize); 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:com.hs.mail.imap.dao.MySqlMailboxDao.java
private Mailbox doCreateMailbox(final long ownerID, final String mailboxName) { final String sql = "INSERT INTO mailbox (name, ownerid, nextuid, uidvalidity) VALUES(?, ?, ?, ?)"; final long uidValidity = System.currentTimeMillis(); KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pstmt.setString(1, mailboxName); pstmt.setLong(2, ownerID); pstmt.setLong(3, 1);//w ww . ja v a2 s .c o m pstmt.setLong(4, uidValidity); return pstmt; } }, keyHolder); Mailbox mailbox = new Mailbox(); mailbox.setMailboxID(keyHolder.getKey().longValue()); mailbox.setOwnerID(ownerID); mailbox.setName(mailboxName); mailbox.setNextUID(1); mailbox.setUidValidity(uidValidity); return mailbox; }
From source file:car_counter.storage.sqlite.SqliteStorage.java
@Override public void store(Path destinationFile, Collection<DetectedVehicle> detectedVehicles) { try {//from w w w. j a v a2 s. c o m PreparedStatement statement = connection.prepareStatement("insert into detected_vehicles " + "(timestamp, initial_location, end_location, speed, colour, video_file) values " + "(?, ?, ?, ?, ?, ?)"); for (DetectedVehicle detectedVehicle : detectedVehicles) { statement.setLong(1, detectedVehicle.getDateTime().getMillis()); statement.setLong(2, detectedVehicle.getInitialLocation().ordinal()); statement.setLong(3, detectedVehicle.getEndLocation().ordinal()); if (detectedVehicle.getSpeed() == null) { statement.setNull(4, Types.NULL); } else { statement.setFloat(4, detectedVehicle.getSpeed()); } statement.setString(5, null); statement.setString(6, destinationFile.getFileName().toString()); statement.executeUpdate(); } } catch (SQLException e) { throw new IllegalStateException("Error inserting records", e); } }
From source file:com.glaf.base.test.service.MxMixFeatureTestService.java
@Transactional public void run() { log.debug("-------------------start run-------------------"); for (int i = 0; i < 2; i++) { SysLog bean = new SysLog(); bean.setAccount("test"); bean.setIp("127.0.0.1"); bean.setOperate("add"); sysLogService.create(bean);//from w w w . j a v a2 s . c o m } String sql = "insert into SYS_LOG(ID, ACCOUNT, IP, CREATETIME, MODULEID, OPERATE, FLAG, TIMEMS) values (?, ?, ?, ?, ?, ?, ?, ?) "; Connection connection = null; PreparedStatement psmt = null; try { connection = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); System.out.println("connection:" + connection.toString()); psmt = connection.prepareStatement(sql); for (int i = 0; i < 2; i++) { psmt.setLong(1, idGenerator.nextId()); psmt.setString(2, "test2"); psmt.setString(3, "192.168.0.100"); psmt.setTimestamp(4, DateUtils.toTimestamp(new Date())); psmt.setString(5, "xx"); psmt.setString(6, "Y"); psmt.setInt(7, 1); psmt.setLong(8, i * i); psmt.addBatch(); } psmt.executeBatch(); psmt.close(); } catch (Exception ex) { ex.printStackTrace(); log.error(ex); throw new RuntimeException(ex); } finally { JdbcUtils.close(psmt); } log.debug("-------------------end run-------------------"); }
From source file:com.surveypanel.dao.JDBCFormDAO.java
@Override public boolean qualify(String formId, long surveyId, Map<String, Object> defaultValues) { FormDTO load = load(formId, surveyId); Map<String, Object> values = defaultValues; values.putAll(load.getValues());//from w w w .ja v a2 s . c o m String sql = "INSERT INTO survey_values_" + surveyId + " (question,name,value,formId,surveyId,created) VALUES (?,?,?,?,?,?);"; final Object[][] args = new Object[values.size()][5]; int count = 0; for (Entry<String, Object> value : values.entrySet()) { String fieldName = value.getKey(); String[] key = fieldName.split("_"); args[count] = new Object[] { key[0], fieldName, (String) value.getValue(), formId, surveyId }; count++; } final int counter = values.size(); batchUpdate(sql, new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setString(1, (String) args[i][0]); ps.setString(2, (String) args[i][1]); ps.setString(3, (String) args[i][2]); ps.setString(4, (String) args[i][3]); ps.setLong(5, (Long) args[i][4]); ps.setTimestamp(6, new Timestamp(Calendar.getInstance().getTimeInMillis())); } public int getBatchSize() { return counter; } }); update("UPDATE survey_" + surveyId + " SET qualified = true WHERE id = ?", new Object[] { formId }); return false; }
From source file:com.anyuan.thomweboss.persistence.jdbcimpl.user.UserDaoJdbcImpl.java
private long saveContact(Connection conn, Contact contact) { long id = -1; String sql = "insert into t_contact(f_email, f_phoneid, f_addressid)" + " values(?, ?, ?)"; try {/*from w w w .j a va2 s .c o m*/ PreparedStatement preState = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS); preState.setString(1, contact.getEmail()); preState.setLong(2, contact.getPhone().getId()); preState.setLong(3, contact.getAddress().getId()); preState.execute(); id = generateId(preState); if (-1 != id) { contact.setId(id); } } catch (SQLException e) { e.printStackTrace(); } return id; }
From source file:zerogame.info.javapay.dao.PayOrderDao.java
@Override public PayOrder add(final PayOrder payorder) { try {//from w w w.j av a 2 s. c om KeyHolder keyHolder = new GeneratedKeyHolder(); getTemplate().update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement("insert into pay_order" + "(uin,account_id,order_id,product_id,product_name,product_desc,money,channel,serverId,status,order_type,create_time,user_level,vip_level) " + "values(?,?,?,?,?,?,?,?,?,?,?,now(),?,?)", Statement.RETURN_GENERATED_KEYS); ps.setLong(1, payorder.getUin()); ps.setString(2, payorder.getAccountId()); ps.setString(3, payorder.getOrderId()); ps.setString(4, payorder.getProductId()); ps.setString(5, payorder.getProductName()); ps.setString(6, payorder.getProductDesc()); ps.setInt(7, payorder.getMoney()); ps.setInt(8, payorder.getChannel()); ps.setInt(9, payorder.getServerId()); ps.setInt(10, payorder.getStatus()); ps.setInt(11, payorder.getOrderType()); ps.setInt(12, payorder.getUserLevel()); ps.setInt(13, payorder.getUserVipLevel()); return ps; } }, keyHolder); return payorder; } catch (Exception e) { logger.warn("add pay order failed", e); return null; } }
From source file:io.lavagna.service.CardDataRepository.java
@Transactional(readOnly = false) public int addUploadContent(final String digest, final long fileSize, final InputStream content, final String contentType) { LobHandler lobHandler = new DefaultLobHandler(); return jdbc.getJdbcOperations().execute(queries.addUploadContent(), new AbstractLobCreatingPreparedStatementCallback(lobHandler) { @Override/*from w w w .j a v a 2s . c o m*/ protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException { ps.setString(1, digest); ps.setLong(2, fileSize); lobCreator.setBlobAsBinaryStream(ps, 3, content, (int) fileSize); ps.setString(4, contentType); } }); }
From source file:com.stratelia.webactiv.util.DBUtil.java
/** * Centralization in order to sets the parameters on a prepare statement. * @param preparedStatement//from w ww . j av a 2s . 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++; } }