private void createTopPages(Statement statement) throws Exception {
		List<String> pageIds = new LinkedList<String>();
		List<String> parentIds = new LinkedList<String>();
		Map<String, String> topPages = new HashMap<String, String>();
		

		ResultSet rs = statement.executeQuery("SELECT id, parent_id FROM pages");
		while (rs.next()) {
			pageIds.add(rs.getString("id"));
			parentIds.add(rs.getString("parent_id"));
		}
		rs.close();

		String lastPage = "", lastParent = "";
		for (String pageId : pageIds) {
			lastPage = pageId;
			lastParent = parentIds.get(pageIds.indexOf(pageId));
			try {
				//System.out.println(lastPage + "|" + lastParent + "|" + parentIds.get(pageIds.indexOf(lastParent)));
				if (lastPage.equals(lastParent)) {
					topPages.put(pageId, lastParent);
				} else {
					while(!lastPage.equals(lastParent)) {
						//System.out.println(lastPage + "|" + lastParent);
						lastPage = pageIds.get(pageIds.indexOf(lastParent));
						lastParent = parentIds.get(pageIds.indexOf(lastParent));
					}
					topPages.put(pageId, lastParent);
				}
			} catch (Exception e) {}
		}
		
		try {
			statement.execute("ALTER TABLE pages ADD top_id INTEGER");
		} catch (Exception e) {
			// Columns exist.
		}
		
		statement.getConnection().setAutoCommit(false);
		long count = 0;
		PreparedStatement updateQuery = statement.getConnection().prepareStatement(
				"UPDATE pages SET top_id=? WHERE id=?");
		
		for (String id : topPages.keySet()) {
			updateQuery.setString(1, topPages.get(id));
			updateQuery.setString(2, id);
			updateQuery.addBatch();
			
			if (count % 1000 == 0) {
				updateQuery.executeBatch();
				statement.getConnection().commit();
			}
			count++;
		}
		
		updateQuery.executeBatch();
		statement.getConnection().commit();
		updateQuery.close();
		
		statement.getConnection().setAutoCommit(true);
	}