Solved

JDBC driver, getTable Method

Posted on 2004-04-22
19
477 Views
Last Modified: 2008-03-04
i am  writing a jdbc driver for one of the application. while writing a code for method getTables in DatabaseMetadata.java, i am getting some weird exception while traversing the resultset. (on further debuging, it is coming from next method in Resultset class.)

this is a part of client application which is using JDBC driver.
--------------------------------------------
DatabaseMetaData md = conn.getMetaData();
      System.out.println("TEST-2");

      ResultSet rs1 = md.getTables(null, null, null, null);
      System.out.println("TEST3");
      //System.out.println(rs1.size());
       while ( rs1.next() )
      {
            System.out.println(rs1.getString(1));
      }
    } catch(Exception ex) {
      System.out.println(ex.getMessage());
     
    }
--------------------------------------------

this is a getTable Method
--------------------------------------------
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
    // the field descriptors for the new ResultSet
System.out.println("in gettable-1");
    Field f[] = new Field[4]; // = new Vector();
    Vector field1_ = new Vector();
    ResultSet r;      // ResultSet for the SQL query that we need to do
    Vector v = new Vector();            // The new ResultSet tuple stuff
// FIELD_TYPE_VAR_STRING = 253 in DBQDefs.java
    f[0] = new Field("FILE_ALIAS", 253, 32);
    f[1] = new Field("USER_EXIT", 253 ,32);
    f[2] = new Field("FILE_INDEX", 253, 32);
    f[3] = new Field("FILE_NAME", 253 ,256);

field1_.addElement(f[0]);
field1_.addElement(f[1]);
field1_.addElement(f[2]);
field1_.addElement(f[3]);

    // Now form the query
    StringBuffer sql = new StringBuffer("disp files");
System.out.println("in gettable-2");
    java.sql.Statement stmt = _conn.createStatement();       
     //Statement stmt = new com.appres.sgi.Statement(_conn, _database);
    r = stmt.executeQuery(sql.toString());

      while (r.next())
      {
            byte[][] tuple = new byte[4][0];
            tuple[0] = r.getString(1).getBytes();
            tuple[1] = r.getString(2).getBytes();
            tuple[2] = r.getString(3).getBytes();
                tuple[3] = r.getString(4).getBytes();
            // remove after success
            System.out.print(r.getString(1));
            v.addElement(tuple);
      }
      r.close();
      System.out.println("in gettable-3");
      return new com.appres.sgi.ResultSet(field1_, v);
  }
--------------------------------------------

and this is a part of Resultset.java part from where it throws the exception.
-----------------------------------------------------
public boolean next() throws java.sql.SQLException
      {

          // debug
          //System.out.println("in Resultset Next" + rows_.size());
          if (rows_.size() == 0) return false;
          if (current_row_num_ + 1 >= rows_.size()) return false;

          current_row_num_++;
          try {
          current_row_ = (Vector) rows_.elementAt(current_row_num_);
          } catch(RuntimeException ex2){
              System.out.println(ex2.getMessage());
              throw ex2;
          }
          catch(Exception ex1){
              System.out.println(ex1.getMessage()); throw ex1;

          }
          return true;
      }
-------------------------------------------------
exception is thrown at rows_.elementAt(current_row_num_);

thanks in advance for any help.

0
Comment
Question by:mkums00
  • 10
  • 5
  • 3
  • +1
19 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10890776
>>current_row_ = (Vector) rows_.elementAt(current_row_num_);

Make sure you're not going beyond the max index of the Vector
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10890917
i.e.

current_row_num_++;
if (current_row_num >= rows_.size()) {
    return false;
}
0
 

Author Comment

by:mkums00
ID: 10890989
CEHJ,
It is giving error on very first iteration. i added the line u mentioned, same result.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10891023
Sorry - i wasn't concentrating - you're doing that anyway. Please post full stack trace
0
 

Author Comment

by:mkums00
ID: 10891159
here is the output of ex2.printStackTrace(); in Resultset.java

