Is it possible to call find the number of columns in a JDBC query without executing it?

From: Geoff Hardy (geoffhardynzNONE_at_hotmail.com)
Date: 02/18/04

  • Next message: Chris Smith: "Re: Is it possible to call find the number of columns in a JDBC query without executing it?"
    Date: 18 Feb 2004 14:52:20 -0800
    
    

    Is it possible to call find the number of columns in a query without
    executing it?

    I am using the Oracle JDBC thin client driver and I have a number of
    queries for which I would like to find out how many columns are being
    selected.

    For example, if given "select a, b, from atable" there are two columns
    being selected.

    Executing the queries is likely to be expensive so I want to avoid
    that. I tried using the following code:

    PreparedStatement preparedStatement =
    connection.prepareStatement(queryStatement);
    long columnCount = preparedStatement.getMetaData().getColumnCount();

    This causes the following exception:

    java.sql.SQLException: statement handle not executed: getMetaData
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179)
    at oracle.jdbc.dbaccess.DBError.check_error(DBError.java:1130)
    at oracle.jdbc.driver.OracleResultSetImpl.getMetaData(OracleResultSetImpl.java:160)
    at oracle.jdbc.driver.OraclePreparedStatement.getMetaData(OraclePreparedStatement.java:1137)

    If I call execute before calling getMetaData, this exception is not
    caused, but I would like to avoid calling execute if possible!

    Thanks in advance for any help!


  • Next message: Chris Smith: "Re: Is it possible to call find the number of columns in a JDBC query without executing it?"

    Relevant Pages

    • Re: Structured Exception Handling (was: Try Finally...)
      ... >statements will be traversed outwards exactly once, executing each ... What really bite me is the re-raise of the exception ... and the continuation in statementList2. ... This means that during unwinding the code immediately returns, ...
      (comp.lang.pascal.delphi.misc)
    • Re: Why finally?
      ... I get the same results from executing the code as well. ... This seems to be a definite improvement of V1.x of the framework. ... normal exception paths. ... // Inject an asynchronous ThreadAbortException in the target thread, ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: DirectShow Insanity
      ... The exception happens immediately upon executing the ... > and what exactly is shown in the debug output window? ... > what happens if you wait for completion befor exiting the program? ...
      (microsoft.public.win32.programmer.directx.audio)
    • Re: WebRequest problems
      ... > I now close the stream as well as the response, ... And are you always executing that Close? ... could be throwing an exception. ... the problem is that there's a connection pool and if the responses ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: HasRows() vs. Read() and ERROR suppression
      ... > record found by executing the commandText. ... >> Syntax error converting datetime from character string. ... >> on the Command object, ... >> MUST call .Readon the SqlDataReader, or you don't get the exception. ...
      (microsoft.public.dotnet.framework.adonet)