Using Java Bean in JSP to show database record

[nobr]I have links in my Tomcat container that if someone clicks on a specific link it should go to a Record page (Show.jsp) with all the values associated with a record from a database.

For example if someone clicks on a specific link like this:

<a href="Show.jsp?lastname='Jones'">LinkExample</a>

it would take you to a jsp with database info for someone namedJones and show you his info:

Lastname = Jones

Firstname = Mike

City = San Diego

I would like to do this using a Java helper class and bean so I dont have any database connection or Java code in my JSP.

I created Java class file that has Database connection that works with a Java bean. I just dont know how to get theShow.jsp to work with the Java Bean and Java helper class file.

Here is what I have forShow.jsp and this is the part I have been working on the longest but cant get it to work because it doesnt seem to work with the database:<jsp:useBean id="user" class="num.UserDB"/>

//do I call getUser(lastname) here and if so how?

<jsp:setProperty name="user" property="*"/>

Last Name: <jsp:getProperty name="user" property="lastname"/><BR>

First Name: <jsp:getProperty name="user" property="firstname"/><BR>

City: <jsp:getProperty name="user" property="city"/><BR>

My Java Helper class that compiles and connects to database:

package num;

import java.io.*;

import java.sql.*;

import java.util.*;

import num.User;

publicclass UserDB

{

public User getUser(String lastname)

{

User user =new User();

try

{

Class.forName...//database connection stuff here

...........

ResultSet results = stmt.executeQuery("SELECT * from user where lastname = '" + lastname +"'");

if(results.next() ==true)

{

results.next();

}

user.setLastname(results.getString("lastname"));

user.setFirstname(results.getString("firstname"));

user.setFirstname(results.getString("city"));

}

catch(Exception e)

{

System.out.println("Exception...");

}

return user;

}

}

My Java Bean that compiles here:

package num;

publicclass User

{

private String firstname;

private String lastname;

private String city;

public User()

{

//no arg constructor

}

public User(String firstname, String lastname, String city)

{

this.lastname = lastname;

this.firstname = firstname;

this.city = city;

}

public String getLastname()

{

return lastname;

}

publicvoid setLastname(String lastname)

{

this.lastname = lastname;

}

//more bean methods for all fields here

}

[/nobr]

[4852 byte] By [kensingtona] at [2007-11-27 1:55:42]
# 1

You didn't tell us what the problem was, except for "doesnt seem to work with the database", you didn't ask a question, and you didn't provide all of the code. And you hide any useful exception information by printing "Exception" instead of the contents of the exception.

What exactly are you expecting us to do about it?

dcmintera at 2007-7-12 1:29:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Sorry if I wasnt specific enough. I have a link that passes a value (field that is passed is called lastname) to a JSP where I want to show record info for that value that is passed.

My question is how do I show the database record info on Show.jsp for the lastname field value of Jones where I want to use a Java Bean and Database helper class in Show.jsp

Here is the message I get when I hit the link (<a href="Show.jsp?lastname='Jones'">LinkExample</a>) and it goes to Show.jsp:

org.apache.jasper.JasperException: Cannot find any information on property 'lastname' in a bean of type 'num.UserDB'

.....

My UserDB class:

public class UserDB

{

public User getUser(String lastname)

{

User user = new User();

try

{

Class.forName("org.gjt.mm.mysql.Driver");

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/dbconnone?user=smitherson&password=abcdefg");

Statement stmt = conn.createStatement();

ResultSet results = stmt.executeQuery("SELECT * from user where lastname = '" + lastname + "'");

if(results.next() == true)

{

results.next();

}

user.setLastname(results.getString("lastname"));

user.setFirstname(results.getString("firstname"));

user.city(results.getString("city"));

}

catch(Exception e)

{

System.out.println("Exception..." + e);

}

return user;

}

}

Bean class:

package num;

public class User

{

private String firstname;

private String lastname;

private String city;

public User()

{

//no arg constructor

}

public User(String firstname, String lastname, Sting city)

{

this.firstname = firstname;

this.lastname = lastname;

this.city = city;

}

public String getFirstname()

{

return firstname;

}

public void setFirstname(String firstname)

{

this.firstname = firstname;

}

public String getLastname()

{

return lastname;

}

public void setLastname(String lastname)

{

this.lastname = lastname;

}

public String getCity()

{

return city;

}

public void setCity(String city)

{

this.city = city;

}

}

kensingtona at 2007-7-12 1:29:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
(edit: deleted).
dcmintera at 2007-7-12 1:29:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

hi,

I also can't seem to pass the data into the User bean. Whenever I issue a <jsp:getProperty name="User" property="lastname" />, it returns null. So I just instantiated it.

Maybe the other posters can help.

But anyway, this is my workaround code

Show.jsp

<jsp:useBean id="UserDB" class="num.UserDB" />

<%

String lastname = request.getParameter( "lastname" );

String firstname = "";

String city = "";

if (lastname!=null) {

num.User objUser = new num.User();

objUser = UserDB.getUser( lastname );

lastname = objUser.getLastname();

firstname = objUser.getFirstname();

city = objUser.getCity();

}

%>

sofia320a at 2007-7-12 1:29:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Hello there,

Okay,

<jsp:useBean id="user" class="num.UserDB"/>

//Get the lastname set, in Userdb, have a public String lastname

//and also public accessor methods (get/set)

<jsp:setProperty name="user" property="lastname"/>

//Now Here call

<% user.getGetUser(); //type void//

%>

Last Name: <jsp:getProperty name="user" property="lastname"/>

//or Last Name: <%= user.getLastName() %>

First Name: <jsp:getProperty name="user" property="firstname"/>

//or First Name: <% = user.getFirstName()%>

City: <jsp:getProperty name="user" property="city"/>

//or City: <%= user.getCity() %>

Now have these variables set up in your bean class

public String firstname;

public String lastname;

when you call getUser(), set the member variables to the results you get back from the database.

My Java Helper class that compiles and connects to database:

package num;

import java.io.*;

import java.sql.*;

import java.util.*;

import num.User;

public class UserDB

{

public User getUser(String lastname)

{

User user = new User();

try

{

Class.forName...//database connection stuff here

...........

ResultSet results = stmt.executeQuery("SELECT * from user where lastname = '" + lastname + "'");

if(results.next() == true)

{

results.next();

}

/*

user.setLastname(results.getString("lastname"));

user.setFirstname(results.getString("firstname"));

user.setFirstname(results.getString("city"));

*/

this.firstname = results.getString("firstname");.............

}

catch(Exception e)

{

System.out.println("Exception...");

}

return user;

}

}

Message was edited by:

dobsun

dobsuna at 2007-7-12 1:29:08 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...