Programatically deducing VARCHAR2 length?  
Author Message
laredotornado





PostPosted: 2003-10-2 23:34:00 Top

java-programmer, Programatically deducing VARCHAR2 length? Hi,
I'm using WebLogic 5.1 sp12 and Oracle 8.1.7 with JDBC 1.2 drivers.
I was wondering, if I know that a column is a vARCHAR2 column, how
could i deduce the maximum length of that column, short of repeatedly
inserting larger and larger strings until I genertae an exception. I
know with SQL*Plus I can describe the table and find out lengths that
way, but I was hoping for a more general automated method.

Thanks for any help anyone can provide - Dave
 
SteveE





PostPosted: 2003-10-3 23:16:00 Top

java-programmer >> Programatically deducing VARCHAR2 length? D. Alvarado wrote:

> Hi,
> I'm using WebLogic 5.1 sp12 and Oracle 8.1.7 with JDBC 1.2 drivers.
> I was wondering, if I know that a column is a vARCHAR2 column, how
> could i deduce the maximum length of that column, short of repeatedly
> inserting larger and larger strings until I genertae an exception. I
> know with SQL*Plus I can describe the table and find out lengths that
> way, but I was hoping for a more general automated method.
>
> Thanks for any help anyone can provide - Dave

Take a look at java.sql.ResultSetMetaData which you can get from a
ResultSet or PreparedStatement. Method getColumnDisplaySize(int col)
returns what you need I think.

Cheers,
SteveE

 
Andree Gro遝





PostPosted: 2003-10-8 15:27:00 Top

java-programmer >> Programatically deducing VARCHAR2 length? D. Alvarado wrote:
> Hi,
> I'm using WebLogic 5.1 sp12 and Oracle 8.1.7 with JDBC 1.2 drivers.
> I was wondering, if I know that a column is a vARCHAR2 column, how
> could i deduce the maximum length of that column, short of repeatedly
> inserting larger and larger strings until I genertae an exception. I
> know with SQL*Plus I can describe the table and find out lengths that
> way, but I was hoping for a more general automated method.

You will get information about table definition as follows:

ResultSet rs = con.getMetaData().getColumns(null, schema, tableName, "%");
while (rs.next()) {
String name = rs.getString("COLUMN_NAME");
String type = rs.getString("TYPE_NAME");
long size = rs.getLong("COLUMN_SIZE");
int digits = rs.getInt("DECIMAL_DIGITS");
}
rs.close();

And you will get information about supported data types like:

ResultSet rs = con.getMetaData().getTypeInfo();
while (rs.next()) {
String name = rs.getString("TYPE_NAME");
long len = rs.getLong("PRECISION");
int nullable = rs.getInt("NULLABLE");
}
rs.close();

See also Java-API-Ref of class java.sql.DatabaseMetaData.

HTH A.G.