File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First Bean with Database

 
Joseph Smithern
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Smithern
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since your question is focused on the JDBC aspect, I've moved this to the JDBC forum.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Smithern
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
executeQueryr( );

That has a typo...
 
Joseph Smithern
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is typo??

 
stu derby
Ranch Hand
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33697
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseph,
I recommend reposting the JSP portion of your question in the JSP forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic