PSQL = Yes ... JDBC = no ??

From: Amn Ojee Uw <amnojeeuw(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: PSQL = Yes ... JDBC = no ??
Date: 2023-09-03 16:00:22
Message-ID: cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello!

This issue really puzzles me beyond intrigue.

Why would this schema:
*SELECT 'CREATE DATABASE jme_test_database' WHERE NOT EXISTS (SELECT
FROM pg_database WHERE datname = 'jme_test_database')\gexec*
work when entered at the psql prompt, but not when passed as a parameter
in a JDBC method?

The bellow snip demonstrates the the creation of a String object with
same character string used in the PSQL#.
//CREATE DATABASE IF NOT EXISTS
//~~~~~~~~~~~~~~~~~~
*var s = new ***myString*(SELECT 'CREATE DATABASE jme_test_database'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname =
'jme_test_database')\gexec);**
**
**stmt.executeUpdate(s.toString());*

However, this time PostgreSQL-15 complains with an error message saying :

*Exception in thread "main" org.postgresql.util.PSQLException: ERROR:
syntax error at or near "\"**
**  Position: 122

*

For better clarification, I'd like to show the code to create the schema
inside Java/JDBC.
---- snip ----

 */
   ...

    var sv = myString();
    ...

    public final myString getCreateDatabase(myString s){
        this.sv.setData("SELECT 'CREATE DATABASE ");
        this.sv.append(s.toString());
        this.sv.append("\'");
        this.sv.append("WHERE NOT EXISTS (SELECT FROM pg_database WHERE
datname = ");
        this.sv.append("\'");
        this.sv.append(s.toString());
        this.sv.append("\'");
        this.sv.append(")\\gexec");

        return sv;
    }

Any help would be very much appreciated.

Thanks in advance.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2023-09-03 16:04:19 Re: PSQL = Yes ... JDBC = no ??
Previous Message veem v 2023-09-03 10:52:32 Re: Question on Partition key