一个很奇怪的sql查询语句
<%
try
{
ResultSet rs=dbc.executeQuery( "select dashangpin from dbo.dashangpin ");//执行Sql语句
rs.last();//将指针移动到最后一个
int r=rs.getRow();//获得数据库的行数
rs.absolute(1);//数据库的指针回滚到索引为1的地方;
//rs.beforeFirst();
Vector vt=new Vector();//构造一个容器来存放数据
for(int i=0;i <r;i++)
{
String gs=rs.getString( "dashangpin ");
vt.addElement(gs);//将数据加入到容器的末尾,然后加1;
//out.print(vt.elementAt(i));
%>
<div class= "style1 "> * <%=rs.getString( "dashangpin ")%> </div> <br/>
<%
if(!rs.next()) break;
}
session.setAttribute( "vtor ",vt);
}
catch(Exception e)
{
out.println(e.getMessage());
}
try
{
Vector vtor1=new Vector();
vtor1=(Vector)session.getAttribute( "vtor ");//传递一个对象
String st=(String)vtor1.elementAt(0).toString();
//out.println(vtor1.size());
//out.print(vtor1.elementAt(0));
}
catch(
ArrayIndexOutOfBoundsException e)
{
out.println(e.getMessage());
}
ResultSet rs1=dbc.executeQuery( "select * from dbo.xiaoshangpin where dashangpin= ' "+st2+ " ' ");
while(rs1.next())
{
%>
<div class= "style1 "> * <%=rs1.getString( "xiaoshangpin ")%> </div> <br/>
<%}%>
编译一下:出现的问题如下:
C:\Tomcat 4.1\work\Standalone\localhost\srt\prictise_jsp.java:116: cannot resolve symbol
symbol : variable st2
location: class org.apache.jsp.prictise_jsp
ResultSet rs1=dbc.executeQuery( "select * from dbo.xiaoshangpin where dashangpin= "+st2);
------解决方案--------------------錯誤信息都給你了,還不清楚嗎,沒有st2這個變量啊
------解决方案--------------------st2-> st
------解决方案--------------------st2改成st,另外你的习惯不好,
/*结果集变量定义应该放在try块之外,并在finally块中做关闭处理*/
ResultSet rs=dbc.executeQuery( "select dashangpin from dbo.dashangpin ");//执行Sql语句
/*使用结果集的循环去得数据更合理而且简单,为何使用for那?
Vector v = new Vector();
while(rs.next()){
String str = rs.getString( "dashangpin ");
v.addElement(str);
out.println( " <div class= 'style1 '> * "+str+ " </div> ");
///注意:结果集操作是很耗资源的,同样的数据一次取出来存到临时变量里供后面使用,不要用一次就取一次。
}
session.setAttribute( "vtor ",v);
*/
rs.last();//将指针移动到最后一个 <