List of usage examples for java.sql PreparedStatement setInt
void setInt(int parameterIndex, int x) throws SQLException;
int
value. From source file:org.dcache.chimera.H2FsSqlDriver.java
@Override long createTagInode(int uid, int gid, int mode) { final String CREATE_TAG_INODE_WITHOUT_VALUE = "INSERT INTO t_tags_inodes (imode, inlink, iuid, igid, isize, " + "ictime, iatime, imtime, ivalue) VALUES (?,1,?,?,0,?,?,?,NULL)"; Timestamp now = new Timestamp(System.currentTimeMillis()); KeyHolder keyHolder = new GeneratedKeyHolder(); int rc = _jdbc.update(con -> { PreparedStatement ps = con.prepareStatement(CREATE_TAG_INODE_WITHOUT_VALUE, Statement.RETURN_GENERATED_KEYS); ps.setInt(1, mode | UnixPermission.S_IFREG); ps.setInt(2, uid);/*from ww w .j a v a2s .co m*/ ps.setInt(3, gid); ps.setTimestamp(4, now); ps.setTimestamp(5, now); ps.setTimestamp(6, now); return ps; }, keyHolder); if (rc != 1) { throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(CREATE_TAG_INODE_WITHOUT_VALUE, 1, rc); } /* H2 uses weird names for the column with the auto-generated key, so we cannot use the code * in the base class. */ return (Long) keyHolder.getKey(); }
From source file:com.l2jfree.gameserver.instancemanager.CCHManager.java
public final boolean checkIsRegistered(L2Clan clan, int hallid) { if (clan == null) return false; if (clan.getHasHideout() > 0) return true; Connection con = null;//from w ww . jav a2 s . c o m boolean register = false; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con .prepareStatement("SELECT clan_id FROM siege_clans WHERE clan_id=? AND castle_id=?"); statement.setInt(1, clan.getClanId()); statement.setInt(2, hallid); register = statement.executeQuery().next(); statement.close(); } catch (Exception e) { _log.error("Exception: checkIsRegistered(): " + e.getMessage(), e); } finally { L2DatabaseFactory.close(con); } return register; }
From source file:com.l2jfree.gameserver.instancemanager.SiegeGuardManager.java
/** * Load guards defined in <CODE>castle_siege_guards</CODE> if castle is * owned by NPCs.<BR>//from w w w .jav a2 s . c o m * Calls {@link MercTicketManager#buildSpawns(SiegeGuardManager)} if * castle is owned by a player clan. */ private void loadSiegeGuard() { if (getCastle().getOwnerId() > 0) { MercTicketManager.getInstance().buildSpawns(this); return; } Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con.prepareStatement(LOAD_NPC_GUARDS); statement.setInt(1, getCastle().getCastleId()); ResultSet rs = statement.executeQuery(); L2Spawn spawn1; L2NpcTemplate template1; while (rs.next()) { template1 = NpcTable.getInstance().getTemplate(rs.getInt("npcId")); if (template1 != null) { spawn1 = new L2Spawn(template1); spawn1.setId(rs.getInt("id")); spawn1.setAmount(1); spawn1.setLocx(rs.getInt("x")); spawn1.setLocy(rs.getInt("y")); spawn1.setLocz(rs.getInt("z")); spawn1.setHeading(rs.getInt("heading")); spawn1.setRespawnDelay(rs.getInt("respawnDelay")); spawn1.setLocation(0); _siegeGuardSpawn.add(spawn1); } else _log.warn("Missing npc data in npc table for id: " + rs.getInt("npcId")); } statement.close(); } catch (Exception e) { _log.warn("Error loading siege guard for castle " + getCastle().getName(), e); } finally { L2DatabaseFactory.close(con); } }
From source file:Controllers.ReportController.java
public int fetchDepartmentId(int accountId) { int departmentId = 0; try {/*from w w w.j a v a2 s .com*/ Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("jdbc/medicalCareDataSource"); connection = ds.getConnection(); PreparedStatement pstmt = connection .prepareStatement("SELECT * FROM doctors" + " WHERE accountId = ?;"); pstmt.setInt(1, accountId); ResultSet resultSet = pstmt.executeQuery(); List<Doctor> appointmentsList = new ArrayList<Doctor>(); while (resultSet.next()) { departmentId = resultSet.getInt("departmentId"); } pstmt.close(); } catch (NamingException ex) { Logger.getLogger(AppointmentController.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(AppointmentController.class.getName()).log(Level.SEVERE, null, ex); } return departmentId; }
From source file:com.serotonin.mango.db.dao.UserDao.java
private void saveRelationalData(final User user) { // Delete existing permissions. ejt.update("delete from dataSourceUsers where userId=?", new Object[] { user.getId() }); ejt.update("delete from dataPointUsers where userId=?", new Object[] { user.getId() }); // Save the new ones. ejt.batchUpdate("insert into dataSourceUsers (dataSourceId, userId) values (?,?)", new BatchPreparedStatementSetter() { public int getBatchSize() { return user.getDataSourcePermissions().size(); }/* ww w. ja v a 2 s . co m*/ public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, user.getDataSourcePermissions().get(i)); ps.setInt(2, user.getId()); } }); ejt.batchUpdate("insert into dataPointUsers (dataPointId, userId, permission) values (?,?,?)", new BatchPreparedStatementSetter() { public int getBatchSize() { return user.getDataPointPermissions().size(); } public void setValues(PreparedStatement ps, int i) throws SQLException { ps.setInt(1, user.getDataPointPermissions().get(i).getDataPointId()); ps.setInt(2, user.getId()); ps.setInt(3, user.getDataPointPermissions().get(i).getPermission()); } }); }
From source file:org.openmrs.module.openhmis.plm.db.DatabaseListProvider.java
/** * Removes all items from the specified list. * @param list The list to clear.//from ww w. ja va2 s . com */ @Override public void clear(final PersistentList list) { Session session = sessionFactory.getCurrentSession(); try { // Delete all items with list key session.doWork(new Work() { public void execute(Connection connection) { try { PreparedStatement cmd = connection.prepareStatement(CLEAR_SQL); cmd.setInt(1, list.getId()); cmd.executeUpdate(); } catch (SQLException sex) { throw new PersistentListException(sex); } } }); } catch (Exception ex) { throw new PersistentListException("An exception occurred while attempting to get the list items.", ex); } finally { session.close(); } }
From source file:dhbw.clippinggorilla.objects.user.UserUtils.java
/** * Sets sendMail to true/false./*ww w . j av a2 s . c o m*/ * * @param u The User who should be changed * @param sendMail true if sending, false otherwise * @return true if successful */ public static boolean setEmailNewsletter(User u, boolean sendMail) { String sql = "UPDATE " + Tables.USER + " SET " + Columns.SEND_CLIPPING_MAIL + " = ? WHERE " + Columns.ID + " = ?"; try { PreparedStatement statement = Database.getConnection().prepareStatement(sql); if (!sendMail) { statement.setInt(1, 0); } else { statement.setInt(1, 1); } statement.setInt(2, u.getId()); statement.executeUpdate(); } catch (SQLException ex) { Log.warning("Sql failed: setEmailNewsletter", ex); return false; } u.setSendClippingMail(sendMail); return true; }
From source file:com.wwpass.cas.example.JdbcUserServiceDao.java
public int createUser(final String username, final String password) { KeyHolder keyHolder = new GeneratedKeyHolder(); getJdbcTemplate().update(new PreparedStatementCreator() { public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement ps = connection.prepareStatement(CREATE_USER, new String[] { "userid" }); ps.setString(1, username);/*from www .j ava 2s . com*/ ps.setString(2, password); ps.setInt(3, 1); return ps; } }, keyHolder); return keyHolder.getKey().intValue(); }
From source file:fr.aliasource.webmail.common.cache.SignatureCache.java
@Override protected void writeCacheImpl(final List<Signature> data) { getJDBCCacheTemplate().execute(new JDBCCacheCallback() { @Override//from ww w . j a va2 s.c o m public void execute(Connection con, int cacheId) throws SQLException { PreparedStatement delete = null; PreparedStatement insert = null; try { delete = con.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE minig_cache = ?"); delete.setInt(1, cacheId); delete.executeUpdate(); for (Signature entry : data) { insert = con.prepareStatement( "INSERT INTO " + TABLE_NAME + " (email,signature,minig_cache) VALUES (?,?,?)"); insert.setString(1, entry.getEmail()); insert.setString(2, entry.getSignature()); insert.setInt(3, cacheId); logger.info("saving signature for " + entry.getEmail() + "\n" + entry.getSignature()); insert.executeUpdate(); } } finally { JDBCUtils.cleanup(null, delete, null); JDBCUtils.cleanup(null, insert, null); } } }); }
From source file:biblivre3.acquisition.quotation.QuotationDAO.java
public boolean updateQuotation(QuotationDTO dto) { Connection conInsert = null;//from w ww. ja va2 s .c o m try { conInsert = getDataSource().getConnection(); final String sqlInsert = " UPDATE acquisition_quotation " + " SET " + " serial_supplier=?, response_date=?, " + " expiration_date=?, delivery_time=?, " + " responsable=?, obs=? " + " WHERE serial_quotation = ?; "; PreparedStatement pstInsert = conInsert.prepareStatement(sqlInsert); pstInsert.setInt(1, dto.getSerialSupplier()); pstInsert.setDate(2, new java.sql.Date(dto.getResponseDate().getTime())); pstInsert.setDate(3, new java.sql.Date(dto.getExpirationDate().getTime())); pstInsert.setInt(4, dto.getDeliveryTime()); pstInsert.setString(5, dto.getResponsible()); pstInsert.setString(6, dto.getObs()); pstInsert.setInt(7, dto.getSerial()); return pstInsert.executeUpdate() > 0; } catch (Exception e) { log.error(e.getMessage(), e); throw new DAOException(e.getMessage()); } finally { closeConnection(conInsert); } }