package com.cfd.drp.client;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import com.cfd.drp.utils.Utils;public class ClientManager { StringBuffer sb = new StringBuffer();
private static ClientManager manager = new ClientManager();
private ClientManager() {}
public static ClientManager getInstance() {
return manager;
}
public String showTree() {
Connection connection = null;
sb.delete(0, sb.length());
try {
connection = Utils.getConnection();
tree(connection,0,1);
}catch(Exception e) {
e.printStackTrace();
}finally {
Utils.close(connection);
}
return sb.toString();
}
public void tree(Connection connection,int id,int level) throws SQLException {
String sql = "select * from t_client where pid = ?";
int i= 0;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
sb.append(rs.getString("name")+"<br>"+"\n");
if(rs.getString("is_leaf").equals("N")) {
tree(connection,rs.getInt("id"),level);
}
}
}finally {
Utils.slose(rs);
Utils.close(ps);
}
}
}
这个问题是尚学谈DRP项目中的:
正常运行的结果应该是:
所有分销商
华北区
北京
北京市医药股份有限公司
东北区
吉林省
辽宁省
黑龙江省
下面是jsp页面的内容,这样也页面的内容和简单 就是把上面的字符串显示出来就是了: <%
ClientManager manager = ClientManager.getInstance();
out.print(manager.showTree());
%>但是当我快速刷新页面的时候却就出现问题了。结果变为了,【只有快速刷新的时候才会出现】
所有分销商
华北区
北京
北京市医药股份有限公司
东北区
吉林省
辽宁省
黑龙江省
所有分销商
华北区
北京
北京市医药股份有限公司
东北区
吉林省
辽宁省
黑龙江省 刷新的速度不一样 字符串重复的次数就不一样 请高手指教!谢谢了
1 如下的代码只有一份,属于实例级的属性
StringBuffer sb = new StringBuffer();2 private static ClientManager manager = new ClientManager();
private ClientManager() {}
public static ClientManager getInstance() {
return manager;
}所有的调用都将使用同一份实例3 所有的线程,使用了同一个StringBuffer,当然出问题了4 修改方式就是,将 StringBuffer放到局部变量里面,比如
public String showTree() {
StringBuffer sb = new StringBuffer(); // 这里声明局部变量
Connection connection = null;
try {
connection = Utils.getConnection();
tree(connection,0,1,sb);// 这里作为参数传过去
}catch(Exception e) {
e.printStackTrace();
}finally {
Utils.close(connection);
}
return sb.toString();
}
public void tree(Connection connection,int id,int level,StringBuffer sb) throws SQLException { // 这里使用传过来的局部变量
String sql = "select * from t_client where pid = ?";
int i= 0;
我对你有点印象 估计是回答其他人的问题的时候我也看过吧 呵呵
谢谢了