wood burning stoves 2.0*
The moose likes JDBC and the fly likes First Bean with 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 » Databases » JDBC
Bookmark "First Bean with Database" Watch "First Bean with Database" New topic
Author

First Bean with Database

Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
I created my first bean and then created a seperate JSP connecting to a database to make sure I have all my connections working.

Now I want to fetch a database value using a bean in JSP that will work with a class file with my database connection.

JSP:



Java class file:


Please advise how I can do this correctly because I got this error message and need alot of help to get my first bean with database working -

INF\classes\colors>javac TestBeany.java
TestBeany.java:46: <identifier> expected
results.close();
^
TestBeany.java:47: <identifier> expected
conn.close();
^
2 errors
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Count your braces.

Also, when posting code to the forum, please be sure to use the UBB code tags to preserve the formatting of your code.


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

Joined: Feb 11, 2006
Posts: 89
Thanks, after I added brace it still doesnt fetch my data. Please advise if I am doing this correctly or another way so I can just get something like this working for me to build on. I just want to fetch a value using a database bean.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Since your question is focused on the JDBC aspect, I've moved this to the JDBC forum.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Joseph,
The bean should have a "throws SQLException" clause.

Also, try putting the whole thing in a try/catch block. That way you can print out the exception (and troubleshoot the problem more easily) and close the resources in a finally block.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
I tried this:


and got this error message when I compiled:

TestBeany.java:29: cannot find symbol
symbol : variable mquery
location: class colors.TestBeany
executeQueryr(mquery);
^
1 error


Please advise.
stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
executeQueryr( );

That has a typo...
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Where is typo??

stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Oh. Now I see. You've got a method name that is one character different from one of the most common JDBC method names, so every experienced programmer on the planet is going to see a typo that isn't really there. You should change that method name.

Your compiler is saying that in this method:

the variable mquery is not defined. And it's saying that because it's true.
[ June 11, 2006: Message edited by: stu derby ]
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
Thanks it now compiles.

Now I added this to my results loop:


When I pull up my JSP with the bean it shows:
Last Name: Jones

The JSP shows one record. But the \Tomcat 5.5\logs show the correct 2 database records so it does work but wont show on the JSP. Please advise what I am doing wrong with my JSP Bean page because it should show two records instead of one:

stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
Your bean only returns the last record from the ResultSet; it needs to return a collection, which you iterate over in your JSP.
Joseph Smithern
Ranch Hand

Joined: Feb 11, 2006
Posts: 89
how do I iterate over in JSP?


I know I cant use the results.next in scriptlet but I am not familiar how you use a collection in JSP Bean page so please advise:


[ June 11, 2006: Message edited by: Joseph Smithern ]
[ June 11, 2006: Message edited by: Joseph Smithern ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

Joseph,
I recommend reposting the JSP portion of your question in the JSP forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60759
    
  65

Before doing so be sure that your bean is working as expexted and returns a collection of items that you have gathered from the result set.
 
Don't get me started about those stupid light bulbs.
 
subject: First Bean with Database
 
Similar Threads
Extracting data from a bean
Using Java Bean in JSP to show database record
A small web application
Problem with jsp:useBean
Fetching Bean value from database