Hi I have problem with this code as it seems to always return the SQL Exception. It was working for a few goes then stopped and came back with exception. so I’m not sure what’s wrong.
I have tried to change the vld.close() to different places before so yeah. Thanks
PreparedStatement vld = conn.prepareStatement("SELECT * FROM carsharing.available(?,?,?,?)");
vld.setString(1, carName);
vld.setString(2, memberUser);
vld.setTimestamp(3, new java.sql.Timestamp(startTime.getTime()));
vld.setTimestamp(4, new java.sql.Timestamp(endTime.getTime()));
System.out.println("test1");
ResultSet rset = vld.executeQuery();
System.out.println("test2");
rset.next();
int num = rset.getInt("num");
boolean valid = rset.getBoolean("aval");
vld.close();
System.out.println(num);
System.out.println(valid);
1) Verify SQL
Can you verify first that the SQL is correct? It doesn’t look correct to me. What you now try to execute is the following SQL:
If you want to limit the resultset by the given name, user and timestamps SQL should look like:
Where you would replace the Field names with the correct column names for your schema.
2) Resultset navigation
Then when you have your resultset after execution you can use first() to navigate to the first position and it also returns boolean so you can check if there even are results available. If you want more values fetched you’ll need a loop.