Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Fetching Bean value from database

 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly are you thinking that this line



is going to do for you?
 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic