List of usage examples for java.sql PreparedStatement setBoolean
void setBoolean(int parameterIndex, boolean x) throws SQLException;
boolean
value. From source file:dk.netarkivet.harvester.datamodel.ScheduleDBDAO.java
/** Sets the first twelve parameters of a Schedule in the order. * name, comments, startdate, enddate, maxrepeats, * timeunit, numtimeunits, anytime, onminute, onhour, * ondayofweek, ondayofmonth/*from w w w . j a v a 2s .co m*/ * @param s a prepared SQL statement * @param schedule a given schedule. * @throws SQLException If the operation fails. */ private void setScheduleParameters(PreparedStatement s, Schedule schedule) throws SQLException { DBUtils.setName(s, 1, schedule, Constants.MAX_NAME_SIZE); DBUtils.setComments(s, 2, schedule, Constants.MAX_COMMENT_SIZE); final Date startDate = schedule.getStartDate(); final int fieldNum = 3; DBUtils.setDateMaybeNull(s, fieldNum, startDate); if (schedule instanceof TimedSchedule) { TimedSchedule ts = (TimedSchedule) schedule; DBUtils.setDateMaybeNull(s, 4, ts.getEndDate()); s.setNull(5, Types.BIGINT); } else { s.setNull(4, Types.DATE); RepeatingSchedule rs = (RepeatingSchedule) schedule; s.setLong(5, rs.getRepeats()); } Frequency freq = schedule.getFrequency(); s.setInt(6, freq.ordinal()); s.setInt(7, freq.getNumUnits()); s.setBoolean(8, freq.isAnytime()); DBUtils.setIntegerMaybeNull(s, 9, freq.getOnMinute()); DBUtils.setIntegerMaybeNull(s, 10, freq.getOnHour()); DBUtils.setIntegerMaybeNull(s, 11, freq.getOnDayOfWeek()); DBUtils.setIntegerMaybeNull(s, 12, freq.getOnDayOfMonth()); }
From source file:org.apache.openjpa.jdbc.sql.PostgresDictionary.java
@Override public void setBoolean(PreparedStatement stmnt, int idx, boolean val, Column col) throws SQLException { // postgres actually requires that a boolean be set: it cannot // handle a numeric argument. stmnt.setBoolean(idx, val); }
From source file:org.apache.hadoop.hive.jdbc.TestJdbcDriver.java
private PreparedStatement createPreapredStatementUsingSetXXX(String sql) throws SQLException { PreparedStatement ps = con.prepareStatement(sql); ps.setBoolean(1, true); //setBoolean ps.setBoolean(2, true); //setBoolean ps.setShort(3, Short.valueOf("1")); //setShort ps.setInt(4, 2); //setInt ps.setFloat(5, 3f); //setFloat ps.setDouble(6, Double.valueOf(4)); //setDouble ps.setString(7, "test'string\""); //setString ps.setLong(8, 5L); //setLong ps.setByte(9, (byte) 1); //setByte ps.setByte(10, (byte) 1); //setByte ps.setString(11, "2012-01-01"); //setString ps.setMaxRows(2);/*from w w w .j av a 2 s . com*/ return ps; }
From source file:org.dspace.storage.rdbms.MockDatabaseManager.java
@Mock private static void loadParameters(PreparedStatement statement, Collection<ColumnInfo> columns, TableRow row) throws SQLException { int count = 0; for (ColumnInfo info : columns) { count++;/*from w w w. j av a2 s . c o m*/ String column = info.getName(); int jdbctype = info.getType(); if (row.isColumnNull(column)) { statement.setNull(count, jdbctype); } else { switch (jdbctype) { case Types.BIT: case Types.BOOLEAN: statement.setBoolean(count, row.getBooleanColumn(column)); break; case Types.INTEGER: if (isOracle) { statement.setLong(count, row.getLongColumn(column)); } else { statement.setInt(count, row.getIntColumn(column)); } break; case Types.NUMERIC: case Types.DECIMAL: statement.setLong(count, row.getLongColumn(column)); // FIXME should be BigDecimal if TableRow supported that break; case Types.BIGINT: statement.setLong(count, row.getLongColumn(column)); break; case Types.CLOB: if (isOracle) { // Support CLOBs in place of TEXT columns in Oracle statement.setString(count, row.getStringColumn(column)); } else { throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } break; case Types.VARCHAR: statement.setString(count, row.getStringColumn(column)); break; case Types.DATE: statement.setDate(count, new java.sql.Date(row.getDateColumn(column).getTime())); break; case Types.TIME: statement.setTime(count, new Time(row.getDateColumn(column).getTime())); break; case Types.TIMESTAMP: statement.setTimestamp(count, new Timestamp(row.getDateColumn(column).getTime())); break; default: throw new IllegalArgumentException("Unsupported JDBC type: " + jdbctype); } } } }
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 ww w. j av a2 s . c o m*/ * @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:com.krminc.phr.security.PHRRealm.java
private void doFailedUpdate(String username, java.sql.Timestamp windowStart, int failedAttempts, boolean setLock) { String query = "UPDATE user_users SET failed_password_attempts = ? , failed_password_window_start = ? , is_locked_out = ?, lockout_begin = ? WHERE username = ?"; PreparedStatement st = null; java.sql.Timestamp lockoutBegin = null; if (setLock) { GregorianCalendar tempCal = new GregorianCalendar(java.util.TimeZone.getTimeZone("GMT")); lockoutBegin = new java.sql.Timestamp(tempCal.getTimeInMillis()); }/*from ww w.j a va 2 s . c o m*/ try { createDS(); //TX for UPDATE conn = ds.getConnection(); st = conn.prepareStatement(query); st.setInt(1, failedAttempts); st.setTimestamp(2, windowStart); st.setBoolean(3, setLock); st.setTimestamp(4, lockoutBegin); st.setString(5, username); st.executeUpdate(); } catch (Exception e) { log("Error updating failed password values"); log(e.getMessage()); } finally { try { st.close(); conn.close(); } catch (Exception e) { log(e.getMessage()); } conn = null; } }
From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectOracle.java
@Override public void setToPreparedStatement(PreparedStatement ps, int index, Serializable value, Column column) throws SQLException { switch (column.getJdbcType()) { case Types.VARCHAR: case Types.CLOB: setToPreparedStatementString(ps, index, value, column); return;//from w w w . j a v a2 s . c o m case Types.BIT: ps.setBoolean(index, ((Boolean) value).booleanValue()); return; case Types.TINYINT: case Types.SMALLINT: ps.setInt(index, ((Long) value).intValue()); return; case Types.INTEGER: case Types.BIGINT: ps.setLong(index, ((Number) value).longValue()); return; case Types.DOUBLE: ps.setDouble(index, ((Double) value).doubleValue()); return; case Types.TIMESTAMP: setToPreparedStatementTimestamp(ps, index, value, column); return; case Types.OTHER: ColumnType type = column.getType(); if (type.isId()) { setId(ps, index, value); return; } else if (type == ColumnType.FTSTORED) { ps.setString(index, (String) value); return; } throw new SQLException("Unhandled type: " + column.getType()); default: throw new SQLException("Unhandled JDBC type: " + column.getJdbcType()); } }
From source file:com.flexive.ejb.beans.MandatorEngineBean.java
/** * {@inheritDoc}// ww w . java 2s . c o m */ @Override @TransactionAttribute(TransactionAttributeType.REQUIRED) public int create(String name, boolean active) throws FxApplicationException { final UserTicket ticket = FxContext.getUserTicket(); final FxEnvironment environment; // Security FxPermissionUtils.checkRole(ticket, Role.GlobalSupervisor); FxSharedUtils.checkParameterEmpty(name, "NAME"); environment = CacheAdmin.getEnvironment(); //exist check for (Mandator m : environment.getMandators(true, true)) if (m.getName().equalsIgnoreCase(name)) throw new FxEntryExistsException("ex.mandator.exists", name); Connection con = null; PreparedStatement ps = null; String sql; try { // Obtain a database connection con = Database.getDbConnection(); // Obtain a new id int newId = (int) seq.getId(FxSystemSequencer.MANDATOR); sql = "INSERT INTO " + TBL_MANDATORS + "(" + //1 2 3 4 5 6 7 8 "ID,NAME,METADATA,IS_ACTIVE,CREATED_BY,CREATED_AT,MODIFIED_BY,MODIFIED_AT)" + "VALUES (?,?,?,?,?,?,?,?)"; final long NOW = System.currentTimeMillis(); ps = con.prepareStatement(sql); ps.setInt(1, newId); ps.setString(2, name.trim()); ps.setNull(3, java.sql.Types.INTEGER); ps.setBoolean(4, active); ps.setLong(5, ticket.getUserId()); ps.setLong(6, NOW); ps.setLong(7, ticket.getUserId()); ps.setLong(8, NOW); ps.executeUpdate(); ps.close(); sql = "INSERT INTO " + TBL_USERGROUPS + " " + "(ID,MANDATOR,AUTOMANDATOR,ISSYSTEM,NAME,COLOR,CREATED_BY,CREATED_AT,MODIFIED_BY,MODIFIED_AT) VALUES (" + "?,?,?,?,?,?,?,?,?,?)"; ps = con.prepareStatement(sql); long gid = seq.getId(FxSystemSequencer.GROUP); ps.setLong(1, gid); ps.setLong(2, newId); ps.setLong(3, newId); ps.setBoolean(4, true); ps.setString(5, "Everyone (" + name.trim() + ")"); ps.setString(6, "#00AA00"); ps.setLong(7, 0); ps.setLong(8, NOW); ps.setLong(9, 0); ps.setLong(10, NOW); ps.executeUpdate(); StructureLoader.addMandator(FxContext.get().getDivisionId(), new Mandator(newId, name.trim(), -1, active, new LifeCycleInfoImpl(ticket.getUserId(), NOW, ticket.getUserId(), NOW))); StructureLoader.updateUserGroups(FxContext.get().getDivisionId(), grp.loadAll(-1)); return newId; } catch (SQLException exc) { final boolean uniqueConstraintViolation = StorageManager.isUniqueConstraintViolation(exc); EJBUtils.rollback(ctx); if (uniqueConstraintViolation) { throw new FxEntryExistsException(LOG, "ex.mandator.exists", name); } else { throw new FxCreateException(LOG, exc, "ex.mandator.createFailed", name, exc.getMessage()); } } finally { Database.closeObjects(MandatorEngineBean.class, con, ps); } }
From source file:de.lsvn.dao.UserDao.java
public void addUser(User user) { try {/* w w w . ja va2 s . c om*/ PreparedStatement preparedStatement = connection.prepareStatement("INSERT into mitglieder(" + "Vorname,Nachname,eMail,Telefon,Handy,TelefonDienstlich,Geburtstag,Mitgliedschaft,Stimmrecht,Jugend,AHK,Sonderstatus,eMailDienstlich,gueltig_von,gueltig_bis) " + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"); // Parameters start with 1 preparedStatement.setString(1, user.getFirstName()); preparedStatement.setString(2, user.getLastName()); preparedStatement.setString(3, user.getEmail()); preparedStatement.setString(4, user.getTelephone()); preparedStatement.setString(5, user.getHandy()); preparedStatement.setString(6, user.getPhoneOffice()); preparedStatement.setString(7, user.getBirthday()); preparedStatement.setString(8, user.getMembership()); preparedStatement.setBoolean(9, user.isVoting()); preparedStatement.setBoolean(10, user.getYouth()); preparedStatement.setBoolean(11, user.isAhk()); preparedStatement.setString(12, user.getSpecialStatus()); preparedStatement.setString(13, user.getEmailOffice()); preparedStatement.setString(14, user.getMedFrom()); preparedStatement.setString(15, user.getMedTo()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:org.schedoscope.export.jdbc.outputformat.JdbcOutputWritable.java
@Override public void write(PreparedStatement ps) throws SQLException { List<Pair<String, String>> record = fromArrayWritable(value); try {// ww w . ja v a 2 s. c om for (int i = 0; i < record.size(); i++) { String type = record.get(i).getLeft().toLowerCase(Locale.getDefault()); if (type.equals(JdbcOutputWritable.STRING)) { if (!record.get(i).getRight().equals("NULL")) { ps.setString(i + 1, record.get(i).getRight()); } else { ps.setNull(i + 1, Types.VARCHAR); } } else if (type.equals(JdbcOutputWritable.DOUBLE)) { if (!record.get(i).getRight().equals("NULL")) { ps.setDouble(i + 1, Double.parseDouble(record.get(i).getRight())); } else { ps.setNull(i + 1, Types.DOUBLE); } } else if (type.equals(JdbcOutputWritable.BOOLEAN)) { if (!record.get(i).getRight().equals("NULL")) { ps.setBoolean(i + 1, Boolean.parseBoolean(record.get(i).getRight())); } else { ps.setNull(i + 1, Types.BOOLEAN); } } else if (type.equals(JdbcOutputWritable.INTEGER)) { if (!record.get(i).getRight().equals("NULL")) { ps.setInt(i + 1, Integer.parseInt(record.get(i).getRight())); } else { ps.setNull(i + 1, Types.INTEGER); } } else if (type.equals(JdbcOutputWritable.LONG)) { if (!record.get(i).getRight().equals("NULL")) { ps.setLong(i + 1, Long.parseLong(record.get(i).getRight())); } else { ps.setNull(i + 1, Types.BIGINT); } } else { LOG.warn("Unknown column type: " + record.get(i).getLeft().toLowerCase(Locale.getDefault())); ps.setString(i + 1, record.get(i).getRight()); } } } catch (NumberFormatException n) { n.printStackTrace(); } }