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 "\"
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
L.rocky
  • 79
  • 1
  • 5

2 Answers2

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.

  • can use java query create table sentence? – L.rocky Aug 16 '19 at 07:24
  • 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"; – L.rocky Aug 16 '19 at 07:25
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

mayank
  • 46
  • 5
  • 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"; – L.rocky Aug 16 '19 at 07:34
  • I want query create table ,not is add create table – L.rocky Aug 16 '19 at 07:46
  • you may need to do some manipulation when you obtain result from PgResultSetMetaData – mayank Aug 16 '19 at 08:08