------------
java.lang.ClassCastException: [[B
        at com.appres.sgi.ResultSet.next(ResultSet.java:83)
        at HelloDBQ.main(HelloDBQ.java:48)

---------------------
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10891222
Tell me what this prints. Instead of

>>current_row_ = (Vector) rows_.elementAt(current_row_num_);

do



Obejct o = rows_.elementAt(current_row_num_);
System.out.println("Current row is of type " + o);
current_row_ = (Vector)o ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10891253
Or better:

System.out.println("Current row is of type " + o.getClass());
0
 

Author Comment

by:mkums00
ID: 10891350
here it is
---------------
Current row is of type class [[B
[[B
[[B
java.lang.ClassCastException: [[B
        at com.appres.sgi.ResultSet.next(ResultSet.java:86)
        at HelloDBQ.main(HelloDBQ.java:48)
--------------------------------

thanks
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 10891463
OK - so what's happening is that you don't have a Vector of Vectors, but a Vector of byte arrays. So you can't cast the row to the expected type (Vector). If you want to be able to have the Vector hold rows of any type, you need to make current_row_ of type Object
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10892473
"[[B" indicates that it's actually a two-dimensional array of bytes -- a byte[][].

I can see that your ResultSet has byte[][]s as its "rows", but you are later treating them as Vectors. This is the problem -- do you mean to treat the rows as byte[][] everywhere?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10892528
How does that differ from what i've already said seanrowen?
0
 

Author Comment

by:mkums00
ID: 10892585
CEHJ,
thanks for the insight. I can't change the type of curr_row_ , but will try to do something diffrent and let you know.
thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10892617
OK
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10896075
CEHJ -- you said that the type of the Object in question was a Vector whose elements are arrays of bytes. The output that mkums00 posted shows that it is actually a byte[][], which is of course not the same thing.

Not a big deal, but I thought that might clarify things. You can see in the code where this result set is filled with byte[][], so maybe that makes things make more sense for him/her.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10896751
try this example it will help you..

http://cswww.essex.ac.uk/TechnicalGroup/TechnicalHelp/databaseMetaData.htm

best of luck

R.K
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10897767
>>you said that the type of the Object in question was a Vector whose elements are arrays of bytes

That's right - that is the situation. 'rows_' is of type Vector and contains arrays of bytes.
0
 
LVL 1

Expert Comment

by:seanrowen
ID: 10899177
OK, I understand what you were referring to. rows_ is a Vector of byte[][].
0
 

Author Comment

by:mkums00
ID: 10899917
Thanks everyone for help. CEHJ was the first to provide the clue about where i was mistaken, I changed the code in getTables and it is working now.

instead of byte[][] , i changed it to be a vector.
----------------
 StringBuffer sql = new StringBuffer("disp files");
    java.sql.Statement stmt = _conn.createStatement();       
     //Statement stmt = new com.appres.sgi.Statement(_conn, _database);
    r = stmt.executeQuery(sql.toString());

      while (r.next())
      {
          /**
            byte[][] tuple = new byte[4][0];
            tuple[0] = r.getString(1).getBytes();
            tuple[1] = r.getString(2).getBytes();
            tuple[2] = r.getString(3).getBytes();
                tuple[3] = r.getString(4).getBytes();
            // remove after success
            System.out.print(r.getString(1));***/
            //v.addElement(tuple);
                row = new Vector(); // could be a memory leak???
                row.addElement(r.getString(1));
                row.addElement(r.getString(2));
                row.addElement(r.getString(3));
                row.addElement(r.getString(4));
                //v.addElement(row);
                v.add(row);
      }
      //v.add(row);
      r.close();
      System.out.println("in gettable-3");
      return new com.appres.sgi.ResultSet(field1_, v);
----------------

Thanks Again
M KUMAR
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10904734
;-) No problem - don't forget to close the question
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
Arrays.asList  VS  ArrayList 4 60
Java exception bubble up 2 18
Running JavaFX on JDeveloper 12C 1 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question