Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.

From: rob stone <floriparob(at)gmail(dot)com>
To: Karen Goh <karenworld(at)yahoo(dot)com>, pgsql-jdbc(at)lists(dot)postgresql(dot)org, Mark Rotteveel <mark(at)lawinegevaar(dot)nl>
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
Date: 2019-09-04 08:40:46
Message-ID: 9b008bfa3107e8a3f7505b6f300237d1e13eff7f.camel@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello,

On Wed, 2019-09-04 at 07:33 +0000, 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)
>

As I said last time, I'm not an "expert".

subjectNames is declared as an array.
In your "for" loop you will be doing ps2.setArray(2, array); multiple
times depending on the number of subjects contained in subjectNames.

I suspect the query is failing and as there is no try .. catch block to
trap the SQLException, this is not reported back to your code.

If you alter the postgresql.conf to log all SQL statements, restart,
run your test again, look in the log file, it will show the passed
parameter values. You can then verify that that the array passed as the
second parameter is correct.

HTH,
Robert

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Rob Sargent 2019-09-04 16:39:09 Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
Previous Message Mark Rotteveel 2019-09-04 08:03:41 Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.