Re: ResultSetMetaData.getTableName() == null

Поиск
Список
Период
Сортировка
От Philip Yarra
Тема Re: ResultSetMetaData.getTableName() == null
Дата
Msg-id 45135D9B.50500@utiba.com
обсуждение исходный текст
Ответ на Re: ResultSetMetaData.getTableName() == null  (Oliver Jowett <oliver@opencloud.com>)
Ответы Re: ResultSetMetaData.getTableName() == null
Список pgsql-jdbc
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
driver = org.postgresql.Driver
url = jdbc:postgresql://your_host:5432/your_db
user = you
pass = your_password
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);
        }
    }


}

В списке pgsql-jdbc по дате отправления:

Предыдущее
От: Oliver Jowett
Дата:
Сообщение: Re: ResultSetMetaData.getTableName() == null
Следующее
От: Oliver Jowett
Дата:
Сообщение: Re: ResultSetMetaData.getTableName() == null