解决了,马上给...关于数组的问题...(恼我3天的问题啊!)

jenry_leon 2004-09-27 09:36:09
<table width=100% border=2>
<logic:iterate id="element" name="models_picnames">
<table>
<bean:write name="element" property="key"/>
<logic:iterate id="elementvalue" name="element" property="value">
<tr>
<td>
<bean:write name="elementvalue"/>
</td>
</tr>
</logic:iterate>
</table>
</logic:iterate>
</table>
上面这是jsp页面....

import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.youcompany.oraclebeans.dbcontrol;

public class IndexpictureAction extends Action {
private int number,numberj;
private String models_picnames[][];
private String models[];
private ResultSet rs;
private ResultSet rs1;
private String sql;
private String sql1;
private int i,j;

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HashMap h=new HashMap();

i=0;
sql="select distinct(MODELS) from TJB";
rs=db.executeQuery(sql);
while(rs.next())
{
i++;
}
number=i;
models=new String[number];
models_picnames=new String[number][];

i=0;
sql="select distinct(MODELS) from TJB";
rs=db.executeQuery(sql);
while(rs.next())
{
j=0;
models[i]=rs.getString("MODELS");

sql1="select distinct(PICNAMES) from TJB where MODELS='"+models[i]+"'";
rs1=db1.executeQuery(sql1);
while(rs1.next())
{
j++;
}
numberj=j;
models_picnames=new String[i][numberj];

j=0;
sql1="select distinct(PICNAMES) from TJB where MODELS='"+models[i]+"'";
rs1=db1.executeQuery(sql1);
while(rs1.next())
{
models_picnames[i][j]=rs1.getString("PICNAMES");
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!出错!提示:值=0
j++;
}
h.put("models_picnames"+i,models_picnames[i]);
i++;
}
request.setAttribute("models_picnames",h);
return mapping.findForward("indexsuccess");
}
}
我在运行时,提示 上面注释的地方值为0.
请高手帮我解决啊!....
这问题恼我3天了!
...全文
106 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
禽兽v5 2004-09-29
问题解决了?
回复
禽兽v5 2004-09-28
发送:
request.setAttribute("models_picnames", modelPicnameList);

接收:
List list = (List)request.getAttribute("models_picnames");

for ( int i = 0; i < list.size(); i++ )
{
// 组织你要的html字符串
}
回复
jenry_leon 2004-09-28
大哥,再帮个忙好吗?
我没用过list
请帮我写个例子好吗?
我的要求是
MODELS1
PICNAMES11
PICNAMES12
....
MODELS2
PICNAMES21
PICNAMES22
....
请帮我好吗?
谢谢,我可以加分!
我急需!
回复
zhonghua2003 2004-09-28
为了文静,努力工作!!帮你顶!!
回复
禽兽v5 2004-09-28
import部分这样写,就可以使用List了。

import java.util.*;

我写的那部分应该够用。其中List的方法,查api文档就可以了。
回复
jenry_leon 2004-09-27
我要在jsp 页面中显示!
应该怎么做啊!
回复
禽兽v5 2004-09-27
sorry,不够仔细,第2个for循环内部的while循环结束后,要加rs.close();
回复
禽兽v5 2004-09-27
sql语句没加别名吧。但是代码也该改:

sql="select distinct(MODELS) from TJB";
rs=db.executeQuery(sql);
while(rs.next())
{
i++;
}
number=i;
models=new String[number];
......
----------
好浪费,先遍历一次,生成数组,又遍历一次获得内容。应该这样:

List modelList = new ArrayList();
List modelPicnameList = new ArrayList();

sql = "select distinct(MODELS) MODELS from TJB"; // 注意加了别名 MODELS
rs = db.executeQuery(sql);
while(rs.next())
{
modelList.add(rs.getString());
}

rs.close(); //

for ( int i = 0; i < modelList.size(); i++ )
{
sql = "select distinct(PICNAMES) PICNAMES from TJB where MODELS='" + (String)modelList.get(i) + "'"; // 加了别名 PICNAMES

rs = db.executeQuery(sql);
while(rs.next())
{
modelPicnameList.add(rs.getString("PICNAMES"));
}
}

// 打印值
for ( int i = 0; i < modelList.size(); i++ )
{
System.out.println("model[" + i + "] = " + (String)modelList.get(i));
System.out.println("model_picname[" + i + "] = " + (String)modelPicnameList.get(i));
}
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告