mkums00
asked on
JDBC driver, getTable Method
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.s ize());
while ( rs1.next() )
{
System.out.println(rs1.get String(1)) ;
}
} catch(Exception ex) {
System.out.println(ex.getM essage());
}
-------------------------- ---------- --------
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.toSt ring());
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.getStri ng(1));
v.addElement(tuple);
}
r.close();
System.out.println("in gettable-3");
return new com.appres.sgi.ResultSet(f ield1_, 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_ro w_num_);
} catch(RuntimeException ex2){
System.out.println(ex2.get Message()) ;
throw ex2;
}
catch(Exception ex1){
System.out.println(ex1.get Message()) ; throw ex1;
}
return true;
}
-------------------------- ---------- ---------- ---
exception is thrown at rows_.elementAt(current_ro w_num_);
thanks in advance for any help.
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.s
while ( rs1.next() )
{
System.out.println(rs1.get
}
} catch(Exception ex) {
System.out.println(ex.getM
}
--------------------------
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(_
r = stmt.executeQuery(sql.toSt
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.getStri
v.addElement(tuple);
}
r.close();
System.out.println("in gettable-3");
return new com.appres.sgi.ResultSet(f
}
--------------------------
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_ro
} catch(RuntimeException ex2){
System.out.println(ex2.get
throw ex2;
}
catch(Exception ex1){
System.out.println(ex1.get
}
return true;
}
--------------------------
exception is thrown at rows_.elementAt(current_ro
thanks in advance for any help.
i.e.
current_row_num_++;
if (current_row_num >= rows_.size()) {
return false;
}
current_row_num_++;
if (current_row_num >= rows_.size()) {
return false;
}
ASKER
CEHJ,
It is giving error on very first iteration. i added the line u mentioned, same result.
It is giving error on very first iteration. i added the line u mentioned, same result.
Sorry - i wasn't concentrating - you're doing that anyway. Please post full stack trace
ASKER
here is the output of ex2.printStackTrace(); in Resultset.java
------------
java.lang.ClassCastExcepti on: [[B
at com.appres.sgi.ResultSet.n ext(Result Set.java:8 3)
at HelloDBQ.main(HelloDBQ.jav a:48)
---------------------
------------
java.lang.ClassCastExcepti
at com.appres.sgi.ResultSet.n
at HelloDBQ.main(HelloDBQ.jav
---------------------
Tell me what this prints. Instead of
>>current_row_ = (Vector) rows_.elementAt(current_ro w_num_);
do
Obejct o = rows_.elementAt(current_ro w_num_);
System.out.println("Curren t row is of type " + o);
current_row_ = (Vector)o ;
>>current_row_ = (Vector) rows_.elementAt(current_ro
do
Obejct o = rows_.elementAt(current_ro
System.out.println("Curren
current_row_ = (Vector)o ;
Or better:
System.out.println("Curren t row is of type " + o.getClass());
System.out.println("Curren
ASKER
here it is
---------------
Current row is of type class [[B
[[B
[[B
java.lang.ClassCastExcepti on: [[B
at com.appres.sgi.ResultSet.n ext(Result Set.java:8 6)
at HelloDBQ.main(HelloDBQ.jav a:48)
-------------------------- ------
thanks
---------------
Current row is of type class [[B
[[B
[[B
java.lang.ClassCastExcepti
at com.appres.sgi.ResultSet.n
at HelloDBQ.main(HelloDBQ.jav
--------------------------
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"[[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?
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?
How does that differ from what i've already said seanrowen?
ASKER
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
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
OK
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.
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.
try this example it will help you..
http://cswww.essex.ac.uk/TechnicalGroup/TechnicalHelp/databaseMetaData.htm
best of luck
R.K
http://cswww.essex.ac.uk/TechnicalGroup/TechnicalHelp/databaseMetaData.htm
best of luck
R.K
>>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.
That's right - that is the situation. 'rows_' is of type Vector and contains arrays of bytes.
OK, I understand what you were referring to. rows_ is a Vector of byte[][].
ASKER
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.toSt ring());
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.getStri ng(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(f ield1_, v);
----------------
Thanks Again
M KUMAR
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(_
r = stmt.executeQuery(sql.toSt
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.getStri
//v.addElement(tuple);
row = new Vector(); // could be a memory leak???
row.addElement(r.getString
row.addElement(r.getString
row.addElement(r.getString
row.addElement(r.getString
//v.addElement(row);
v.add(row);
}
//v.add(row);
r.close();
System.out.println("in gettable-3");
return new com.appres.sgi.ResultSet(f
----------------
Thanks Again
M KUMAR
;-) No problem - don't forget to close the question
Make sure you're not going beyond the max index of the Vector