This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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 ]
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.
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.
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
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.
Joined: Sep 16, 2003
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
Joined: Oct 10, 2000
Did you try using this instead and you still get an error ?
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 ]