From: Karen Goh <karenworld@yahoo.com>
To: Rob Sargent <robjsargent@gmail.com>
Cc: pgsql-jdbc@lists.postgresql.org
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
Date: Sat, 7 Sep 2019 03:21:51 +0000 (UTC)
Message-ID: <440942117.2418399.1567826511126@mail.yahoo.com> (raw)
In-Reply-To: <E30447FA-D267-4727-B0AD-A28FC6A90A4A@gmail.com>
References: <932079842.712590.1567405714491@mail.yahoo.com>
<E30447FA-D267-4727-B0AD-A28FC6A90A4A@gmail.com>
Hi Rob,
I found out that Tomcat is inconsistent in giving me the result and as mentioned, even though certain lines are commented out it is pointing to the commented out lines.
And even after clearing cache whatsoever, I am still getting the old error which is not supposed to be there.
Till I really iron out all Tomcat problem, I can't comment anything. But, as of now, the problem I posted here is dissolved.
Right now, it is giving me concurrentModificationList exception at (String s : subject) but this is Java problem and nothing to do with Postgresql...so that's the state of thing now.
Thanks for the reply thought....oh the list is part of the domain model.....as what you have to do to every Model construction...
On Friday, September 6, 2019, 3:24:42 PM GMT+8, Rob Sargent <robjsargent@gmail.com> wrote:
On Sep 2, 2019, at 12:28 AM, Karen Goh <karenworld@yahoo.com> wrote:
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 s: subjList)
if (s.contains(rs.getString("subject_name")))
subjList.add(rs.getString("subject_name"));
newSub.addAll(subjList);
}
myList.add(t);
request.setAttribute("tutors", myList);
request.setAttribute("sub", newSub);
Please. I hope someone can tell me what's wrong with the code or how to overcome this error...I have been struggling for 2 weeks already.
Thanks.
}
myList.add(t);
request.setAttribute("tutors", myList);
request.setAttribute("sub", newSub);
I put your code into my editor as if it were an entire method. That formatted version is below.I find the for loop very odd. See comments with your code below, in bold for easy reference:
void func() {// subjectNames set from the request String[] subjectNames = request.getParameterValues("txtsubjects"); String zipcode = request.getParameter("location”);// subjList built from subjectNames 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++) {// subjList add to query 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 s: subjList)// test if one entry of subjList matches the current row’s “subject_name” value, // repeatedly asking for “subject_name” from current rs if (s.contains(rs.getString("subject_name")))// if so add current value to list being iterated subjList.add(rs.getString("subject_name")); newSub.addAll(subjList);// If I follow the intent correctly (match subjects to tutor) I believe the following would be more effective String rsSubject = rs.getString("subject_name"); if (subjList.contains(rsSubject)) { newSub.add(rsSubject); }// However your sql seems to be asking for all tutors in a given zipcode and what subjects they handle. You might want to at least order the return set by tutor to deal with one tutor over several result rows. Alternatively you could group subjects into an array per tutor:String sql1 = "select t.tutor_contact_no, t.zipcode, t.tutor_id, array_agg(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((?)) group by t.tutor_contact_no,t.zipcode,t.tutor_id”;and grab the array of tutor’s subjects.Further your responce doesn’t tie specific tutor to specific subjects. Perhaps you can add List<String>subjects to your “tutor” class?
} myList.add(t); request.setAttribute("tutors", myList); request.setAttribute("sub", newSub); }
I hope this helps.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: karenworld@yahoo.com, robjsargent@gmail.com
Subject: Re: how to resolve org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
In-Reply-To: <440942117.2418399.1567826511126@mail.yahoo.com>
References: <932079842.712590.1567405714491@mail.yahoo.com> <E30447FA-D267-4727-B0AD-A28FC6A90A4A@gmail.com> <440942117.2418399.1567826511126@mail.yahoo.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox