odd results from SQL statement in JSP

Hi.

Getting very strange results from my SQL statement housed in my JSP.

the last part of it is like so:

"SELECT DISTINCT AID, ACTIVE, REQUESTOR_NAME, ..." +

"REQUESTOR_EMAIL" +

" FROM CHANGE_CONTROL_ADMIN a INNER JOIN CHANGE_CONTROL_USER b " +

"ON a.CHANGE_CTRL_ID = b.CHANGE_CTRL_ID " +

" WHERE UPPER(REQUESTOR_NAME) LIKE ? ";

I've set the following variables and statements:

String reqName = request.getParameter("requestor_name");

PreparedStatement prepstmt = connection.prepareStatement(preparedQuery);

prepstmt.setString(1, "%" + reqName.trim().toUpperCase() + "%");

ResultSet rslts = prepstmt.executeQuery();

rslts.next();

int aidn = rslts.getInt(1);

int actbox = rslts.getInt(2);

String reqname = rslts.getString(3).toUpperCase();

String reqemails = rslts.getString(4);

String bizct = rslts.getString(5);

String dept = rslts.getString(6);

String loc = rslts.getString(7);

Date datereq = rslts.getDate(8);

String busvp = rslts.getString(9);

AND SO ON

So then I loop it, or try to with the following:

<%

try{

while ((rslts).next()){ %>

<tr class="style17">

<td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>

<td><%=aidn %></td>

</tr>

<%

}

rslts.close();

selstmt.close();

}

catch(Exception ex){

ex.printStackTrace();

log("Exception", ex);

}

%>

AND so on, setting 13 getXXX methods of the 16 cols in the SQL statement.

Trouble is I'm getting wildly inconsistent results.

For example, typing 'H' (w/o quotes) will spit out 20 duplicate records of a guy named Herman, with the rest of his corresponding info correct, just repeated for some reason.

Typing in 'He' will bring back the record twice (2 rows of the complete result set being queried).

However, typing in 'Her' returns nothing. I could type in 'ell' (last 3 letters of his name, Winchell) and it will again return two duplicate records, but typing in 'hell' would return nothing.

Am I omitting something crucial from the while statement that's needed to accurately print out the results set without duplicating it and that will ensure returning it?

There's also records in the DB that I know are there but aren't being returned. Different names (i.e. Jennifer, Jesse, Jeremy) won't be returned by typing in partial name strings like Je.

Any insight would be largely appreciated.

One sidenote: I can go to SQL Plus and accurately return a results set through the above query. Having said that, is it possible the JDBC driver has some kind of issue?

Message was edited by:

bpropes20

Message was edited by:

bpropes20

[3507 byte] By [bpropes20a] at [2007-10-2 23:05:58]
# 1

> Am I omitting something crucial from the while

> statement that's needed to accurately print out the

> results set without duplicating it and that will

> ensure returning it?

Yes.

In this code, nothing ever changes the value of reqname or any of the other variables.

while ((rslts).next()) { %>

<tr class="style17">

<td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>

<td><%=aidn %></td>

</tr>

<%

}

You code needs to be like this:while (rslts.next()) {

reqname = rslts.getString(3).toUpperCase();

reqemails = rslts.getString(4);

bizct = rslts.getString(5);

dept = rslts.getString(6);

loc = rslts.getString(7);

datereq = rslts.getDate(8);

busvp = rslts.getString(9);

%>

<tr class="style17">

<td><%=reqname%></td><td><%=reqemails %></td><td><%=bizct %></td>td><%=dept %></td>

<td><%=aidn %></td>

</tr>

<%

}

> There's also records in the DB that I know are there

> but aren't being returned. Different names (i.e.

> Jennifer, Jesse, Jeremy) won't be returned by typing

> in partial name strings like Je.

Well, you're half-right, your loop won't display all the rows in the result set, because you call rslts.next(); once immediately after executing the query. That advance the result set to the first row; when the loop is entered, it starts displaying at the 2nd row (or later if there are more next() calls in the code you omitted).

StuDerbya at 2007-7-14 6:19:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

ok, thank you, Stu! Let me fix that up!

Well, that did fix most of it, but strangely enough, there are still records in the table that aren't getting returned, even though I know they're there (I can see them in the SQL Plus query, as well as through the MS Access ODBC I set up).

Wonder why some of those wouldn't be getting returned. Any thoughts there?

Message was edited by:

bpropes20

bpropes20a at 2007-7-14 6:19:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

I was going to ask if this was MS Access. I sort of recall seeing other posts where Access via ODBC was returning strange results for SELECT DISTINCT. Haven't seen them for a while though and I don't remember whether there was any good answers, or whether it was a bug, or what. Try searching this forum for some words out of this reply.

DrClapa at 2007-7-14 6:19:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
If DrClaps's idea is the problem, you'll get the expected results when you remove the DISTINCT from the query and get weird results when the DISTINCT is added back. If so, you'll either need a better driver, or manage distinctness in your Java code.
StuDerbya at 2007-7-14 6:19:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

ok, thanks, guys!

Stu, I used your suggestion, in removing that first .next() call, right after the query was executed...I think I had that in there at that point to test something earlier, then mistakenly left it in, but probably not thinking it would be consequential. However, your post explained definiteively otherwise....thank you!

bpropes20a at 2007-7-14 6:19:56 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...