Thread: postgres-jdbc

postgres-jdbc

From
aparna
Date:
hi
I'm a computer student.
as part of my mini project i'm working on the project GUI frontend for
postgres.

i'm doing it in Java.
Is there any way to retrieve the structure of a table from the java
program and display it?

I tried to execute the command \d using the program.
it worked.
But i couldn't get the result.
Can anybody help?
Please send mail to
aparna@chintech.org
--regards aparna


Re: postgres-jdbc

From
"mikael-aronsson"
Date:
Hi !

The meta data gives all information you need about the tablöe, for example
to get column names you could:

ResultSet rs = ....
ResultSetMetaData meta = rs.getMetaData();
int count = meta.getColumnCount();
String[] cols = new String[ count];
int i;

for( i = 1; i <= count; i++)
  cols[ i - 1] = meta.getColumnLabel( i);

I am not sure if this is what you want, if it is this all part of the JDBC
standard so just look it up in the JDBC docs.

Mikael

----- Original Message -----
From: "aparna" <aparna@chintech.org>
To: <pgsql-jdbc@postgresql.org>
Sent: Thursday, March 17, 2005 2:40 PM
Subject: [JDBC] postgres-jdbc


> hi
> I'm a computer student.
> as part of my mini project i'm working on the project GUI frontend for
> postgres.
>
> i'm doing it in Java.
> Is there any way to retrieve the structure of a table from the java
> program and display it?
>
> I tried to execute the command \d using the program.
> it worked.
> But i couldn't get the result.
> Can anybody help?
> Please send mail to
> aparna@chintech.org
> --regards aparna
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings


Re: postgres-jdbc

From
"Chris Smith"
Date:
aparna wrote:
> i'm doing it in Java.
> Is there any way to retrieve the structure of a table from the java
> program and display it?
>
> I tried to execute the command \d using the program.
> it worked.
> But i couldn't get the result.

\d is a meta-command in psql, and it only works in psql.  The same basic
information, though, is available through JDBC.  Take a look at the
java.sql.DatabaseMetaData class in your copy of the Java API documentation.
Basically, once you've opened a connection of the database, call getMetaData
on the connection, and then call methods of the resulting class.

The columns in a table are available by calling getColumns(...) on
DatabaseMetaData.

The results are (confusingly) returned in a ResultSet instead of a more
specific data structure, so you'll need to make frequent reference to the
API documentation in order to interpret what you get back.  Also, since the
structure of that ResultSet is specified by Java and not by PostgreSQL,
there will be many inapplicable or undeterminable columns.  However, it will
give you the same information provided about table columns by "\d" in psql.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Re: postgres-jdbc

From
Roland Walter
Date:
java.sql.Connection offers the method getMetaData() that returns
a java.sql.DatabaseMetaData object.

Refer to the java api at

http://java.sun.com/j2se/1.5.0/docs/api/index.html

for further information of usage.

--
Roland Walter
MOSAIC SOFTWARE AG
Telefon: 02225/882-411 Fax: 02225/882-201
http://www.mosaic-ag.com