📄 userdirdb.java
字号:
package com.redmoon.blog;
import cn.js.fan.base.ObjectDb;
import cn.js.fan.db.PrimaryKey;
import cn.js.fan.db.Conn;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import cn.js.fan.web.SkinUtil;
import cn.js.fan.util.ResKeyException;
import java.util.Iterator;
public class UserDirDb extends ObjectDb {
private String userName;
private String code;
public static final String DEFAULT = "default"; // 默认所属的目录名称
public UserDirDb() {
super();
}
public UserDirDb(String userName, String code) {
this.userName = userName;
this.code = code;
init();
load();
}
public static String getDefaultName() {
return "我的文章";
}
/**
* 取得catalogCode目录下所属的用户目录
* @param catalogCode String
* @return Vector
*/
public Vector ListAllUserDir(String catalogCode) {
Vector v = new Vector();
Conn conn = new Conn(connname);
ResultSet rs = null;
String sql = "select userName,code from " + tableName +
" where catalogCode=? order by addDate desc";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, catalogCode);
rs = conn.executePreQuery();
if (rs != null) {
while (rs.next()) {
String userName = rs.getString(1);
String code = rs.getString(2);
v.addElement(getUserDirDb(userName, code));
}
}
} catch (SQLException e) {
logger.error("ListAllUserDir(String catalogCode): " + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return v;
}
/**
* 取得目录下的所有文章数
* @param dirCode String
* @return int
*/
public int getMsgCountOfDir(String userName, String dirCode) {
ResultSet rs = null;
Conn conn = new Conn(connname);
try {
String sql = "select count(*) from sq_message where name=? and isBlog=1 and blogUserDir=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, dirCode);
rs = conn.executePreQuery();
if (rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
logger.error("load:" + e.getMessage());
}
finally {
if (conn!=null) {
conn.close();
conn = null;
}
}
return 0;
}
public void delDirsOfUser(String userName) throws ResKeyException {
Iterator ir = list(userName).iterator();
while (ir.hasNext()) {
UserDirDb udd = (UserDirDb)ir.next();
udd.del();
}
}
public boolean del() throws ResKeyException {
int rowcount = 0;
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement ps = conn.prepareStatement(this.QUERY_DEL);
ps.setString(1, userName);
ps.setString(2, code);
rowcount = conn.executePreUpdate();
UserDirCache uc = new UserDirCache(this);
primaryKey.setKeyValue("userName", userName);
primaryKey.setKeyValue("code", code);
uc.refreshDel(primaryKey);
} catch (SQLException e) {
logger.error("del:" + e.getMessage());
throw new ResKeyException(new SkinUtil(), SkinUtil.ERR_DB);
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return rowcount==1?true:false;
}
public int getObjectCount(String sql) {
return 0;
}
public ObjectDb getObjectRaw(PrimaryKey pk) {
return new UserDirDb(pk.getKeyStrValue("userName"), pk.getKeyStrValue("code"));
}
public boolean create() throws ResKeyException {
int rowcount = 0;
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement ps = conn.prepareStatement(this.QUERY_CREATE);
ps.setString(1, code);
ps.setString(2, dirName);
ps.setString(3, catalogCode);
ps.setString(4, userName);
ps.setString(5, color);
ps.setString(6, "" + System.currentTimeMillis());
rowcount = conn.executePreUpdate();
} catch (SQLException e) {
logger.error("create:" + e.getMessage());
throw new ResKeyException(new SkinUtil(), SkinUtil.ERR_DB);
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return rowcount>0? true:false;
}
public String toOptions(String userName) {
String str = "";
Iterator ir = list(userName).iterator();
while (ir.hasNext()) {
UserDirDb asd = (UserDirDb)ir.next();
str += "<option value='" + asd.getCode() + "'>" + asd.getDirName() + "</option>";
}
return str;
}
public Vector list(String userName) {
Vector v = new Vector();
Conn conn = new Conn(connname);
ResultSet rs = null;
String sql = "select code from " + tableName +
" where userName=? order by sort asc";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, userName);
rs = conn.executePreQuery();
if (rs != null) {
while (rs.next()) {
String code = rs.getString(1);
v.addElement(getUserDirDb(userName, code));
}
}
} catch (SQLException e) {
logger.error("list(String userName): " + e.getMessage());
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return v;
}
public boolean save() throws ResKeyException {
int rowcount = 0;
Conn conn = null;
try {
conn = new Conn(connname);
PreparedStatement ps = conn.prepareStatement(this.QUERY_SAVE);
ps.setString(1, dirName);
ps.setString(2, catalogCode);
ps.setString(3, color);
ps.setString(4, userName);
ps.setString(5, code);
rowcount = conn.executePreUpdate();
UserDirCache uc = new UserDirCache(this);
primaryKey.setKeyValue("userName", userName);
primaryKey.setKeyValue("code", code);
uc.refreshSave(primaryKey);
} catch (SQLException e) {
logger.error("save:" + e.getMessage());
throw new ResKeyException(new SkinUtil(), SkinUtil.ERR_DB);
} finally {
if (conn != null) {
conn.close();
conn = null;
}
}
return rowcount>0? true:false;
}
public UserDirDb getUserDirDb(String userName, String code) {
primaryKey.setKeyValue("userName", userName);
primaryKey.setKeyValue("code", code);
return (UserDirDb)getObjectDb(primaryKey.getKeys());
}
public void load() {
ResultSet rs = null;
Conn conn = new Conn(connname);
try {
PreparedStatement ps = conn.prepareStatement(this.QUERY_LOAD);
primaryKey.setKeyValue("userName", userName);
primaryKey.setKeyValue("code", code);
ps.setString(1, userName);
ps.setString(2, code);
rs = conn.executePreQuery();
if (rs.next()) {
dirName = rs.getString(1);
catalogCode = rs.getString(2);
color = rs.getString(3);
loaded = true;
}
} catch (SQLException e) {
logger.error("load:" + e.getMessage());
}
finally {
if (conn!=null) {
conn.close();
conn = null;
}
}
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setCode(String code) {
this.code = code;
}
public void setDirName(String dirName) {
this.dirName = dirName;
}
public void setCatalogCode(String catalogCode) {
this.catalogCode = catalogCode;
}
public void setColor(String color) {
this.color = color;
}
public String getUserName() {
return userName;
}
public String getCode() {
return code;
}
public String getDirName() {
return dirName;
}
public String getCatalogCode() {
return catalogCode;
}
public String getColor() {
return color;
}
private String dirName;
private String catalogCode = DEFAULT;
private String color = "";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -