Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database access from JSP

 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "no error", does that mean that you've checked the server logs to make sure nothing is blowing up?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dave Robbins
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try using this instead and you still get an error ?

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Robbins:



ResultSet index Start from 1 not from zero
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic