dbforum.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,659 行 · 第 1/5 页
JAVA
1,659 行
Connection con = null; Statement stmt = null; try { con = ConnectionManager.getConnection(); stmt = ConnectionManager.createScrollableStatement(con); // Set the maximum number of rows to end at the end of this block. ConnectionManager.setMaxRows(stmt, BLOCK_SIZE * (blockID+1)); ResultSet rs = stmt.executeQuery(query); // Grab BLOCK_SIZE rows at a time. ConnectionManager.setFetchSize(rs, BLOCK_SIZE); // Position the cursor right before the first row that we're insterested in. ConnectionManager.scrollResultSet(rs, blockStart); // Keep reading results until the result set is exhausted or // we come to the end of the block. int count = 0; while (rs.next() && count < BLOCK_SIZE) { objectList.add(rs.getLong(1)); count++; } } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } objectIDs = objectList.toArray(); // Add the block to cache factory.cacheManager.queryCache.put(key, objectIDs); addQueryKey(key); /** * The actual block may be smaller than BLOCK_SIZE. If * that's the case, it means two things: * 1) We're at the end boundary of all the results. * 2) If the start index is greater than the length of the current * block, than there aren't really any results to return. */ if (startIndex >= blockStart + objectIDs.length) { // Return an empty array return EMPTY_BLOCK; } else { return objectIDs; } } } } /** * Returns the count associated with a ResultFilter query. * * @param key the key associted with the query. */ private int getCount(QueryCacheKey key) { // Synchronize on the sql query so that only one thread can try to load the // count at once. This should help to prevent the database from // becoming overloaded on busy sites. StringBuffer internKey = new StringBuffer(key.getSql().length()+4); internKey.append(key.getSql()).append(key.getBlockID()); synchronized (internKey.toString().intern()) { Integer count = (Integer)factory.cacheManager.queryCache.get(key); // Check in short-term cache if not in the long-term cache. if (count == null && factory.cacheManager.isShortTermQueryCacheEnabled()) { count = (Integer)factory.cacheManager.shortTermQueryCache.get(key); } // If already in cache, return the count. if (count != null) { return count.intValue(); } // Otherwise, we have to load the count from the db. else { Connection con = null; Statement stmt = null; try { con = ConnectionManager.getConnection(); stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(key.getSql()); rs.next(); count = new Integer(rs.getInt(1)); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { stmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } // Add the thread count to cache factory.cacheManager.queryCache.put(key, count); addQueryKey(key); return count.intValue(); } } } /** * Adds a key to the queryKey list. The method ensures that the list never * grows larger than fifty elements by removing keys from the end of the * list. This doesn't necessarily ensure proper caching behavior, but the * queryKeys list will only grow large in rare circumstances, such as when * statistics are being run. * * @param key the key to add to the queryKey list. */ protected void addQueryKey(QueryCacheKey key) { synchronized (queryKeys) { queryKeys.add(key); if (queryKeys.size() > 50) { factory.cacheManager.queryCache.remove(queryKeys.removeLast()); } } // Re-add forum to cache. factory.cacheManager.forumCache.put(new Long(id), this); } /** * Loads properties from the database. */ private synchronized void loadPropertiesFromDb() { this.properties = new Hashtable(); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); while(rs.next()) { String name = rs.getString(1); String value = rs.getString(2); properties.put(name, value); } } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Inserts a new property into the datatabase. */ private void insertPropertyIntoDb(String name, String value) { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(INSERT_PROPERTY); pstmt.setLong(1, id); pstmt.setString(2, name); pstmt.setString(3, value); pstmt.executeUpdate(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Updates a property value in the database. */ private void updatePropertyInDb(String name, String value) { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(UPDATE_PROPERTY); pstmt.setString(1, value); pstmt.setString(2, name); pstmt.setLong(3, id); pstmt.executeUpdate(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Deletes a property from the db. */ private synchronized void deletePropertyFromDb(String name) { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(DELETE_PROPERTY); pstmt.setLong(1, id); pstmt.setString(2, name); pstmt.execute(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Loads forum data from the database. */ private void loadFromDb() throws ForumNotFoundException { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(LOAD_FORUM); pstmt.setLong(1,id); ResultSet rs = pstmt.executeQuery(); if( !rs.next() ) { throw new ForumNotFoundException("Forum " + getID() + " could not be loaded from the database."); } this.id = rs.getLong(1); this.name = rs.getString(2); this.description = rs.getString(3); this.modDefaultThreadValue = rs.getInt(4); this.modDefaultMessageValue = rs.getInt(5); this.modMinThreadValue = rs.getInt(6); this.modMinMessageValue = rs.getInt(7); this.creationDate = new java.util.Date(Long.parseLong(rs.getString(8).trim())); this.modifiedDate = new java.util.Date(Long.parseLong(rs.getString(9).trim())); this.categoryID = rs.getLong(10); // Now, load any extended forum properties if (!LAZY_PROP_LOADING) { pstmt.close(); properties = new Hashtable(); pstmt = con.prepareStatement(LOAD_PROPERTIES); pstmt.setLong(1, id); rs = pstmt.executeQuery(); while(rs.next()) { String name = rs.getString(1); String value = rs.getString(2); properties.put(name, value); } } } catch( SQLException sqle ) { sqle.printStackTrace(); throw new ForumNotFoundException("Forum " + getID() + " could not be loaded from the database."); } catch (NumberFormatException nfe) { System.err.println("WARNING: In DbForum.loadFromDb() -- there " + "was an error parsing the dates returned from the database. Ensure " + "that they're being stored correctly."); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Inserts a new record into the database. */ private void insertIntoDb(int categoryIndex) { Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = ConnectionManager.getTransactionConnection(); pstmt = con.prepareStatement(ADD_FORUM); pstmt.setLong(1,id); pstmt.setLong(2, categoryID); pstmt.setInt(3, categoryIndex); pstmt.setString(4, name); pstmt.setString(5, description); pstmt.setInt(6, modDefaultThreadValue); pstmt.setInt(7, modDefaultMessageValue); pstmt.setInt(8, modMinThreadValue); pstmt.setInt(9, modMinMessageValue); pstmt.setString(10, StringUtils.dateToMillis(creationDate)); pstmt.setString(11, StringUtils.dateToMillis(modifiedDate)); pstmt.executeUpdate(); } catch( SQLException sqle ) { sqle.printStackTrace(); abortTransaction = true; } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } ConnectionManager.closeTransactionConnection(con, abortTransaction); } } /** * Saves forum data to the database. */ private synchronized void saveToDb() { Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(SAVE_FORUM); pstmt.setString(1, name); pstmt.setString(2, description); pstmt.setInt(3, modDefaultThreadValue); pstmt.setInt(4, modDefaultMessageValue);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?