aspose file tools*
The moose likes JSP and the fly likes database access from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "database access from JSP" Watch "database access from JSP" New topic
Author

database access from JSP

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
Hello All

I've played with java quite a bit but am new to JSP's
I want to put this code in my JSP to retieve some data from a MySQL database



if I run this code in a stand alone jave app it works fine
when I put it in a JSP it returns an empty ResultSet even though it should be getting data
no error, it just doesn't return the data it should
is there something different about JSP's I'm missing
I realize I could just put the code in a bean or a servlet and the problem would go away, I just want to understand what's going on

Thanks
Dave
[ January 05, 2007: Message edited by: Dave Robbins ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

When you say "no error", does that mean that you've checked the server logs to make sure nothing is blowing up?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

You shouldn't be doing database access in a JSP. It violates all manner of good practices and so on so forth...

Apart from all the architectural clap-trap I could pile upon you, as you can see, it makes it hard to debug when things go awry.

Rather, factor all your database access out into a normal java class that you can test from a standalone command-line application. Once that is working, it should be easy to call (not move) the code from your JSP, or better yet, a servlet controller that you use to keep all code out of JSPs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I wouldn't expect any output from that code, even if you uncomment the line in the while loop. You aren't writing anyything out through the JspWriter so you should just get a blank page. That doesn't mean you don't get a ResultSet with data.


GenRocket - Experts at Building Test Data
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
I'm using Netbeans as a development environment and single stepping the code
nothing fails
it just doesn't return any data
if I uncomment the "//Object o = rs.getString(0);" line it will throw an exception and say I've run off the end of the rowset
I know I should move the code out of the jsp but I want to know why it's failing

Dave
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
From the ResultSet Javadoc:

The ResultSet interface provides getter methods (getBoolean, getLong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. In general, using the column index will be more efficient. Columns are numbered from 1. For maximum portability, result set columns within each row should be read in left-to-right order, and each column should be read only once.


It looks like you are indexing from zero instead of 1.
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
Carol

you're correct, but it doesn't explain why it's coming back with no data
I've converted it to using a bean and it works fine
I'm perplexed

Dave
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Did you try using this instead and you still get an error ?

Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Dave Robbins:
Carol

you're correct, but it doesn't explain why it's coming back with no data
I've converted it to using a bean and it works fine
I'm perplexed

Dave


I assume you've got the database calls wrapped in a set of try/catch blocks.
What are you doing when an exception is thrown?
What's in your catch block?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Dave, I have to agree with Bear. If you want to learn about JSP, this isn't the way to do that. One of the features of Java scriptlets in JSP is that they are very difficult to debug. So, now that you have learned that may I suggest you move on to something more suitable?
[ January 08, 2007: Message edited by: Paul Clapham ]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Dave Robbins:



ResultSet index Start from 1 not from zero


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: database access from JSP