PSQL = Yes ... JDBC = no ?? - Mailing list pgsql-general

From Amn Ojee Uw
Subject PSQL = Yes ... JDBC = no ??
Date
Msg-id cbe8bd8f-7d34-29b3-a169-9cefb9f1de83@gmail.com
Whole thread Raw
Responses Re: PSQL = Yes ... JDBC = no ??
List 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.

pgsql-general by date:

Previous
From: veem v
Date:
Subject: Re: Question on Partition key
Next
From: Adrian Klaver
Date:
Subject: Re: PSQL = Yes ... JDBC = no ??