请高手帮小弟我看看为什么这段出现运行起来会很慢

请高手帮我看看为什么这段出现运行起来会很慢
public static void getAllId() {

DBConnection dbc = null ;
PreparedStatement ps = null;
ResultSet rs = null;
String csql = "select name from huiyuan";//获取不法会员姓名
String lsql = "select name from ls";//所有律师会员id

try {
//会员
dbc = new DBConnection();
ps = dbc.getConnection().prepareStatement(csql);//执行语句获取所有普通会员id
rs = ps.executeQuery();
List allI=new ArrayList();
while(rs.next()){
String i = rs.getString("name");
allI.add(i);
}
for(int a=0;a<allI.size();a++){
String name=(String) allI.get(a);
String huiyuanid = "select auto_id from member where content_type=1 and content_user='"+name+"'";
dbc = new DBConnection();
ps = dbc.getConnection().prepareStatement(huiyuanid);
rs = ps.executeQuery();
while(rs.next()){
int memberId= rs.getInt(1);
commonlist.add(memberId);
  System.out.println(commonlist.size());
  
}
}  
rs.close();
ps.close();


//律师
ps = dbc.getConnection().prepareStatement(lsql);
rs = ps.executeQuery();
List allName=new ArrayList();
while(rs.next()){
String name=rs.getString("name");
allName.add(name);
}
for(int i=0;i<allName.size();i++){
String name=(String) allName.get(i);
String huiyuanid = "select auto_id from member where content_type=2 and content_user='"+name+"'";
System.out.println(huiyuanid);
dbc = new DBConnection();
ps = dbc.getConnection().prepareStatement(huiyuanid);
rs = ps.executeQuery();
while(rs.next()){
int memberId= rs.getInt(1);
lawyerlist.add(memberId);  
System.out.println(lawyerlist.size());
}
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
dbc.close();
}
}
我链接本地数据库的时候虽然速度快一些:但是我也明显的看得出等待的时间 System.out.println(commonlist.size());
打印在控制台时比如打印到24会停止一小会,就是有点卡那种,呵呵.但是链接本地数据库还算可以忍受吧。我链接服务器数据库(网站上线了)运行到这里就慢了 网速好的时候要2分钟,慢的时候要很久很久无法忍受的程度!
我的另一个程序,也链接服务器的同一时间内 那个程序运行起来还是蛮快的
所以我就想:一方面是网速。第二方面是程序有问题。程序应该是主要的问题。

补充下 这段程序的大体业务:
  数据库有两张表 会员和律师。我是想根据这两张表中的律师和会员名去另一种表中查询出对对应的律师或者会员的id 然后放到一个集合中。以便下面的程序来操作这个集合!
 

------解决方案--------------------
为什么反复看到dbc.getConnection()

就一次不行么?!
------解决方案--------------------
这段程序,快的话就不正常了。。。

不知道你的getConnection中怎么实现的。。总之只需要取一次连接就行了。。
最后也没看见你关闭connection。。

查询的时候没有条件。。查询所有记录本身没有分页,况且在一个循环能做好的。你用了两次循环。。无故多循环了一次。。。。

------解决方案--------------------
select auto_id from member where content_type=1 and content_user in(select name from huiyuan)
一个sql就可以查出 自然不用多个循环
Connection 只用一次创建即可
------解决方案--------------------
select auto_id,content_type from member where content_type in (1,2) and (content_user in (select name from huiyuan) or content_user in (select name from ls))
一次查询就可以得到你需要的所有数据,然后判断一下content_type进行分类
只访问一次数据库就可以实现了
------解决方案--------------------
楼主的代码有很严重的问题 楼上的已经说的很清楚了 建议楼主网上下载一些代码结构比较好的jdbc连接数据库代码