|
发表于 2013-8-27 16:04:04
|
显示全部楼层
本帖最后由 hehongbo 于 2013-8-27 17:01 编辑
//数据库中级别需要自己来维护的,数据库增加级别字段且写如下代码:
public static void Sum() throws Exception{
updateRootLevel();//设置根目录的级别
int level=1;
while(hasChildLevel(level)>0)//判断该级别下边是否有子节点
{
updateLevel(level);//逐级设置级别等级
level++;
}
while(level>0)
{
updateNum(level);//参照等级,依次更新它们的数量
level--;
}
}
需要调用的方法如下:
//先获得父为空的数据,等级设置为1
public static void updateRootLevel() throws Exception{
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
try {
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
pstmt = conn.prepareStatement("update PA_FZ set fLevel=1 where fParent is null");
pstmt.execute();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//判断该级别下边是否有子节点
public static int hasChildLevel(int init) throws Exception{
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
ResultSet rs=null;
int count=0;
try {
// 取得数据库连接
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
pstmt = conn.prepareStatement("select count(*) as countsum from PA_FZ b where b.fParent in (select a.fID from PA_FZ a where a.fLevel=?)");
pstmt.setInt(1, init);
rs=pstmt.executeQuery();
rs.next();
count=rs.getInt("countsum");
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
rs.close();
rs=null;
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return count;
}
//3.给子也赋值相应的等级
public static void updateLevel(int init) throws Exception{
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
try {
// 取得数据库连接
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
pstmt = conn.prepareStatement("update PA_FZ b,PA_FZ a set b.fLevel=? where b.fParent = a.fID and a.fLevel=? ");
pstmt.setInt(1, init+1);
pstmt.setInt(2, init);
pstmt.execute();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
//取得数字最大的等级
public static int getMaxLevel() throws Exception{
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
ResultSet rs=null;
int max=0;
try {
// 取得数据库连接
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
pstmt = conn.prepareStatement("select max(fLevel) as tLevel from PA_FZ");
rs=pstmt.executeQuery();
rs.next();
max=rs.getInt("tLevel");
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
rs.close();
rs=null;
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return max;
}
//参照等级,依次更新它们的数量
public static void updateNum(int init) throws Exception{
java.sql.Connection conn = null;
java.sql.PreparedStatement pstmt = null;
try {
// 取得数据库连接
conn = com.justep.model.ModelUtils.getConnectionInTransaction("/ParentSon/ParentSon/data");
pstmt = conn.prepareStatement("update PA_FZ, (select fParent,sum(fNum) as tNum from PA_FZ where fLevel=? group by fParent) b set fNum=b.tNum where b.fParent = PA_FZ.fID ");
pstmt.setInt(1, init);
pstmt.execute();
} catch (NamingException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
|
|