Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String problem

Status
Not open for further replies.

zeero

Programmer
Aug 4, 2004
60
US
I believe this is strictly a Java syntax question, but I'm having a problem displaying a String. It's basically setup like this:

Code:
<html>
<head>

<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb);
Statement stmt = myConn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT petid FROM pettable");
while(rs.next()){
String id = rs.getString("petid");
}
con.close();
%>

</head>
<body>

<%= id %>

</body>
</html>

I'm assuming this is because the string "id" is inside the while statement, but what would be the best way to get it to display in the body tag? Thanks
 
You String instance "id" is out of scope because it is within thw while loop.

Change it like so :

Code:
<html>
<!-- you don;t actually need the <head> tag here as you don't have any html data in it.
<head>
</head>
-->
<body>
<table>
<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb);
Statement stmt = myConn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT petid FROM pettable");
while(rs.next()){
  String id = rs.getString("petid");
  // Add the data in table rows, so it looks prettier ...
  out.println("<tr><td>");
  out.println(id);
  out.println("</td></tr>");
}
con.close();
%>

</table>
</body>
</html>

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks sedj, but let's say the result set could return anywhere between 1 and let's say, 5 results at once. If I were to use out.print(id+" "); it will return them with a space in between each one, however when I used the
<%= id %> it only returned the first one. why is this? isn't the "<%= %>" the same as an out.print?

The reason I wanted to do the <%= %> was because the result needed to be positioned in a particular spot on the page.
 
You can still do what you're mentioning (using <%=%>), but if you wait until AFTER the while-loop to do it, you'll only have the last value returned in the variable 'id.' What it sounds like you're asking to do is print out all the values. For this, you have to put it in the while-loop. You can do this the way sedj did, or you can:

Code:
<%
while(whatever.next())
{
%>
 <%=whatever.get("ID")%>&nbsp;
<%
}//end while
%>

'hope that helps.

--Dave
 
is there a way to take a count of the result sets? I'd like to format the results to have a comma in between them and then an "and" between the last result.
e.g.
blah, blah, blah, and blah.

Im new to Java, but I'm assuming I would use an array?
 
I use resultsetId.getRowCount() when I need it. I've only used JSPs on a JRUN server, however. I don't know if getRowCount() is JRUN-specific. Try it. If it doesn't work, do the query twice. The first time, however, just

select count(*) from myTable where myConditions

THEN do the query for the results. You'll know how many there are from the first query.

'hope that helps.

--Dave
 
If you reed the documentation, getRowCount() is NOT guaranteed to return the number of rows in the ResultSet (yes, it sounds dumb, but its true).

--------------------------------------------------
Free Database Connection Pooling Software
 
One of the best ways I've found to get a row count is the following:

Code:
ResultSet rs;
...Do your query here...
if(rs.last()) { //moves resultset to last row
   rowCount = rs.getRow(); //returns the row number
   rs.first(); //moves resultset back to beginning
}
else {
   rowCount = 0;
}

Using this method allows you to get a row count without having to perform the query twice.

Now you can loop through the resultset and print out your results.

Code:
<%
if (rowCount > 0) {
<%= rs.getString("petId") %>
<%
   for (int i=0; i<rowCount; i++) {
      if (rs.next()) {
%>
      ,&nbsp;<%= rs.getString("petId") %>
<%
      }
   }
   if (rs.next()) {
%>
      &nbsp;and&nbsp;<%= rs.getString("petId"); %>
<%
   }
}
%>
this should give you your "blah, blah, blah and blah"

 
Another option is to include the rowcount in your query.

Instead of:

select count(*) from people_table where lname='Smith'

...followed by:

select home_phone from people_table where lname='Smith'

...you can do this:

select (select count(*) from people_table where lname='Smith'), home_phone from people_table where lname='Smith'

Then the rowcount appears in every row that is returned and all you have to do is pluck it from there.

--Dave

P.S., that's weird about getRowCount(). I actually rely upon it in some of my code. !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top