List of usage examples for java.sql PreparedStatement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:com.flexive.core.storage.PostgreSQL.PostgreSQLHierarchicalStorage.java
/** * {@inheritDoc}// w ww. j ava 2s. c o m */ @Override public void lockTables(Connection con, long id, int version) throws FxRuntimeException { try { PreparedStatement ps = null; try { String ver = version <= 0 ? "" : " AND VER=?"; ps = con.prepareStatement( "SELECT * FROM " + DatabaseConst.TBL_CONTENT + " WHERE ID=?" + ver + " FOR UPDATE"); ps.setLong(1, id); if (version > 0) ps.setInt(2, version); ps.executeQuery(); ps.close(); ps = con.prepareStatement( "SELECT * FROM " + DatabaseConst.TBL_CONTENT_DATA + " WHERE ID=?" + ver + " FOR UPDATE"); ps.setLong(1, id); if (version > 0) ps.setInt(2, version); ps.executeQuery(); ps.close(); /*ps = con.prepareStatement("SELECT * FROM " + DatabaseConst.TBL_CONTENT_BINARY + " WHERE ID=?" + ver + " FOR UPDATE"); ps.setLong(1, id); if (version > 0) ps.setInt(2, version); ps.executeQuery();*/ //fulltext table uses MyISAM engine and can not be locked } finally { if (ps != null) ps.close(); } if (LOG.isDebugEnabled()) LOG.debug("Locked instances of id #" + id + (version > 0 ? " and version #" + version : " (all versions)")); } catch (SQLException e) { throw new FxDbException(LOG, e, "ex.db.sqlError", e.getMessage()).asRuntimeException(); } }
From source file:net.sf.l2j.gameserver.instancemanager.CoupleManager.java
private final void load() { java.sql.Connection con = null; try {//from w w w.j av a 2 s . c o m PreparedStatement statement; ResultSet rs; con = L2DatabaseFactory.getInstance().getConnection(); statement = con.prepareStatement("Select id from couples order by id"); rs = statement.executeQuery(); while (rs.next()) { getCouples().add(new Couple(rs.getInt("id"))); } statement.close(); _log.info("Loaded: " + getCouples().size() + " couples(s)"); } catch (Exception e) { _log.error("Exception: CoupleManager.load(): " + e.getMessage(), e); } finally { try { con.close(); } catch (Exception e) { } } }
From source file:com.mechanicshop.service.SearchServiceImpl.java
@Override public void updatePassword(String password) { byte[] encodedBytes = Base64.encode(password.getBytes()); String encodedPassword = new String(encodedBytes); String sql = "UPDATE parameters SET AdminPassword = ?"; Connection conn = null;//from w ww . j a v a 2 s . c o m try { conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, encodedPassword); ps.executeUpdate(); ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } }
From source file:com.nabla.wapp.server.json.SqlToJson.java
public FetchResult serializeSql(final AbstractFetch options, final Connection conn, final String sql, Object... parameters) throws SQLException { final PreparedStatement stmt = StatementFormat.prepare(conn, createSql(options, sql), parameters); try {/*from ww w . j a v a 2 s.c o m*/ int i = (parameters != null) ? parameters.length : 0; if (options.isRange()) { stmt.setInt(++i, options.getEndRow() - options.getStartRow() + 1); stmt.setInt(++i, options.getStartRow()); } return serialize(options, stmt); } finally { stmt.close(); } }
From source file:com.l2jfree.gameserver.instancemanager.CoupleManager.java
public void deleteCouple(int coupleId) { int index = getCoupleIndex(coupleId); Couple couple = getCouples().get(index); if (couple != null) { L2Player player1 = L2World.getInstance().getPlayer(couple.getPlayer1Id()); L2Player player2 = L2World.getInstance().getPlayer(couple.getPlayer2Id()); L2ItemInstance item = null;/*w ww . j a va 2s .co m*/ if (player1 != null) { player1.setPartnerId(0); player1.setMaried(false); player1.setCoupleId(0); item = player1.getInventory().getItemByItemId(9140); if (player1.isOnline() == 1 && item != null) { player1.destroyItem("Removing Cupids Bow", item, player1, true); // No need to update every item in the inventory //player1.getInventory().updateDatabase(); } if (player1.isOnline() == 0 && item != null) { Integer PlayerId = player1.getObjectId(); Integer ItemId = 9140; Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con .prepareStatement("DELETE FROM items WHERE owner_id = ? AND item_id = ?"); statement.setInt(1, PlayerId); statement.setInt(2, ItemId); statement.execute(); statement.close(); } catch (Exception e) { e.printStackTrace(); } finally { L2DatabaseFactory.close(con); } } } if (player2 != null) { player2.setPartnerId(0); player2.setMaried(false); player2.setCoupleId(0); item = player2.getInventory().getItemByItemId(9140); if (player2.isOnline() == 1 && item != null) { player2.destroyItem("Removing Cupids Bow", item, player2, true); // No need to update every item in the inventory //player2.getInventory().updateDatabase(); } if (player2.isOnline() == 0 && item != null) { Integer Player2Id = player2.getObjectId(); Integer Item2Id = 9140; Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con .prepareStatement("DELETE FROM items WHERE owner_id = ? AND item_id = ?"); statement.setInt(1, Player2Id); statement.setInt(2, Item2Id); statement.execute(); statement.close(); } catch (Exception e) { e.printStackTrace(); } finally { L2DatabaseFactory.close(con); } } } couple.divorce(); getCouples().remove(index); } }
From source file:com.l2jfree.gameserver.communitybbs.bb.Post.java
/** * @param i// ww w . jav a 2s .co m */ public void updatetxt(int i) { Connection con = null; try { CPost cp = getCPost(i); con = L2DatabaseFactory.getInstance().getConnection(con); PreparedStatement statement = con.prepareStatement( "UPDATE posts SET post_txt=? WHERE post_id=? AND post_topic_id=? AND post_forum_id=?"); statement.setString(1, cp.postTxt); statement.setInt(2, cp.postId); statement.setInt(3, cp.postTopicId); statement.setInt(4, cp.postForumId); statement.execute(); statement.close(); } catch (Exception e) { _log.warn("error while saving new Post to db ", e); } finally { L2DatabaseFactory.close(con); } }
From source file:net.sf.l2j.gameserver.handler.AutoAnnouncementHandler.java
/** * Removes and cancels ALL auto announcement for the given announcement id. * * @param int Id/* w w w . ja va2s . c o m*/ * @return boolean removedSuccessfully */ public boolean removeAnnouncement(int id) { AutoAnnouncementInstance announcementInst = _registeredAnnouncements.get(id); java.sql.Connection con = null; try { con = L2DatabaseFactory.getInstance().getConnection(); PreparedStatement statement = con.prepareStatement("DELETE FROM auto_announcements WHERE id=?"); statement.setInt(1, announcementInst.getDefaultId()); statement.executeUpdate(); statement.close(); } catch (Exception e) { _log.fatal("Could not Delete Auto Announcement in Database, Reason:", e); } finally { try { con.close(); } catch (Exception e) { } } return removeAnnouncement(announcementInst); }
From source file:com.dbsvg.models.SQLiteInternalDataDAO.java
@Override public void removeViewsForAllTables(SchemaPage page, Connection conn) throws SQLException { String insertTableViewSQL = "DELETE FROM table_page_position WHERE pageid=?;"; PreparedStatement ps = conn.prepareStatement(insertTableViewSQL); ps.setString(1, page.getId().toString()); ps.executeUpdate();// ww w. java 2s .c om ps.close(); page.getTableViews().clear(); LOG.info("DELETED All TableViews for page {}", page.getId()); }
From source file:com.cloudera.sqoop.manager.NetezzaExportManualTest.java
protected void createTableNZ(String tableName, ColumnGenerator... extraCols) throws SQLException { String sqlStatement = getDropTableStatement(tableName); conn.rollback();/*from www . j ava2 s .c o m*/ LOG.info("Executing drop statement : " + sqlStatement); PreparedStatement statement = conn.prepareStatement(sqlStatement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); try { statement.executeUpdate(); conn.commit(); } catch (SQLException sqle) { conn.rollback(); } finally { statement.close(); } StringBuilder sb = new StringBuilder(); sb.append("CREATE TABLE "); sb.append(tableName); sb.append(" (id INT NOT NULL PRIMARY KEY, msg VARCHAR(64)"); int colNum = 0; for (ColumnGenerator gen : extraCols) { sb.append(", " + forIdx(colNum++) + " " + gen.getType()); } sb.append(")"); sqlStatement = sb.toString(); LOG.info("Executing create statement : " + sqlStatement); statement = conn.prepareStatement(sqlStatement, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); try { statement.executeUpdate(); conn.commit(); } finally { statement.close(); } }
From source file:hermes.store.schema.DefaultJDBCAdapter.java
public void insert(Connection connection, String storeId, Message message) throws SQLException, JMSException { final String destinationName = message.getJMSDestination() == null ? "default" : JMSUtils.getDestinationName(message.getJMSDestination()); final Domain domain = message.getJMSDestination() == null ? Domain.QUEUE : Domain.getDomain(message.getJMSDestination()); final String messageAsXMLString = xmlHelper.toXML(message); final InputStream messageAsXML = new StringInputStream(messageAsXMLString); final String messageId = getNextMessageId(storeId); ///*from ww w.ja v a 2 s.co m*/ // DBUtils does not seem to correctly deal with CLOBS, so we have to use // normal JDBC... // // runner.update(connection, "insert into messages values (?, ?)", new // Object[] { message.getJMSMessageID(), messageAsXML }); final PreparedStatement pstmt = connection.prepareStatement("insert into messages values (?, ?)"); pstmt.setString(1, messageId); pstmt.setAsciiStream(2, messageAsXML, messageAsXMLString.length()); pstmt.execute(); pstmt.close(); final QueryRunner runner = new QueryRunner(); runner.update(connection, "insert into stores values (?, ?, ?, ?)", new Object[] { storeId, destinationName, domain.getId(), messageId }); }