📄 dbforum.java
字号:
* Updates the modified date but doesn't require a security check since
* it is a protected method.
*/
protected void updateModifiedDate(java.util.Date modifiedDate) {
this.modifiedDate = modifiedDate;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE);
pstmt.setString(1, ""+modifiedDate.getTime());
pstmt.setInt(2, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:updateModifiedDate()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Loads forum properties from the database.
*/
private void loadProperties() {
synchronized(saveLock) {
Properties newProps = new Properties();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String name = rs.getString("name");
String value = rs.getString("propValue");
newProps.put(name, value);
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:loadProperties():" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
this.properties = newProps;
}
}
/**
* Saves forum properties to the database.
*/
private void saveProperties() {
synchronized(saveLock) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
//Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setInt(1, id);
pstmt.execute();
pstmt.close();
//Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Enumeration enum = properties.keys();
while (enum.hasMoreElements()) {
String name = (String)enum.nextElement();
String value = (String)properties.get(name);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
System.err.println(sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
/**
* Loads filters from the database.
*/
private void loadFiltersFromDb() {
ArrayList newFilters = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_FILTERS);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
try {
ObjectInputStream in = new ObjectInputStream(rs.getBinaryStream("filterObject"));
newFilters.add(in.readObject());
}
catch (ClassCastException cce) {
//ignore for now since the filter might be updated. we
//need a solution for this. probably custom class loading
//of filter classes to protect against failure like this.
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
filters = new ForumMessageFilter[newFilters.size()];
for (int i=0; i<filters.length; i++) {
filters[i] = (ForumMessageFilter)newFilters.get(i);
}
//Finally, save filters back to Db. Effectively, this deletes filters
//from the database that failed to load. See note above.
//saveFiltersToDb(); <<-- commenting out to try to fix filters bug.
}
/**
* Saves filters to the database. Filter saving works by serializing
* each filter to a byte stream and then inserting that stream into
* the database.
*/
protected void saveFiltersToDb() {
boolean abort = false;
boolean supportsTransactions = false;
synchronized (saveLock) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
supportsTransactions = con.getMetaData().supportsTransactions();
if (supportsTransactions) {
con.setAutoCommit(false);
}
pstmt = con.prepareStatement(DELETE_FILTERS);
pstmt.setInt(1,id);
pstmt.execute();
//Now insert new list of filters.
pstmt.close();
pstmt = con.prepareStatement(ADD_FILTER);
for (int i=0; i<filters.length; i++) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(filters[i]);
pstmt.setInt(1,id);
pstmt.setInt(2,i);
pstmt.setBytes(3,byteOut.toByteArray());
pstmt.execute();
}
catch (Exception e) {
abort = true;
e.printStackTrace();
}
}
pstmt.close();
}
catch( SQLException sqle ) {
abort = true;
sqle.printStackTrace();
}
finally {
try {
if (supportsTransactions) {
if (abort == true) {
con.rollback();
}
else {
con.commit();
}
}
}
catch (Exception e) { e.printStackTrace(); }
try {
if (supportsTransactions) {
con.setAutoCommit(true);
}
}
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 = DbConnectionManager.getConnection();
//See if we should load by forumID or by name
if (id == -1) {
pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME);
pstmt.setString(1,name);
}
else {
pstmt = con.prepareStatement(LOAD_FORUM_BY_ID);
pstmt.setInt(1, id);
}
ResultSet rs = pstmt.executeQuery();
if( !rs.next() ) {
throw new ForumNotFoundException("Forum " + getID() +
" could not be loaded from the database.");
}
id = rs.getInt("forumID");
name = rs.getString("name");
description = rs.getString("description");
this.creationDate =
new java.util.Date(Long.parseLong(rs.getString("creationDate").trim()));
this.modifiedDate =
new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim()));
moderated = rs.getInt("moderated");
}
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() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_FORUM);
pstmt.setInt(1,id);
pstmt.setString(2,name);
pstmt.setString(3,description);
pstmt.setString(4, Long.toString(creationDate.getTime()));
pstmt.setString(5, Long.toString(modifiedDate.getTime()));
pstmt.setInt(6, moderated);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:insertIntoDb()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves forum data to the database.
*/
private synchronized void saveToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_FORUM);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setString(3, Long.toString(creationDate.getTime()));
pstmt.setString(4, Long.toString(modifiedDate.getTime()));
pstmt.setInt(5, moderated);
pstmt.setInt(6, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:saveToDb()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -