aspose file tools*
The moose likes JSP and the fly likes Why can I display one statement only?! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Why can I display one statement only?!" Watch "Why can I display one statement only?!" New topic
Author

Why can I display one statement only?!

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
I choose any title in mybbs whatever it can display one content only! It's the first result only!? my main.jsp is:
/////////////////main.jsp///////////////////////
String query="select * from qijiashe";
rs=gbook.executeQuery(query);
try{
while(rs.next()){
out.println("<tr><td align=center><font size=2>");
out.println(rs.getString("name"));
out.println("</font></td><td align=center><font size=2><a href=view.jsp?id=");
out.println("target=_blank>");
out.println(rs.getString("title"));
out.println("</a></td><td align=center>");
out.println(rs.getString("time"));
out.println("</a></td></tr>");
//////////////////////////view.jsp////////////////
String sql;
ResultSet rs;
String id="select id from qijiashe where title="+"'title'";
sql="select statement from qijiashe where id="+"(id)";
rs=gbook.executeQuery(sql);
if(rs.next()){
out.println("<table width=750 border=0 bgcolor=#D8E4F8 align=center>");
out.println("<tr><td>");
out.println(rs.getString("statement"));
out.println("</td></tr>");
}
The importance is the parameter "title" can't be sent to the view.jsp!I use session.
can anyone help me?
my msn is liyongqijiashe@hotmail.com


When I face C# and Java I choose Java.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

I'm not sure what you're trying to do here. The way you are concatenating strings in view.jsp makes me suspect that you intended title and id to be variables. However, you enclose them in quotes (""), so they are both literals anyway, and the concatenation is totally unnecessary.
Even if you used the variable id, it is set to another SQL statement. Again, what do you mean to do? title doesn't exist as a variable at all. Do you mean to pass it as a parameter to the page? If so, have something put it in the session, and then have view.jsp read it out of the session. Finally view.jsp is using "if" instead of "while", so the table can only have one row, even if you get the SQL right.
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Thank you very much to reply my question.I meant I want to make a guestbook(BBS).There are "name,title,email,time".When the guest choose the title that he want to reply,the web page switch to the url he wants.There are "main.jsp" and "view.jsp".So I must get to the title as the parameter.I use the "title" parameter to check MS sql server and I use JDBC.But I couldn't get the title parameter in view.jsp even so I use session!
The view.jsp can get the "null" only from main.jsp!
My code:
///////////////main.jsp////////////////////////
<%
String query="select * from qijiashe";
rs=gbook.executeQuery(query);
try{
while(rs.next()){
out.println("<tr><td>");
out.println(rs.getString("name"));
out.println("</font></td><td><a href=view.jsp?id=");
out.println(rs.getString("id"));
out.println(" target=_blank>");
out.println(rs.getString("title"));
out.println("</a></td><td>");
out.println(rs.getString("time"));
out.println("</a></td></tr>");

}
}catch(NumberFormatException e){
out.println("error");
}
%>
<%
String title=rs.getString("title");
session.setAttribute("title",title);
%>
//////////////////////view.jsp//////////////////
<%
String sql;
ResultSet rs;
String title=(String)session.getAttribute("title");
String id="select id from qijiashe where title='"+title+"'";
sql="select statement from qijiashe where id='"+id+"'";
out.println(id);
rs=gbook.executeQuery(sql);
if(rs.next()){
out.println("<table width=750 border=0 bgcolor=#D8E4F8 align=center>");
out.println("<tr><td>");
out.println(rs.getString("statement"));
out.println("</td></tr>");
}
%>
Can you help me? :roll:
Kent Farnsworth
Greenhorn

Joined: May 31, 2000
Posts: 7
Hi I beleive the problem is the following code.
<%
String title=rs.getString("title");
session.setAttribute("title",title);
%>
This code comes after the resultset has been processed. No more rows exit but you are still trying to get more. title would then be null every time and passed to your view.jsp.
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Thank you! I can get the "title" as parameter in "main.jsp" but the "view.jsp" can't get the "title" parameter and it get "null"! I use session.
///////////////////main.jsp//////////////
<%
String query="select * from qijiashe";
rs=gbook.executeQuery(query);
while(rs.next()){
String title=(String)session.getAttribute("title");
out.println(title);
String id="select id from qijiashe where title="+"'title'";
out.println(rs.getInt("id"));
String bid="select id from qijiashe where title='"+title+"'";
out.println(bid);
%>
<tr><td>
<%out.println(rs.getString("name"));%>
</td><td><a href="view.jsp?id=<%=bid%> target=_blank">
<%out.println(rs.getString("title"));%>
</a></td><td>
<%out.println(rs.getString("time"));%>
</td></tr>
<%
}
rs.close();
%>
////////////////////view.jsp/////////////////
<%
String sql;
ResultSet rs;
String title=(String)session.getAttribute("title");
String id="select id from qijiashe where title='"+title+"'";
sql="select statement from qijiashe where id='"+id+"'";
out.println(id);
rs=gbook.executeQuery(sql);
if(rs.next()){
out.println("<table width=750 border=0 bgcolor=#D8E4F8 align=center>");
out.println("<tr><td>");
out.println(rs.getString("statement"));
out.println("</td></tr>");
}
%>
Why??? :roll:
 
jQuery in Action, 2nd edition
 
subject: Why can I display one statement only?!