Обсуждение: next() error

Поиск
Список
Период
Сортировка

next() error

От
Nahum Castro
Дата:
I am using postgresql 8 and the 8 JDBC 3 driver.
I get the next error:

javax.servlet.ServletException: ResultSet not positioned properly,
perhaps you need to call next.

This code used to work on postgresql 7.4.6 and 215 JDBC 3 driver
Has changed something on the server or on the driver?

Thanks for your help.

This is the code on a jsp page:
-------------------------------------------------------------------------------------
<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="java.sql.*" errorPage="" %>
<%@ include file="Connections/connPubs.jsp" %>

<%
String RecordsetPubs1 = "1";
if (request.getParameter("au_id") != null)
{
    RecordsetPubs1 = (String)request.getParameter("au_id");
}
%>

<%
Driver DriverRecordsetPubs1 =
(Driver)Class.forName(connPubs_DRIVER).newInstance();
Connection ConnRecordsetPubs1 =
DriverManager.getConnection(connPubs_STRING,connPubs_USERNAME,connPubs_PASSWORD);
PreparedStatement StatementRecordsetPubs1 =
ConnRecordsetPubs1.prepareStatement("SELECT * FROM public.authors
WHERE au_id = " + RecordsetPubs1 + "");
ResultSet RecordsetPubs1 = StatementRecordsetPubs1.executeQuery();
boolean RecordsetPubs1_isEmpty = ! RecordsetPubs1.next();
boolean RecordsetPubs1_hasData = ! RecordsetPubs1_isEmpty;
Object RecordsetPubs1_data;
int RecordsetPubs1_numRows = 0;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <table align="center" border="1">
  <tr>
    <td align="left" width="50%">au_id</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("au_id"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">au_lname</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("au_lname"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">au_fname</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("au_fname"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">phone</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("phone"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">address</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("address"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">city</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("city"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">state</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("state"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  <tr>
    <td align="left" width="50%">zip</td>
    <td align="left" width="50%"><%=(((RecordsetPubs1_data =
RecordsetPubs1.getObject("zip"))==null ||
RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
  </tr>
  </table>

</body>
</html>
<%
RecordsetPubs1.close();
StatementRecordsetPubs1.close();
ConnRecordsetPubs1.close();
%>
--------------------------------------------------------------------------------------

--
Nahum Castro González
León, Guanajuato, México

Re: next() error

От
Oliver Jowett
Дата:
Nahum Castro wrote:

> javax.servlet.ServletException: ResultSet not positioned properly,
> perhaps you need to call next.

> Has changed something on the server or on the driver?

Newer drivers follow the JDBC specification more strictly.

> ResultSet RecordsetPubs1 = StatementRecordsetPubs1.executeQuery();
> boolean RecordsetPubs1_isEmpty = ! RecordsetPubs1.next();
> boolean RecordsetPubs1_hasData = ! RecordsetPubs1_isEmpty;

[...]

>     <td align="left" width="50%"><%=(((RecordsetPubs1_data =
> RecordsetPubs1.getObject("au_id"))==null ||
> RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>

You appear to be calling ResultSet.getObject() even if ResultSet.next()
returned false. You're not allowed to do this -- see the JDBC javadoc.

-O

Re: next() error

От
Nahum Castro
Дата:
On Fri, 04 Feb 2005 12:05:12 +1300, Oliver Jowett <oliver@opencloud.com> wrote:
> Nahum Castro wrote:
>
> > javax.servlet.ServletException: ResultSet not positioned properly,
> > perhaps you need to call next.
>
> > Has changed something on the server or on the driver?
>
> Newer drivers follow the JDBC specification more strictly.
>
> > ResultSet RecordsetPubs1 = StatementRecordsetPubs1.executeQuery();
> > boolean RecordsetPubs1_isEmpty = ! RecordsetPubs1.next();
> > boolean RecordsetPubs1_hasData = ! RecordsetPubs1_isEmpty;
>
> [...]
>
> >     <td align="left" width="50%"><%=(((RecordsetPubs1_data =
> > RecordsetPubs1.getObject("au_id"))==null ||
> > RecordsetPubs1.wasNull())?"":RecordsetPubs1_data)%></td>
>
> You appear to be calling ResultSet.getObject() even if ResultSet.next()
> returned false. You're not allowed to do this -- see the JDBC javadoc.
>
> -O
>
Thank you.
I solved the problem now.

--
Nahum Castro González
León, Guanajuato, México