📄 userconfigdb.java
字号:
package com.redmoon.blog;
import java.sql.*;
import cn.js.fan.base.ObjectDb;
import cn.js.fan.db.Conn;
import cn.js.fan.db.PrimaryKey;
import cn.js.fan.util.*;
import cn.js.fan.db.SQLFilter;
import java.util.Vector;
import cn.js.fan.db.ListResult;
import com.redmoon.forum.MsgDb;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class UserConfigDb extends ObjectDb {
public UserConfigDb() {
}
public UserConfigDb(String userName) {
this.userName = userName;
init();
load();
}
public boolean create() throws ErrMsgException {
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement ps = conn.prepareStatement(QUERY_CREATE);
// this.QUERY_CREATE =
// "INSERT " + tableName + " (userName,title,subtitle,penName,skin,notice) VALUES (?,?,?,?,?,?)";
ps.setString(1, userName);
ps.setString(2, title);
ps.setString(3, subtitle);
ps.setString(4, penName);
ps.setString(5, skin);
ps.setString(6, notice);
ps.setString(7, kind);
ps.setString(8, "" + System.currentTimeMillis());
if (!(conn.executePreUpdate() == 1 ? true : false))
return false;
UserConfigCache mc = new UserConfigCache(this);
mc.refreshCreate();
} catch (Exception e) {
logger.error("create: " + e.getMessage());
return false;
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return true;
}
/**
* del
*
* @return boolean
* @throws ErrMsgException
* @throws ResKeyException
* @todo Implement this cn.js.fan.base.ObjectDb method
*/
public boolean del() throws ErrMsgException, ResKeyException {
boolean re = false;
// 删除用户的博客贴子
MsgDb md = new MsgDb();
md.delMesssagesOfBlog(userName);
// 删除用户的博客目录
UserDirDb udd = new UserDirDb();
udd.delDirsOfUser(userName);
// 删除用户博客
Conn conn = new Conn(connname);
try {
PreparedStatement ps = conn.prepareStatement(QUERY_DEL);
ps.setString(1, userName);
re = conn.executePreUpdate() == 1 ? true : false;
} catch (Exception e) {
logger.error("del:" + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
if (re) {
UserConfigCache uc = new UserConfigCache(this);
primaryKey.setValue(userName);
uc.refreshDel(primaryKey);
}
return re;
}
public ListResult ListBlog(String listsql, int curPage, int pageSize) throws
ErrMsgException {
int total = 0;
ResultSet rs = null;
Vector result = new Vector();
ListResult lr = new ListResult();
lr.setTotal(total);
lr.setResult(result);
Conn conn = new Conn(connname);
try {
// 取得总记录条数
String countsql = SQLFilter.getCountSql(listsql);
rs = conn.executeQuery(countsql);
if (rs != null && rs.next()) {
total = rs.getInt(1);
}
if (rs != null) {
rs.close();
rs = null;
}
if (total != 0)
conn.setMaxRows(curPage * pageSize); // 尽量减少内存的使用
rs = conn.executeQuery(listsql);
if (rs == null) {
return lr;
} else {
rs.setFetchSize(pageSize);
int absoluteLocation = pageSize * (curPage - 1) + 1;
if (rs.absolute(absoluteLocation) == false) {
return lr;
}
do {
UserConfigDb ug = getUserConfigDb(rs.getString(1));
result.addElement(ug);
} while (rs.next());
}
} catch (SQLException e) {
logger.error(e.getMessage());
throw new ErrMsgException("数据库出错!");
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
lr.setResult(result);
lr.setTotal(total);
return lr;
}
/**
*
* @param pk Object
* @return Object
* @todo Implement this cn.js.fan.base.ObjectDb method
*/
public ObjectDb getObjectRaw(PrimaryKey pk) {
return new UserConfigDb(pk.getStrValue());
}
public UserConfigDb getUserConfigDb(String userName) {
return (UserConfigDb)getObjectDb(userName);
}
/**
* load
*
* @throws ErrMsgException
* @throws ResKeyException
* @todo Implement this cn.js.fan.base.ObjectDb method
*/
public void load() {
Conn conn = new Conn(connname);
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(this.QUERY_LOAD);
pstmt.setString(1, userName);
rs = conn.executePreQuery();
if (rs.next()) {
// this.QUERY_LOAD =
// "SELECT title,subtitle,penName,skin,notice FROM " + tableName + " WHERE userName=?";
title = rs.getString(1);
subtitle = rs.getString(2);
penName = rs.getString(3);
skin = rs.getString(4);
notice = rs.getString(5);
addDate = DateUtil.parse(rs.getString(6));
// 用下句会丢失小时分钟信息
// addDate = rs.getDate(6);
valid = rs.getInt(7)==1?true:false;
viewCount = rs.getInt(8);
msgCount= rs.getInt(9);
replyCount = rs.getInt(10);
kind = rs.getString(11);
loaded = true;
primaryKey.setValue(userName);
}
} catch (SQLException e) {
logger.error("load:" + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
}
/**
* save
*
* @return boolean
* @throws ErrMsgException
* @throws ResKeyException
* @todo Implement this cn.js.fan.base.ObjectDb method
*/
public boolean save() throws ResKeyException {
boolean re = false;
Conn conn = new Conn(connname);
try {
PreparedStatement ps = conn.prepareStatement(QUERY_SAVE);
ps.setString(1, title);
ps.setString(2, subtitle);
ps.setString(3, penName);
ps.setString(4, skin);
ps.setString(5, notice);
ps.setInt(6, valid?1:0);
ps.setInt(7, viewCount);
ps.setInt(8, msgCount);
ps.setInt(9, replyCount);
ps.setString(10, kind);
ps.setString(11, userName);
re = conn.executePreUpdate() == 1 ? true : false;
} catch (Exception e) {
logger.error("save:" + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
if (re) {
UserConfigCache uc = new UserConfigCache(this);
primaryKey.setValue(userName);
uc.refreshSave(primaryKey);
}
return re;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setTitle(String title) {
this.title = title;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public void setPenName(String penName) {
this.penName = penName;
}
public void setSkin(String skin) {
this.skin = skin;
}
public void setNotice(String notice) {
this.notice = notice;
}
public void setValid(boolean valid) {
this.valid = valid;
}
public void setViewCount(int viewCount) {
this.viewCount = viewCount;
}
public void setMsgCount(int msgCount) {
this.msgCount = msgCount;
}
public void setReplyCount(int replyCount) {
this.replyCount = replyCount;
}
public void setKind(String kind) {
this.kind = kind;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public String getUserName() {
return userName;
}
public String getTitle() {
return title;
}
public String getSubtitle() {
return subtitle;
}
public String getPenName() {
return penName;
}
public String getSkin() {
return skin;
}
public String getNotice() {
return notice;
}
public java.util.Date getAddDate() {
return addDate;
}
public boolean isValid() {
return valid;
}
public int getViewCount() {
return viewCount;
}
public int getMsgCount() {
return msgCount;
}
public int getReplyCount() {
return replyCount;
}
public String getKind() {
return kind;
}
public void initDB() {
this.tableName = "blog_user_config";
primaryKey = new PrimaryKey("userName", PrimaryKey.TYPE_STRING);
objectCache = new UserConfigCache(this);
this.QUERY_DEL =
"delete FROM " + tableName + " WHERE userName=?";
this.QUERY_CREATE =
"INSERT into " + tableName + " (userName,title,subtitle,penName,skin,notice,kind,addDate) VALUES (?,?,?,?,?,?,?,?)";
this.QUERY_LOAD =
"SELECT title,subtitle,penName,skin,notice,addDate,isValid,viewCount,msgCount,replyCount,kind FROM " + tableName + " WHERE userName=?";
this.QUERY_SAVE =
"UPDATE " + tableName +
" SET title=?,subtitle=?,penName=?,skin=?,notice=?,isValid=?,viewCount=?,msgCount=?,replyCount=?,kind=? WHERE userName=?";
this.QUERY_LIST = "select userName from " + tableName + " order by addDate desc";
isInitFromConfigDB = false;
}
private String userName;
private String title;
private String subtitle;
private String penName;
private String skin;
private String notice;
private java.util.Date addDate;
private boolean valid = true;
private int viewCount = 0;
private int msgCount = 0;
private int replyCount = 0;
private String kind;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -