Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

First Bean with Database

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since your question is focused on the JDBC aspect, I've moved this to the JDBC forum.
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
executeQueryr( );

That has a typo...
 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is typo??

 
stu derby
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joseph,
I recommend reposting the JSP portion of your question in the JSP forum.
 
Bear Bibeault
Sheriff
Posts: 67618
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic