Thread: Problem in getting column names from DatabaseMetadata Object

Problem in getting column names from DatabaseMetadata Object

From
stallapp
Date:
Hi,

I am using the following code to get the column names of my postgres table
but it fails. My database name is "employee" and table name is "employee".

Postgres version : 9.3
jdbc driver version : 9.3

public class DBTest {
    public static void main(String[] args) throws Throwable {
        Connection conn = getDBConnection();
        DatabaseMetaData metaData = conn.getMetaData();
        ResultSet resultSet = metaData.getColumns(null, "employee", "employee",
null);

        if (!resultSet.next())
            System.out.println("No result set Entries!!");

        while (resultSet.next()) {
            System.out.println(resultSet.getString("COLUMN_NAME"));
        }
        conn.close();
    }

    public static Connection getDBConnection() {
        Connection conn = null;
        try {
            Class.forName("org.postgresql.Driver");

            conn =
DriverManager.getConnection("jdbc:postgresql://sampel.db:5432/data_gen",
"postgres",
                    "postgres");
        } catch (Throwable th) {
            th.printStackTrace();
        }
        return conn;
    }

any ideas??

Thanks



--
View this message in context:
http://postgresql.nabble.com/Problem-in-getting-column-names-from-DatabaseMetadata-Object-tp5872498.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: Problem in getting column names from DatabaseMetadata Object

From
Thomas Kellerer
Date:
> I am using the following code to get the column names of my postgres table
> but it fails. My database name is "employee" and table name is "employee".

But you are connecting to a database called "data_gen":

> DriverManager.getConnection("jdbc:postgresql://sampel.db:5432/data_gen", "postgres"

Also: what exactly does "it fails" mean? Do you get an exception? If yes, what is the exception?

Thomas

Re: Problem in getting column names from DatabaseMetadata Object

From
Dave Cramer
Date:
Leave the schema pattern null as well and it should work


On 3 November 2015 at 08:23, stallapp <sree.tallapalli@gmail.com> wrote:
Hi,

I am using the following code to get the column names of my postgres table
but it fails. My database name is "employee" and table name is "employee".

Postgres version : 9.3
jdbc driver version : 9.3

public class DBTest {
        public static void main(String[] args) throws Throwable {
                Connection conn = getDBConnection();
                DatabaseMetaData metaData = conn.getMetaData();
                ResultSet resultSet = metaData.getColumns(null, "employee", "employee",
null);

                if (!resultSet.next())
                        System.out.println("No result set Entries!!");

                while (resultSet.next()) {
                        System.out.println(resultSet.getString("COLUMN_NAME"));
                }
                conn.close();
        }

        public static Connection getDBConnection() {
                Connection conn = null;
                try {
                        Class.forName("org.postgresql.Driver");

                        conn =
DriverManager.getConnection("jdbc:postgresql://sampel.db:5432/data_gen",
"postgres",
                                        "postgres");
                } catch (Throwable th) {
                        th.printStackTrace();
                }
                return conn;
        }

any ideas??

Thanks



--
View this message in context: http://postgresql.nabble.com/Problem-in-getting-column-names-from-DatabaseMetadata-Object-tp5872498.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbc

Re: Problem in getting column names from DatabaseMetadata Object

From
stallapp
Date:
Tried passing schema pattern also null, still resultset is empty.



--
View this message in context:
http://postgresql.nabble.com/Problem-in-getting-column-names-from-DatabaseMetadata-Object-tp5872498p5872511.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.


Re: Problem in getting column names from DatabaseMetadata Object

From
stallapp
Date:
Works with following change.

ResultSet resultSet = metaData.getColumns(null, null, "employee", null);

Thanks



--
View this message in context:
http://postgresql.nabble.com/Problem-in-getting-column-names-from-DatabaseMetadata-Object-tp5872498p5872538.html
Sent from the PostgreSQL - jdbc mailing list archive at Nabble.com.