• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can I display one statement only?!

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic