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!

Bean with database outputs only one record 2

Status
Not open for further replies.

evergreean43

Technical User
May 25, 2006
165
US
Earlier this week I posted a JSP Bean that showed null result. I got it working to now it will show a database value but now I want it to show all the records.

In the \Tomcat 5.5\logs it shows all the records but the JSP displays only 1 record which seems to be the last record value.

Please advise what I am doing wrong.

JSP:
Code:
<jsp:useBean id="tester" class="colors.Tester"/>
<jsp:setProperty name="tester"  property="lastName" value="*" />
 
<jsp:getProperty name="tester" property="lastName" />

Java class file:
Code:
package colors;
import java.io.*;
import java.sql.*;
import java.util.*;  

public class Tester
{   
   protected String lastName; 
   String mquery = "";

public Tester(){}  

public String getLastName()
{   
this.viewDatabaseTable();   
return lastName;
} 

public void setLastName(String aLastName)
{   
lastName = aLastName;
} 

public void viewDatabaseTable()
{ 
    exeQry(mquery);
}  

public void exeQry(String mquery)	
{					
try		
{				
Class.forName("org.gjt.mm.mysql.Driver");    
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/myfirst?user=root&password=aaaaaa");    
Statement stmt = conn.createStatement();    
ResultSet results = stmt.executeQuery("SELECT * from user");			
while(results.next())				
{						
lastName = results.getString("lastName");	
System.out.println(lastName);	
}		
}			
catch(Exception e)			
{				
System.out.println("Exception here.");					
}			
}
}

Output from JSP is:
Jones


The \Tomcat 5.5\logs output shows all the records which is what I want the JSP to show:
Edwardson
Smith
Carson
Jones
 
You iterate through the result set setting 'lastName' to the current result set's value - so you just set the 'lastName' value 4 times - the last of which is "Jones".

You need to to something more like :

lastName += resultSet.get("lastName") +",";

or something similar to build up the strings.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks,

It works and now outputs all my values.

Please advise how I handle the null output.

Here is my output:
nullEdwardson
Smith
Carson
Jones



My attempts to eliminate null didnt work because null still shows up in my output:

Code:
ResultSet results = stmt.executeQuery("SELECT * from user where lastName <> null");

I also tried this:
Code:
if(lastName != null}
{
    lastName += resultSet.getString("lastName") +"<br>";
}

 
Alter the code as below :

Code:
ResultSet results = stmt.executeQuery("SELECT * from user");            
lastName = "";
while(results.next())  {
    lastName += resultSet.getString("lastName") +"<br>";
}
// If no records came back, reset lastName to null
if (lastName.length() == 0) {
  lastName = null;
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
feherke - whilst correct, this will not correct the OP's problem, as the issue is not caused by a null record in the result set - but how 'lastName' variable is set.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks,

I tried this and still get the null in front of my results.
Code:
while(results.next())			
{					
   lastName += results.getString("lastName") +"<br>";
   // If no records came back, reset lastName to null
   if (lastName.length() == 0) 
   {
     lastName = null;
   }	
}

Please advise.
 
Sorry that was my mistake.

Thanks for all the help in making this work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top