List of usage examples for java.sql PreparedStatement setBoolean
void setBoolean(int parameterIndex, boolean x) throws SQLException;
boolean
value. From source file:com.alfaariss.oa.engine.requestor.jdbc.JDBCFactory.java
/** * @see com.alfaariss.oa.engine.core.requestor.factory.IRequestorPoolFactory#getAllEnabledRequestorPools() *//*w w w.j a v a2 s . c o m*/ @Override public Collection<RequestorPool> getAllEnabledRequestorPools() throws RequestorException { Collection<RequestorPool> collPools = new Vector<RequestorPool>(); Connection oConnection = null; PreparedStatement oPreparedStatement = null; ResultSet oResultSet = null; try { oConnection = _oDataSource.getConnection(); oPreparedStatement = oConnection.prepareStatement(_sQuerySelectAllEnabledRequestorpools); oPreparedStatement.setBoolean(1, true); oResultSet = oPreparedStatement.executeQuery(); while (oResultSet.next()) { JDBCRequestorPool oRequestorPool = new JDBCRequestorPool(oResultSet, _oDataSource, _sPoolsTable, _sRequestorsTable, _sRequestorPropertiesTable, _sAuthenticationTable, _sPoolPropertiesTable); if (oRequestorPool != null) collPools.add(oRequestorPool); } } catch (SQLException e) { _logger.error("Can not read enabled requestorpools from database", e); throw new RequestorException(SystemErrors.ERROR_RESOURCE_RETRIEVE); } catch (RequestorException e) { throw e; } catch (Exception e) { _logger.fatal("Internal error during retrieving all enabled requestorpools", e); throw new RequestorException(SystemErrors.ERROR_INTERNAL); } finally { try { if (oResultSet != null) oResultSet.close(); } catch (Exception e) { _logger.error("Could not close resultset", e); } try { if (oPreparedStatement != null) oPreparedStatement.close(); } catch (Exception e) { _logger.error("Could not close statement", e); } try { if (oConnection != null) oConnection.close(); } catch (Exception e) { _logger.error("Could not close connection", e); } } return Collections.unmodifiableCollection(collPools); }
From source file:de.lsvn.dao.UserDao.java
public void updateUser(User user) { connection = DbUtil.getConnection(); try {//from www.j a va 2s.co m PreparedStatement preparedStatement = connection.prepareStatement( "UPDATE medicals JOIN (mitglieder JOIN adressen ON mitglieder.AdresseId = adressen.AId) ON mitglieder.Id = medicals.MId SET " + "Vorname=?,Nachname=?,Postleitzahl=?,Ort=?,eMail=?,Telefon=?,Handy=?,TelefonDienstlich=?,Geburtstag=?,Mitgliedschaft=?,Stimmrecht=?,Scheinpilot=?,Jugend=?,AHK=?,Sonderstatus=?,eMailDienstlich=?,gueltig_von=?,gueltig_bis=?,Strasse=? " + "WHERE Id=?"); preparedStatement.setString(1, user.getFirstName()); preparedStatement.setString(2, user.getLastName()); preparedStatement.setString(3, user.getPlz()); preparedStatement.setString(4, user.getCity()); preparedStatement.setString(5, user.getEmail()); preparedStatement.setString(6, user.getTelephone()); preparedStatement.setString(7, user.getHandy()); preparedStatement.setString(8, user.getPhoneOffice()); preparedStatement.setString(9, user.getBirthday()); preparedStatement.setString(10, user.getMembership()); preparedStatement.setBoolean(11, user.isVoting()); preparedStatement.setBoolean(12, user.getLicense()); preparedStatement.setBoolean(13, user.getYouth()); preparedStatement.setBoolean(14, user.isAhk()); preparedStatement.setString(15, user.getSpecialStatus()); preparedStatement.setString(16, user.getEmailOffice()); preparedStatement.setString(17, user.getMedFrom()); preparedStatement.setString(18, user.getMedTo()); preparedStatement.setString(19, user.getStreet()); preparedStatement.setInt(20, user.getUserid()); preparedStatement.executeUpdate(); DbUtil.closeStatement(preparedStatement); } catch (SQLException e) { e.printStackTrace(); } finally { DbUtil.closeConnection(connection); } }
From source file:com.mkmeier.quickerbooks.LoadAccounts.java
private void saveAccounts(List<QbAccount> accounts) { String host = "jdbc:mysql://mysql.mdmeier.com"; String db = "quickerbooks"; String user = "quickerbooks"; String password = "quickerbooks"; Connection conn = null;/* w w w . j av a 2 s.c om*/ PreparedStatement stmt = null; try { conn = DatabaseConnection.getDatabaseConnection(host, db, user, password); //Clear table of existing data String sql = "TRUNCATE TABLE account"; stmt = conn.prepareStatement(sql); stmt.executeQuery(); sql = "INSERT INTO account (" + "name, " + "ref_num, " + "timestamp, " + "acct_type, " + "oba_amount, " + "description, " + "acct_num, " + "scd, " + "bank_num, " + "extra, " + "hidden, " + "del_count, " + "use_id) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); for (QbAccount acct : accounts) { stmt.setString(1, acct.getName()); stmt.setInt(2, acct.getRefNum()); stmt.setLong(3, acct.getTimeStamp()); stmt.setString(4, acct.getAcctType()); stmt.setBigDecimal(5, acct.getObaAmount()); stmt.setString(6, acct.getDescription()); stmt.setInt(7, acct.getAcctNum()); stmt.setInt(8, acct.getScd()); stmt.setString(9, acct.getBankNum()); stmt.setString(10, acct.getExtra()); stmt.setBoolean(11, acct.isHidden()); stmt.setInt(12, acct.getDelCount()); stmt.setBoolean(13, acct.isUseId()); stmt.execute(); } } catch (SQLException ex) { logger.error(ex); } finally { // Clean up connection. try { stmt.close(); } catch (SQLException ex) { ex.printStackTrace(); } try { conn.close(); } catch (SQLException ex) { ex.printStackTrace(); } } }
From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java
private void setArgument(PreparedStatement pstmt, String argument, int targetSqlType, int index) throws SQLException { switch (targetSqlType) { case Types.INTEGER: pstmt.setInt(index, Integer.parseInt(argument)); break;// www. j a va 2s .c o m case Types.DECIMAL: case Types.NUMERIC: pstmt.setBigDecimal(index, new BigDecimal(argument)); break; case Types.DOUBLE: case Types.FLOAT: pstmt.setDouble(index, Double.parseDouble(argument)); break; case Types.CHAR: case Types.LONGVARCHAR: case Types.VARCHAR: pstmt.setString(index, argument); break; case Types.BIT: case Types.BOOLEAN: pstmt.setBoolean(index, Boolean.parseBoolean(argument)); break; case Types.BIGINT: pstmt.setLong(index, Long.parseLong(argument)); break; case Types.DATE: pstmt.setDate(index, Date.valueOf(argument)); break; case Types.REAL: pstmt.setFloat(index, Float.parseFloat(argument)); break; case Types.TINYINT: pstmt.setByte(index, Byte.parseByte(argument)); break; case Types.SMALLINT: pstmt.setShort(index, Short.parseShort(argument)); break; case Types.TIMESTAMP: pstmt.setTimestamp(index, Timestamp.valueOf(argument)); break; case Types.TIME: pstmt.setTime(index, Time.valueOf(argument)); break; case Types.BINARY: case Types.VARBINARY: case Types.LONGVARBINARY: pstmt.setBytes(index, argument.getBytes()); break; case Types.NULL: pstmt.setNull(index, targetSqlType); break; default: pstmt.setObject(index, argument, targetSqlType); } }
From source file:com.alfaariss.oa.engine.requestor.jdbc.JDBCFactory.java
/** * @see com.alfaariss.oa.engine.core.requestor.factory.IRequestorPoolFactory#getAllEnabledRequestors() */// ww w. j a va2 s . c o m @Override public Collection<IRequestor> getAllEnabledRequestors() throws RequestorException { Collection<IRequestor> collRequestors = new Vector<IRequestor>(); Connection oConnection = null; PreparedStatement oPreparedStatement = null; ResultSet rsRequestor = null; ResultSet rsProperties = null; try { oConnection = _oDataSource.getConnection(); oPreparedStatement = oConnection.prepareStatement(_sQuerySelectAllEnabledRequestors); oPreparedStatement.setBoolean(1, true); rsRequestor = oPreparedStatement.executeQuery(); while (rsRequestor.next()) { oPreparedStatement = oConnection.prepareStatement(_sQuerySelectRequestorProperties); oPreparedStatement.setString(1, rsRequestor.getString(JDBCRequestor.COLUMN_ID)); rsProperties = oPreparedStatement.executeQuery(); JDBCRequestor oJDBCRequestor = new JDBCRequestor(rsRequestor, rsProperties); IRequestor oRequestor = oJDBCRequestor.getRequestor(); if (oRequestor != null) collRequestors.add(oRequestor); rsProperties.close(); _logger.debug("Retrieved requestor: " + oRequestor); } } catch (SQLException e) { _logger.error("Can not read all enabled requestors from database", e); throw new RequestorException(SystemErrors.ERROR_RESOURCE_RETRIEVE); } catch (RequestorException e) { throw e; } catch (Exception e) { _logger.fatal("Internal error during retrieval of all enabled requestors", e); throw new RequestorException(SystemErrors.ERROR_INTERNAL); } finally { try { if (rsRequestor != null) rsRequestor.close(); } catch (Exception e) { _logger.error("Could not close requestor resultset", e); } try { if (rsProperties != null) rsProperties.close(); } catch (Exception e) { _logger.error("Could not close requestor properties resultset", e); } try { if (oPreparedStatement != null) oPreparedStatement.close(); } catch (Exception e) { _logger.error("Could not close statement", e); } try { if (oConnection != null) oConnection.close(); } catch (Exception e) { _logger.error("Could not close connection", e); } } return Collections.unmodifiableCollection(collRequestors); }
From source file:dk.statsbiblioteket.doms.licensemodule.persistence.LicenseModuleStorage.java
public void persistDomLicenseGroupType(String key, String value, String value_en, String description, String description_en, String query, boolean mustGroup) throws Exception { if (!StringUtils.isNotEmpty(key)) { throw new IllegalArgumentException("Key must not be null when creating new Group"); }/*from w w w .j a v a 2s .c o m*/ if (!StringUtils.isNotEmpty(value)) { throw new IllegalArgumentException("Value must not be null when creating new Group"); } if (!StringUtils.isNotEmpty(value_en)) { throw new IllegalArgumentException("Value(EN) must not be null when creating new Group"); } if (!StringUtils.isNotEmpty(query)) { throw new IllegalArgumentException("Query must not be null when creating new Group"); } log.info("Persisting new dom license group type: " + key); validateValue(value); value = value.trim(); PreparedStatement stmt = null; try { stmt = connection.prepareStatement(persistDomLicenseGroupTypeQuery); stmt.setLong(1, generateUniqueID()); stmt.setString(2, key); stmt.setString(3, value); stmt.setString(4, value_en); stmt.setString(5, description); stmt.setString(6, description_en); stmt.setString(7, query); stmt.setBoolean(8, mustGroup); stmt.execute(); connection.commit(); } catch (SQLException e) { log.error("SQL Exception in persistDomLicenseGroupType:" + e.getMessage()); throw e; } finally { closeStatement(stmt); } LicenseCache.reloadCache(); // Force reload so the change will be instant in the cache }
From source file:dk.statsbiblioteket.doms.licensemodule.persistence.LicenseModuleStorage.java
public void updateDomLicenseGroupType(long id, String value_dk, String value_en, String description, String description_en, String query, boolean mustGroup) throws Exception { PreparedStatement stmt = null; try {/*from ww w . j av a 2 s . com*/ log.info("Updating Group type with id:" + id); // if it exists already, we do not add it. stmt = connection.prepareStatement(updateDomLicenseGroupTypeQuery); stmt.setString(1, value_dk); stmt.setString(2, value_en); stmt.setString(3, description); stmt.setString(4, description_en); stmt.setString(5, query); stmt.setBoolean(6, mustGroup); stmt.setLong(7, id); int updated = stmt.executeUpdate(); if (updated != 1) { throw new SQLException("Grouptype id not found:" + id); } connection.commit(); } catch (Exception e) { log.error("Exception in updateDomLicenseGroupType:" + e.getMessage()); throw e; } finally { closeStatement(stmt); } LicenseCache.reloadCache(); // Force reload so the change will be instant in the cache }
From source file:info.raack.appliancelabeler.data.JDBCDatabase.java
public void addUserAppliance(final EnergyMonitor energyMonitor, final UserAppliance userAppliance) { logger.debug("Inserting new user appliance " + userAppliance + " for energy monitor " + energyMonitor); // look for energy monitor id int id = energyMonitor.getId(); if (id <= 0) { id = getIdForEnergyMonitor(energyMonitor); energyMonitor.setId(id);/* w w w . jav a2 s . co m*/ } KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection .prepareStatement(userAppliance.getAlgorithmId() > 0 ? insertForNewUserApplianceForAlgorithm : insertForNewUserAppliance, new String[] { "id" }); ps.setInt(1, energyMonitor.getId()); ps.setLong(2, userAppliance.getAppliance().getId()); ps.setString(3, userAppliance.getName()); int next = 4; if (userAppliance.getAlgorithmId() > 0) { ps.setInt(next++, userAppliance.getAlgorithmId()); } ps.setBoolean(next++, userAppliance.isAlgorithmGenerated()); return ps; } }, keyHolder); userAppliance.setId(keyHolder.getKey().intValue()); }
From source file:gov.nih.nci.cabig.caaers.datamigrator.UserDataMigrator.java
/** * This method inserts appropriate records into study personnel table based on existing role_code. * @param map//from ww w .j av a 2s . c om * @param groups */ @SuppressWarnings("unchecked") protected void insertIntoStudyPersonnel(final Map map, final List groups, final boolean onOracleDB) { String sql = getInsertStudyPersonnelSql(onOracleDB); BatchPreparedStatementSetter setter = null; setter = new BatchPreparedStatementSetter() { public int getBatchSize() { return groups.size(); } public void setValues(PreparedStatement ps, int index) throws SQLException { java.sql.Timestamp startDate = (java.sql.Timestamp) map.get("start_date"); java.sql.Timestamp endDate = (java.sql.Timestamp) map.get("end_date"); if (onOracleDB) { BigDecimal studySiteId = (BigDecimal) map.get("study_sites_id"); ps.setBigDecimal(1, studySiteId); } else { int studySiteId = ((Integer) map.get("study_sites_id")).intValue(); ps.setInt(1, studySiteId); } ps.setString(2, groups.get(index).toString()); if (onOracleDB) { BigDecimal retiredIndicator = (BigDecimal) map.get("retired_indicator"); ps.setBigDecimal(3, retiredIndicator); } else { Boolean retiredIndicator = (Boolean) map.get("retired_indicator"); ps.setBoolean(3, retiredIndicator); } ps.setTimestamp(4, startDate); ps.setTimestamp(5, endDate); if (onOracleDB) { BigDecimal siteResearchStaffId = (BigDecimal) map.get("site_research_staffs_id"); ps.setBigDecimal(6, siteResearchStaffId); } else { int siteResearchStaffId = ((Integer) map.get("site_research_staffs_id")).intValue(); ps.setInt(6, siteResearchStaffId); } } }; getJdbcTemplate().batchUpdate(sql, setter); }
From source file:com.draagon.meta.manager.db.ObjectManagerDB.java
protected PreparedStatement getPreparedStatement(Connection c, String query, Collection<?> args) throws MetaException, SQLException { String sql = query; // (String) mOQLCache.get( query ); // If it's not in the cache, then parse it and put it there //if ( sql == null ) //{/*from w w w. ja va 2s .co m*/ //Map<String,MetaClass> m = getMetaClassMap( query ); //if ( m.size() > 0 ) { // sql = convertToSQL( query, m ); //} //else sql = query; //mOQLCache.put( query, sql ); //} PreparedStatement s = c.prepareStatement(sql); if (args != null) { int i = 1; for (Object o : args) { if (o instanceof Boolean) { s.setBoolean(i, (Boolean) o); } else if (o instanceof Byte) { s.setByte(i, (Byte) o); } else if (o instanceof Short) { s.setShort(i, (Short) o); } else if (o instanceof Integer) { s.setInt(i, (Integer) o); } else if (o instanceof Long) { s.setLong(i, (Long) o); } else if (o instanceof Float) { s.setFloat(i, (Float) o); } else if (o instanceof Double) { s.setDouble(i, (Double) o); } else if (o instanceof Date) { s.setTimestamp(i, new Timestamp(((Date) o).getTime())); } else if (o == null) { s.setString(i, null); } else { s.setString(i, o.toString()); } // Increment the i i++; } } return s; }