X-Original-To: pgsql-jdbc-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 897BD9FB3DF for ; Fri, 22 Sep 2006 00:51:07 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 00980-02 for ; Fri, 22 Sep 2006 00:50:53 -0300 (ADT) X-Greylist: delayed 02:15:25.189098 by SQLgrey- Received: from mail.utiba.com.au (eth2643.vic.adsl.internode.on.net [150.101.158.82]) by postgresql.org (Postfix) with ESMTP id 41EF09FB3DC for ; Fri, 22 Sep 2006 00:50:50 -0300 (ADT) Received: from localhost (scan1.utiba.com.au [127.0.0.1]) by mail.utiba.com.au (Postfix) with ESMTP id 626DE44485; Fri, 22 Sep 2006 13:50:49 +1000 (EST) Received: from mail.utiba.com.au ([127.0.0.1]) by localhost (scan1.utiba.com.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 24221-05; Fri, 22 Sep 2006 13:50:42 +1000 (EST) Received: from [192.168.0.49] (pc49.utiba.melb [192.168.0.49]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.utiba.com.au (Postfix) with ESMTP id 6BB9044481; Fri, 22 Sep 2006 13:50:42 +1000 (EST) Message-ID: <45135D9B.50500@utiba.com> Date: Fri, 22 Sep 2006 13:50:51 +1000 From: Philip Yarra User-Agent: Mozilla Thunderbird 1.0.7 (X11/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Oliver Jowett Cc: Alex Stienstra , pgsql-jdbc@postgresql.org Subject: Re: ResultSetMetaData.getTableName() == null References: <20060921121816.9wvu8sr34w5c4848@webmail.versatel.nl> <45133DE5.4030100@utiba.com> <451354B5.2070701@opencloud.com> In-Reply-To: <451354B5.2070701@opencloud.com> Content-Type: multipart/mixed; boundary="------------050704050102090304070609" X-Virus-Scanned: amavisd-new at utiba.com.au X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.135 tagged_above=0 required=5 tests=FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200609/89 X-Sequence-Number: 15807 This is a multi-part message in MIME format. --------------050704050102090304070609 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Oliver Jowett wrote: > Essentially I think it boiled down to "getTableName() should return the > table alias name", and since we don't have that available we return "". > There's a postgresql-specific interface to get at the underlying table > name (in the cases where that info is available). Hmmm... maybe I'm missing something, but the attached test case doesn't use any aliases, and it returns "" (at least it does for me). Is this expected behaviour? If there's a postgresql-specific way to get it, couldn't this method implement the same way of getting it? BTW: not a big deal for me, I can't see that I'd ever want to use that method. I'm just curious... Regards, Philip. -- Philip Yarra Senior Software Engineer, Utiba Pty Ltd philip@utiba.com --------------050704050102090304070609 Content-Type: text/plain; name="pg.prop" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pg.prop" driver = org.postgresql.Driver url = jdbc:postgresql://your_host:5432/your_db user = you pass = your_password --------------050704050102090304070609 Content-Type: text/x-java; name="rsmd.java" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="rsmd.java" import java.sql.*; import java.util.*; import java.io.*; import java.text.SimpleDateFormat; class rsmd { static Connection conn = null; static Properties prop = null; public static void main(String [] args) { try { if(args.length > 0) prop = loadProp(args[0]); else usage(); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String pass = prop.getProperty("pass"); String driver = prop.getProperty("driver"); Class.forName(driver); conn = DriverManager.getConnection(url,user,pass); conn.setAutoCommit(true); createTable(); insertData(); executeSelect(); conn.commit(); dropTable(); conn.commit(); conn.close(); log("all done"); } catch(Exception ex) { log(ex); System.exit(1); } } static void executeSelect() { PreparedStatement stmt = null; try{ stmt = conn.prepareStatement("SELECT * FROM tempextest"); ResultSet rs = stmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); String table = rsmd.getTableName(1); if(table == null) log("null table name"); if("".equals(table)) log("empty table name"); log("table name [" + table + "]"); } catch (SQLException sqlex) { log(sqlex); closeStatement(stmt); } } static void createTable() { log("Creating table tempextest"); PreparedStatement stmt = null; try { stmt = conn.prepareStatement("CREATE TABLE tempextest(id INT PRIMARY KEY)"); stmt.executeUpdate(); } catch (SQLException sqlex) { log(sqlex); log("error creating table tempextest, can't proceed"); closeStatement(stmt); System.exit(1); } log("Table tempextest created"); } static void insertData() { log("Inserting data into table tempextest"); PreparedStatement stmt = null; try { stmt = conn.prepareStatement("INSERT INTO tempextest(id) VALUES (42)"); stmt.executeUpdate(); } catch (SQLException sqlex) { log(sqlex); log("error inserting data into table tempextest, can't proceed"); closeStatement(stmt); System.exit(1); } log("Table tempextest created"); } static void dropTable() { log("Dropping table tempextest"); PreparedStatement stmt = null; try { stmt = conn.prepareStatement("DROP TABLE tempextest"); stmt.executeUpdate(); } catch (SQLException sqlex) { log(sqlex); closeStatement(stmt); } log("Table tempextest dropped"); } static Properties loadProp(String fileName) { try{ Properties prop = new Properties(); FileInputStream fis = new FileInputStream(fileName); prop.load(fis); return prop; } catch (Exception ex) { log("exception loading properties: " + ex); usage(); } return null; } static void usage() { System.err.println("Usage: java rsmd propfile"); System.exit(1); } static void log(String msg) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.S"); System.out.println(sdf.format(new java.util.Date()) + ":" + msg); } static void log(Exception ex) { log(ex.toString()); ex.printStackTrace(); } static void log(Object o) { log(o.toString()); } static void closeStatement(Statement st) { try{ st.close(); }catch(SQLException sqlex) { log(sqlex); } } } --------------050704050102090304070609--