0

How do I execute \d tablename using Java and PostgreSQL?

PreparedStatement pst = jdbc.conn.prepareStatement("\d u_item");
ResultSet set = pst.executeQuery();

Error:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
8
  • Can it be executed ‘\d tablename’ sql use java? Commented Aug 16, 2019 at 7:10
  • 2
    "\\d u_item" If you want a backslash in your statement, it needs to be escaped with another one. Commented Aug 16, 2019 at 7:14
  • I allready use "\\d u_item" execute Commented Aug 16, 2019 at 7:20
  • still ERROR: syntax error at or near "\" Commented Aug 16, 2019 at 7:21
  • Why do you try to do this? What do you really want to do? Your question looks like a XY Problem. Commented Aug 16, 2019 at 7:23

2 Answers 2

2

\d is a command of the interactive psql client. It is not part of SQL.

You can't execute this command over a JDBC connection.

Sign up to request clarification or add additional context in comments.

2 Comments

can use java query create table sentence?
for example: result CREATE TABLE "public"."test" ( "id" int4 NOT NULL DEFAULT nextval('test_id_seq'::regclass), "name" varchar(255) COLLATE "pg_catalog"."default", CONSTRAINT "test_pkey" PRIMARY KEY ("id") ) ; ALTER TABLE "public"."test" OWNER TO "postgres";
2

You can also obtain table meta data Using Postgres java driver.

String tableName = "u_item";
ResultSet rs = metaData.getColumns(null, null, tableName.toLowerCase(), null))

Table name is converted to lowercase when Postgres stores its name in the system catalog.

[updated] have you checked this

3 Comments

I want result CREATE TABLE "public"."test" ( "id" int4 NOT NULL DEFAULT nextval('test_id_seq'::regclass), "name" varchar(255) COLLATE "pg_catalog"."default", CONSTRAINT "test_pkey" PRIMARY KEY ("id") ) ; ALTER TABLE "public"."test" OWNER TO "postgres";
I want query create table ,not is add create table
you may need to do some manipulation when you obtain result from PgResultSetMetaData

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.