List of usage examples for java.sql PreparedStatement setBoolean
void setBoolean(int parameterIndex, boolean x) throws SQLException;
boolean
value. From source file:com.flexive.core.storage.MySQL.MySQLTreeStorage.java
/** * {@inheritDoc}//from w ww . ja v a2 s.c om */ @Override public List<String> getLabels(Connection con, FxTreeMode mode, long labelPropertyId, FxLanguage language, boolean stripNodeInfos, long... nodeIds) throws FxApplicationException { List<String> ret = new ArrayList<String>(nodeIds.length); if (nodeIds.length == 0) return ret; PreparedStatement ps = null; ResultSet rs; try { ps = con.prepareStatement("SELECT tree_FTEXT1024_Chain(?,?,?,?)"); ps.setInt(2, (int) language.getId()); ps.setLong(3, labelPropertyId); ps.setBoolean(4, mode == FxTreeMode.Live); for (long id : nodeIds) { ps.setLong(1, id); try { rs = ps.executeQuery(); if (rs != null && rs.next()) { final String path = rs.getString(1); if (!StringUtils.isEmpty(path)) ret.add(stripNodeInfos ? stripNodeInfos(path) : path); else addUnknownNodeId(ret, id); } else addUnknownNodeId(ret, id); } catch (SQLException e) { if ("22001".equals(e.getSQLState())) { //invalid node id in MySQL addUnknownNodeId(ret, id); } else throw e; } } return ret; } catch (SQLException e) { throw new FxLoadException(LOG, e, "ex.db.sqlError", e.getMessage()); } finally { try { if (ps != null) ps.close(); } catch (Exception e) { //ignore } } }
From source file:org.georchestra.mapfishapp.addons.notes.NoteBackend.java
/** * Store note in database configured in this backend. * @param note note to store in this backend *///from w ww . ja va2s . co m public void store(Note note) throws SQLException { Connection connection = null; PreparedStatement st = null; // try { connection = this.basicDataSource.getConnection(); st = connection.prepareStatement("INSERT INTO " + this.table + "(followup, email, comment, map_context, login, the_geom) VALUES (?,?,?,?,?,ST_SetSRID(ST_MakePoint(?,?),?))"); st.setBoolean(1, note.getFollowUp()); st.setString(2, note.getEmail()); st.setString(3, note.getComment()); st.setString(4, note.getMapContext()); st.setString(5, note.getLogin()); st.setDouble(6, note.getLongitude()); st.setDouble(7, note.getLatitude()); st.setInt(8, this.srid); st.executeUpdate(); // } // catch (SQLException e) { // throw new RuntimeException(e); // } finally { // if (st != null) try { st.close(); } catch (SQLException e) {LOG.error(e);} // if (connection != null) try { connection.close(); } catch (SQLException e) {LOG.error(e);} // } }
From source file:dk.netarkivet.common.utils.DBUtils.java
/** * Prepare a statement given a query string and some args. * * NB: the provided connection is not closed. * * @param c a Database connection/*from w ww . j av a2 s . c o m*/ * @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, String query, Object... args) throws SQLException { ArgumentNotValid.checkNotNull(c, "Connection c"); ArgumentNotValid.checkNotNullOrEmpty(query, "String query"); ArgumentNotValid.checkNotNull(args, "Object... args"); PreparedStatement s = c.prepareStatement(query); 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.concursive.connect.web.modules.documents.dao.FileItemList.java
public static void convertTempFiles(Connection db, int linkModuleId, int modifiedBy, int id, String attachmentList, boolean setDefault) throws SQLException { StringTokenizer items = new StringTokenizer(attachmentList, ","); PreparedStatement pst = db.prepareStatement("UPDATE project_files " + "SET link_module_id = ?, link_item_id = ? " + (setDefault ? ", default_file = ? " : "") + "WHERE link_module_id = ? AND item_id = ? AND enteredby = ? "); while (items.hasMoreTokens()) { int itemId = Integer.parseInt(items.nextToken().trim()); int i = 0; pst.setInt(++i, linkModuleId);/*ww w. ja v a2s .c om*/ pst.setInt(++i, id); if (setDefault) { pst.setBoolean(++i, true); } pst.setInt(++i, Constants.TEMP_FILES); pst.setInt(++i, itemId); pst.setInt(++i, modifiedBy); int count = pst.executeUpdate(); } pst.close(); }
From source file:com.onclave.testbench.jdbcTemplate.DAOSupport.StudentDAOImplementation.java
@Override public boolean createNewStudent(final StudentPOJO student) { this.jdbcTemplate.update(new PreparedStatementCreator() { @Override//from w w w .j a v a 2 s . c o m public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { PreparedStatement preparedStatement = connection .prepareStatement(QueryStatements.INSERT_STUDENT_SQL); preparedStatement.setString(1, student.getName()); preparedStatement.setString(2, student.getRollNo()); preparedStatement.setString(3, student.getEmail()); preparedStatement.setBoolean(4, student.isActive()); preparedStatement.setInt(5, student.getAge()); return preparedStatement; } }); return true; }
From source file:com.flexive.core.storage.genericSQL.GenericBinarySQLOutputStream.java
/** * {@inheritDoc}//from w w w .j a v a2 s .com */ @Override public void close() throws IOException { super.close(); try { rcvThread.join(); } catch (InterruptedException e) { LOG.error("Receiving thread got interrupted: " + e.getMessage(), e); } PreparedStatement ps = null; Connection con = null; try { con = Database.getNonTXDataSource(divisionId).getConnection(); ps = con.prepareStatement( "UPDATE " + DatabaseConst.TBL_BINARY_TRANSIT + " SET TFER_DONE=?, BLOBSIZE=? WHERE BKEY=?"); ps.setBoolean(1, true); ps.setLong(2, count); ps.setString(3, handle); if (ps.executeUpdate() != 1) LOG.error("Failed to update binary transit for handle " + handle); } catch (SQLException e) { LOG.error("SQL error marking binary as finished: " + e.getMessage(), e); } finally { Database.closeObjects(GenericBinarySQLOutputStream.class, con, ps); } }
From source file:fll.web.playoff.JsonBracketDataTests.java
private void verifyScore(final Connection connection, final int team, final int run) throws SQLException { PreparedStatement ps = null; try {// w w w . j ava 2 s . c o m ps = connection .prepareStatement("UPDATE Performance SET Verified = ? WHERE TeamNumber = ? AND RunNumber = ?"); ps.setBoolean(1, true); ps.setInt(2, team); ps.setInt(3, run); Assert.assertEquals(1, ps.executeUpdate()); } finally { SQLFunctions.close(ps); } }
From source file:dk.netarkivet.harvester.datamodel.GlobalCrawlerTrapListDBDAO.java
/** * Returns a list of either all active or all inactive trap lists. * @param isActive whether to return active or inactive lists. * @return a list if global crawler trap lists. *//*from w w w . ja v a 2s. c o m*/ private List<GlobalCrawlerTrapList> getAllByActivity(boolean isActive) { List<GlobalCrawlerTrapList> result = new ArrayList<GlobalCrawlerTrapList>(); Connection conn = HarvestDBConnection.get(); PreparedStatement stmt = null; try { stmt = conn.prepareStatement(SELECT_BY_ACTIVITY); stmt.setBoolean(1, isActive); ResultSet rs = stmt.executeQuery(); while (rs.next()) { result.add(read(rs.getInt(1))); } return result; } catch (SQLException e) { String message = "Error reading trap list\n" + ExceptionUtils.getSQLExceptionCause(e); log.warn(message, e); throw new UnknownID(message, e); } finally { DBUtils.closeStatementIfOpen(stmt); HarvestDBConnection.release(conn); } }
From source file:org.schedoscope.metascope.tasks.repository.mysql.impl.FieldEntityMySQLRepository.java
@Override public void insertOrUpdate(Connection connection, FieldEntity fieldEntity) { String insertFieldSql = "insert into field_entity (" + JDBCUtil.getDatabaseColumnsForClass(FieldEntity.class) + ") values (" + JDBCUtil.getValuesCountForClass(FieldEntity.class) + ") " + "on duplicate key update " + MySQLUtil.getOnDuplicateKeyString(FieldEntity.class); PreparedStatement stmt = null; try {/* w w w . j a va2 s . c o m*/ stmt = connection.prepareStatement(insertFieldSql); stmt.setString(1, fieldEntity.getFqdn()); stmt.setString(2, fieldEntity.getName()); stmt.setString(3, fieldEntity.getType()); stmt.setString(4, fieldEntity.getDescription()); stmt.setInt(5, fieldEntity.getFieldOrder()); stmt.setBoolean(6, fieldEntity.isParameterField()); stmt.setString(7, fieldEntity.getFqdn()); stmt.execute(); } catch (SQLException e) { LOG.error("Could not save field", e); } finally { DbUtils.closeQuietly(stmt); } }
From source file:org.eclipse.ecr.core.storage.sql.extensions.H2Functions.java
public static ResultSet upgradeVersions(Connection conn) throws SQLException { PreparedStatement ps1 = null; PreparedStatement ps2 = null; try {// ww w .ja v a2 s. co m String sql = "SELECT v.id, v.versionableid, h.majorversion, h.minorversion" + " FROM versions v JOIN hierarchy h ON v.id = h.id" + " ORDER BY v.versionableid, v.created DESC"; ps1 = conn.prepareStatement(sql); ResultSet rs = ps1.executeQuery(); String series = null; boolean isLatest = false; boolean isLatestMajor = false; while (rs.next()) { String id = rs.getString("id"); String vid = rs.getString("versionableid"); long maj = rs.getLong("majorversion"); long min = rs.getLong("minorversion"); if (vid == null || !vid.equals(series)) { // restart isLatest = true; isLatestMajor = true; series = vid; } boolean isMajor = min == 0; ps2 = conn.prepareStatement( "UPDATE versions SET label = ?, islatest = ?, islatestmajor = ?" + " WHERE id = ?"); ps2.setString(1, maj + "." + min); ps2.setBoolean(2, isLatest); ps2.setBoolean(3, isMajor && isLatestMajor); ps2.setString(4, id); ps2.executeUpdate(); // next isLatest = false; if (isMajor) { isLatestMajor = false; } } } finally { if (ps1 != null) { ps1.close(); } if (ps2 != null) { ps2.close(); } } return new SimpleResultSet(); }