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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bean page showing null output from database

Status
Not open for further replies.

evergreean43

Technical User
May 25, 2006
165
0
0
US
I am trying to get my first database bean working in my Tomcat 5.5 Container.

The page keeps giving me null output. I know it has a value in the MySql database but it keeps showing null in my JSP with JDBC bean.

Please advise what I am doing wrong as I assume my problem is in the JSP where I am calling the bean:

Code:
<%@ page import = "num.dbBean" %>
<jsp:useBean id="db" scope="session" class="num.dbBean">
<jsp:setProperty name="db" property="resultTable" />
</jsp:useBean>
<html>
<body>
s
<jsp:getProperty name="db" property="resultTable" />
</body>
</html>


Here is my Java class file:
Code:
package num;
import java.io.*;
import java.sql.*;
import java.util.*;
 
public class dbBean implements Serializable
{
	   protected transient String query;
	   protected transient String resultTable;
	   protected transient Connection conn;
	   
	   public dbBean()
	   {
			  //query = null;
			  resultTable = null;
			  conn = null;
	   }
	   
	    public void setResultTable(String resultTable)
	   {
	   		this.resultTable = resultTable;
	   }
	   
	   public String getResultTable()
	   {
	   		 this.viewDatabaseTable();
	   		 return resultTable;
	   }
	   
	   public void viewDatabaseTable()
	   {
	   	 
			  
			  executeQueryr(query);
			  
		}
		
		public void executeQueryr(String query)
		{
		 	   if(conn == null)
			   {
			   		initializeDb();
			   }
			   
			   try
			   {
			   	   Statement stmt = conn.createStatement();
				   ResultSet results = stmt.executeQuery("SELECT * from user where lastname = 'Jones'");
 
				   while(results.next())
				   {
				   	//results.getString("lastname");
					String lastName = results.getString("lastname");
					System.out.println(lastName);
						
				   }
				   
				   stmt.close();
				   results.close();
				   conn.close();
				}
				catch (SQLException sqle)
				{
				 	  sqle.printStackTrace();
				}
				
		 }
		 
		 public void initializeDb()
		 {
		  	try
			{                
			   Class.forName("org.gjt.mm.mysql.Driver");
               conn = DriverManager.getConnection("jdbc:mysql://localhost/myfirst?user=root&password=mypasswordhere");
            }
			catch (SQLException sqle)
			{
			   sqle.printStackTrace();
			}
			catch (ClassNotFoundException cnfe)
			{
			   cnfe.printStackTrace();
			}
		 }		
}
 
Might be easier to test the bean in isolation from a main method on a test class, rather than the added complexity of hosting it in a JSP environment.

I can't spot where you're setting the resultTable value from the results of the query. I'd have expected this to be in your executeQuery method. If it's not set, it'll return a null value when accessed by the JSP page via the getResultTable method.

BTW, I release that this is an early attempt, but you'll need to pay some attention to ensuring that your database connection is closed in the event some of the ResultSet code throws an exception. The finally clause of the try is useful for this.

Tim
 
Of cause its null - you have not set it yet.

Your JSP just instantiates the "dbBean" class (BTW, Java class names should begin with capitals), and rhen you ask for the value of "resultTable" - which is going to be null.

Another issue (not related to the null problem) is why do you bother making the class Serializable when all your member varibles are transient ? Its pointless, because marking variables as transient means they will not be serialized - so although your class implements Serializable, nothing in it actually will be.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
If you're going to be building this into a bigger solution, it might be worth reading up on the 'Data Access Object Pattern', sometimes referred to as the DAO Pattern.

Also, as mentioned many times in the Java forums, if you're expecting moderate to heavy loads on your JSP pages which access the db, have a look at connection pooling as a means of getting db connections.

I realise you didn't actually asks about these aspects. I'm just anticipating the next question. Sorry if I'm being presumptious :)

Tim
 
Thanks,

I assume this is where I set the resultTable value from the results of the query:

Code:
public void setResultTable(String resultTable)
{
   this.resultTable = resultTable;
}


How would I set the resultTable in this method to make this work?

Also, I will correct the closing database exception and the Serialization issue.
 
Well, there's also the issue that your query code seems to expect multiple records whilst the bean is designed for single value accesses.

If you retrieved a single value from the database, you'd have a line in the query running part which called the setResultTable with the value. Or you could just assign the value to the resultTable variable directly.

Tim
 
Thanks,

If I understand what you said then this is what I should be doing with my query:

Code:
ResultSet results = stmt.executeQuery("SELECT * from user");

And the JSP bean call will look like this?
Code:
<%@ page import = "num.dbBean" %>
<jsp:useBean id="db" scope="session" class="num.dbBean">
<jsp:setProperty name="db" property="resultTable" value="lastname = 'Jones'"/>
</jsp:useBean>
<html>
<body>
s
<jsp:getProperty name="db" property="resultTable" />
</body>
</html>

The above still gave me null output.

Please advise.
 
ResultSet is a set of results. Even if you are expecting only 1 result. You've got a loop which iterates over the entire ResultSet. Change it to test if there is at least one result with a single results.next() i.e. an if rather than a while.

If you do have one row in the set, extract the value for the field you want, and set that value on the resultTable variable. It is then available to the JSP page when it calls the getResultTable() method.

If the test for results.next() gives a false, then you'll need code in to cope. Maybe you could set the resultTable variable with a "No Result Found" string, which would be displayed by the JSP instead.

Tim
 
... and again, I'd forget about doing the JSP stuff until you've got a working bean. Create a simple plain text class with a main method which creates your bean and tests it. When it's working, you can put it into the JSP page and work on that.

Tim
 
Thanks timr,

I will follow your suggestion on this and create a simple plain text class with a main method and test it from there.

Also, do you or anyone know of a good site or guide on creating a beginners Database Bean to work with JSP? I was hoping for some guidance with a very beginners simple example so I can start from there.
 
You're really not that far away from a beginners simple example with what you've got. Just make the changes we've pointed out and you should have it. (We usually prefer to guide people on this site, rather than simply giving complete solutions.)

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top