还是ArrayList对象的问题 50分! 怎么创建的ArrayList??说出来看看 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 怎么创建的ArrayList??说出来看看 public java.util.AbstractList queryByBranch(String branch_id) { ArrayList al=new ArrayList(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { con = DBManager.getConnection(); stmt = con.createStatement(); String temp = " where branch_id='" + branch_id + "'"; sb.append(temp); rs = stmt.executeQuery(sb.toString()); ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); if(rs.next()!=false) { ArrayList al0=new ArrayList(); for (int i = 1; i <= cols; ++i) { if(rs.getString(i)==null) { al0.add("null"); } else { al0.add(rs.getString(i)); } } al.add(al0); } } catch(Exception e) { e.printStackTrace();} finally { try { if(rs!=null) { rs.close(); } } catch(Exception e){} try { if(stmt!=null) { stmt.close(); } } catch(Exception e){} try { if(con!=null) { con.close(); } } catch(Exception e){} } return al;} 你这个方法应该只能得到一条记录吧?你用的if (rs.next())……用while(rs.next()) 你的a1中当然只有一个item了,你的代码中if(rs.next()!=false) { ArrayList al0=new ArrayList(); for (int i = 1; i <= cols; ++i) { if(rs.getString(i)==null) { al0.add("null"); } else { al0.add(rs.getString(i)); } } al.add(al0); }注意你的第一行代码用的是if,而不是while。 楼上各位兄弟讲得很对,记录集需要用循环来取出所有数据的!必须用while! public java.util.AbstractList queryByBranch(String branch_id) { ArrayList al=new ArrayList(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { con = DBManager.getConnection(); stmt = con.createStatement(); String temp = " where branch_id='" + branch_id + "'"; sb.append(temp); rs = stmt.executeQuery(sb.toString()); ResultSetMetaData rsmd = rs.getMetaData(); int cols = rsmd.getColumnCount(); while(rs.next()) { for (int i = 1; i <= cols; ++i) { if(rs.getString(i)==null) { al.add("null"); } else { al.add(rs.getString(i)); } } } } catch(Exception e) { e.printStackTrace();} finally { try { if(rs!=null) { rs.close(); } } catch(Exception e){} try { if(stmt!=null) { stmt.close(); } } catch(Exception e){} try { if(con!=null) { con.close(); } } catch(Exception e){} } return al;}这样写比较好 谁帮我看看这段代码,太感谢啦 用户登陆后,如何把登陆框隐藏,在登陆框的位置把用户信息给显示出来 初学struts,请帮小弟一个忙! 请教一个Struts的问题 在JSP页面读取Sql Server中Text字段 WEB出大问题啦!!!!! 一个简单但是让我无法理解的问题,请教各位! 有没有用UltraEdit开发JSp的朋友? ajax 从后台传值到前台 MQTT TLS连接问题 jsp提问高手请进 请高手帮忙解答关于“登陆”问题,急!!!中午就要结果啊,我是菜鸟,分不是问题
说出来看看
{
ArrayList al=new ArrayList();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
con = DBManager.getConnection();
stmt = con.createStatement();
String temp = " where branch_id='" + branch_id + "'";
sb.append(temp);
rs = stmt.executeQuery(sb.toString());
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
if(rs.next()!=false)
{
ArrayList al0=new ArrayList();
for (int i = 1; i <= cols; ++i)
{
if(rs.getString(i)==null)
{
al0.add("null");
}
else
{
al0.add(rs.getString(i));
}
}
al.add(al0);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();
}
}
catch(Exception e){}
try
{
if(stmt!=null)
{
stmt.close();
}
}
catch(Exception e){}
try
{
if(con!=null)
{
con.close();
}
}
catch(Exception e){}
}
return al;
}
if(rs.next()!=false)
{
ArrayList al0=new ArrayList();
for (int i = 1; i <= cols; ++i)
{
if(rs.getString(i)==null)
{
al0.add("null");
}
else
{
al0.add(rs.getString(i));
}
}
al.add(al0);
}注意你的第一行代码用的是if,而不是while。
必须用while!
{
ArrayList al=new ArrayList();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try
{
con = DBManager.getConnection();
stmt = con.createStatement();
String temp = " where branch_id='" + branch_id + "'";
sb.append(temp);
rs = stmt.executeQuery(sb.toString());
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
while(rs.next())
{
for (int i = 1; i <= cols; ++i)
{
if(rs.getString(i)==null)
{
al.add("null");
}
else
{
al.add(rs.getString(i));
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(rs!=null)
{
rs.close();
}
}
catch(Exception e){}
try
{
if(stmt!=null)
{
stmt.close();
}
}
catch(Exception e){}
try
{
if(con!=null)
{
con.close();
}
}
catch(Exception e){}
}
return al;
}这样写比较好