Re: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next. - Mailing list pgsql-jdbc

From Mark Rotteveel
Subject Re: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next.
Date
Msg-id 7c561f52-004a-010a-3f30-b74c43b6bfa2@lawinegevaar.nl
Whole thread Raw
In response to Re: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next.  (Karen Goh <karenworld@yahoo.com>)
List pgsql-jdbc
On 4-9-2019 09:33, Karen Goh wrote:
> Here's the entire code again:
> 
> String[] subjectNames = request.getParameterValues("txtsubjects");
>  > > String zipcode = request.getParameter("location");
>  > > List<String> subjList = new 
> ArrayList<String>(Arrays.asList(subjectNames));
>  > > List<tutor> myList = new ArrayList<>();
>  > > ArrayList<String> subList = new ArrayList<>();
>  > > CopyOnWriteArrayList<String>newSub = new 
> CopyOnWriteArrayList<String>();
>  > > tutor t = new tutor();
>  > >
>  > >
>  > > String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, 
> t2.subject_name FROM s_tutor t JOIN tutor_subject t2 ON t.tutor_id = 
> t2.tutor_id where t.zipcode = ? And t2.subject_name = Any((?))";
>  > >
>  > > PreparedStatement ps2 = connection.prepareStatement(sql1);
>  > > ps2.setString(1, zipcode);
>  > > for (int i = 0; i < subjectNames.length; i++) {
>  > > Array array = connection.createArrayOf("text", subjList.toArray());
>  > > ps2.setArray(2, array);
>  > > }
>  > > ResultSet rs = ps2.executeQuery();
>  > > while (rs.next()) {
>  > > t.setContactNo(rs.getString("tutor_contact_no"));
>  > > t.setZipcode(rs.getString("zipcode"));
>  > > t.settutor_id(rs.getInt("tutor_id"));
>  > >for (String : subjList)
> even if i used subList.add(rs.get("mycolumnNameforthesubjects")
> And then I used a system.out.println here to print out all the id, 
> contact No, zipcode and subjects name
> and then according to the stacktrace error, the System.out.println is 
> the line that has the error - ResultSet not positioned properly, perhaps 
> you need to call next.
> 
> Basically, I have removed the setAttributes lines and simply replace it 
> with a System out line (which I had earlier used it to see the print-out 
> and it worked before so am not sure why it is not working this time round)
> to make it easier to diagnose what has gone wrong....
>
> It could be due to Tomcat as yesterday I was trying to find out the 
> issue but again and again, even after cleaning Tomcat directory before I 
> run the code, even with certain lines already taken off, it shows me 
> things that are not supposed to be there...

We are missing a lot of context here. The code you quoted doesn't 
contain a System.out line, nor setAttributes lines. You also haven't 
indicated which line is line 117 of searchController.java.

Based on the exception stacktrace, the only thing we can tell you is 
that - apparently - the code is calling ResultSet.getInt(..) before a 
call to ResultSet.next() (or possibly after ResultSet.next() has 
returned false.

The only suspicious thing I noticed in the code in your first email is 
that the for-loop nested in the `while (rs.next())` loop doesn't use a 
block, while the indentation suggests it should.

As an aside, given the code has multiple > prefixes and no indentation 
makes it hard to read: could you copy directly from your IDE instead, or 
maybe publish it in a GitHub gist or something similar?

Mark
-- 
Mark Rotteveel



pgsql-jdbc by date:

Previous
From: Karen Goh
Date:
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next.
Next
From: rob stone
Date:
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet notpositioned properly, perhaps you need to call next.