From: | Chuck Davis <cjgunzel(at)gmail(dot)com> |
---|---|
To: | |
Cc: | "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org> |
Subject: | Re: JDBC |
Date: | 2018-06-10 23:21:34 |
Message-ID: | CAHf=Y_bph5E7pvOzkyFXawDaC=v9mOaTBP=KjYdFp2ZW+RKhkA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
If I query without the "exists" clause I get the PG exception that the
relation does not exist (which I guess tells me the table does not
exist) but I'd like a better way to test for existence than
exceptions.
On Sun, Jun 10, 2018 at 4:15 PM, Dave Cramer <pg(at)fastcrypt(dot)com> wrote:
>
> On 10 June 2018 at 19:09, Chuck Davis <cjgunzel(at)gmail(dot)com> wrote:
>>
>> If I try to return a ResultSet the class will not compile with the
>> message that boolean cannot be converted to a ResultSet. And this is
>> in synch with the documentation that states the "exists" subquery will
>> return a boolean -- not a ResultSet.
>>
>> There are a number of sites that indicate the statement is the way to
>> find out if a table exists. Are the sites for an older version of PG?
>> I'm running on 10.
>>
>> Thanks.
>>
>> On Sun, Jun 10, 2018 at 1:44 PM, David G. Johnston
>> <david(dot)g(dot)johnston(at)gmail(dot)com> wrote:
>> > On Sunday, June 10, 2018, Chuck Davis <cjgunzel(at)gmail(dot)com> wrote:
>> >>
>> >>
>> >> try {
>> >> result = stat.execute("select exists (select 1 from
>> >> information_schema.tables where table_name = 'entities')");
>> >> System.out.println("the checkEntity returned a result of "
>> >> + result);
>> >> } catch (SQLException ex) {
>> >>
>> >> Logger.getLogger(ClientConstants.class.getName()).log(Level.SEVERE,
>> >> null, ex);
>> >> result = false;
>> >> return result;
>> >> }
>> >
>> >
>> > Your query is putting "false" into cell (0,0) of a ResultSet. You are
>> > failing to even look at the resultset to see if it holds a true or
>> > false.
>> >
>> > The query should never fail since you aren't using the table name
>> > directly
>> > but are checking for it as a value in another table that always exists.
>> > The
>> > inner query returns zero records when the table doesn't exist and the
>> > EXISTS
>> > construct converts that to false.
>> >
>> > David J.
>> >
>>
>
>
> idiomatic java suggests that you need to do
>
> resultset rs = statement.query(your select statement)
>
> if rs.next then
> result = rs.getInt(1)
>
> close result set and statement
>
> return result == 1
>
From | Date | Subject | |
---|---|---|---|
Next Message | David G. Johnston | 2018-06-10 23:26:52 | Re: JDBC |
Previous Message | Dave Cramer | 2018-06-10 23:15:45 | Re: JDBC |