This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Fetching Bean value from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Fetching Bean value from database" Watch "Fetching Bean value from database" New topic
Author

Fetching Bean value from database

Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
I have a Database bean working great with my JSP.
The records are fetched from my sql in the Java Bean.
But now I want to be able to set the value on the JSP and fetch the records from what is set in the JSP Bean page.
For example if I put in "Jones" in the bean value on the JSP it should return 3 people in my database with the Last Name of Jones.

Here is my attempt that works not putting any hard coded value in the JSP:



Now if I try and put a value to fetch all last names with "Jones" like this:


It gives me an invalid error.

Do I have to change anything in my class file to get this to work?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60758
    
  65

What exactly are you thinking that this line



is going to do for you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
I wanted this to pull up Jones but I realized I needed to add the property name.
I added the property name (lastName) but it still doesnt work.



It now shows all records but I need it to show Jones only.

Please advise.
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197

It now shows all records but I need it to show Jones only.


it shows all names because your SQL is
SELECT * FROM user

Try this:
Extract your database connection code, then make getLastName() and setLastName() methods in a bean that returns lastName

Then use a PreparedStatement instead of Statement





Who dares, wins! (SAS motto)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60758
    
  65

You really need to step back and take the time to go through a JSP and a JDBC tutorial. It appears that you are taking some code that works one way and making random changes to it in the hopes that it will do what you want without understanding what steps need to be taken to make things work in the newly intended manner.

I'm not saying this to be mean or anything, but coming here and asking how to change the code without understanding it isn't going to really help you learn how to do things for yourself.
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Thanks, what are the books I can start with to learn JSP Bean and Databases?
The Head First one I am using doesnt go into details.

Also I tried this and it is not giving me the multiple Jones output:



Here is Bean part:
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Thanks again for all the help and direction on my post.

It worked after I put this in the setString statement:
kwame Iwegbue
Ranch Hand

Joined: Sep 02, 2000
Posts: 197
You can try Core Servlets and JavaServer Pages by Marty Hall. It has excellent coverage of servlets and jsp, and also a very good, straight to the point section on JDBC with some good working code. Before you buy, you can read the full text online at www.coreservlets.com
[ June 18, 2006: Message edited by: kwame Iwegbue ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fetching Bean value from database
 
Similar Threads
session tracking problem
Using Java Bean in JSP to show database record
jsp:getProperty name shows only first word in string.
First Bean with Database
jsp:useBean id not clear