Sagewire Logo

How to know columns names in Derby database table

6 Message(s) by 4 Author(s) originally posted in java databases


From: Luis Angel Fdez. Fdez. Date:   Sunday, September 23, 2007
Hi!

I'm trying this:

[...]

Vector<String> vNames = new Vector<String>();
String[] names;
try {
ResultSet myRs;
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby:/home/koxo/tmp/db/tests/
ubicharge");
myRs = con.getMetaData().getColumns(null , null, "bornes", "%");
while (myRs.next()) {
String str = myRs.getString("COLUMN_NAME");
vNames.add(str);
}
} catch (SQL Exception sqle) {
System.out.println(sqle.toString());
}
catch(Exception e) {
System.out.println("getColumnNames: "+e.toString());
e.printStackTrace();
}

[...]

The connection is made, and the 'bornes' table exists, but the loop is
never executed. What's wrong?

Thanks in advance.

Bye!

--
Slackware 11.0.0 (kernel 2.6.22 i686) Gnome 2.16.3
Intel(R) Core(TM)2 Quad CPU (2260.386 MHz) up 6 days, 1:28
Hattrick: Zanzabornín (1457021) X.1762 # Jabber: laffdez@xxxxxxxxxxx
Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754


From: Roedy Green Date:   Sunday, September 23, 2007
On Sun, 23 Sep 2007 09:54:50 +0200, "Luis Angel Fdez. Fdez."
wrote in message, quoted or indirectly quoted someone who
said :

[...]
The connection is made, and the 'bornes' table exists, but the loop is
never executed. What's wrong?



another way to get this information is with commands like this:

SHOW DATABASES; -- examine list of supported data bases
USE mydata; -- select mydata database
SHOW TABLES; -- examine tables in mydata database
DESCRIBE animals; -- look at column descriptions in the animals table
CREATE DATABASE plants; -- create a new database.
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: Roedy Green Date:   Sunday, September 23, 2007
On Sun, 23 Sep 2007 09:54:50 +0200, "Luis Angel Fdez. Fdez."
wrote in message, quoted or indirectly quoted someone who
said :

myRs = con.getMetaData().getColumns(null, null, "bornes", "%");



This is a quite tricky bit of code . I'd try dumping some other
MetaData info out to make sure all is working.

The way I read the docs, the fourth parm must be the column name. That
isn't logical given the name of the method. Also presume all is
case-sensitive.ResultSet getColumns(String catalog,
String schemaPattern,
String tableNamePattern,
String columnNamePattern)
throws SQLException
Retrieves a description of table columns available in the specified
catalog.

Only column descriptions matching the catalog, schema, table and
column name criteria are returned. They are ordered by
TABLE_CAT,TABLE_SCHEM, TABLE_NAME, and ORDINAL_POSITION.

Each column description has the following columns:
TABLE_CAT String => table catalog (may be null)
TABLE_SCHEM String => table schema (may be null)
TABLE_NAME String => table name
COLUMN_NAME String => column name
DATA_TYPE int => SQL type from JAVA.sql.Types
TYPE_NAME String => Data source dependent type name, for a UDT the
type name is fully qualified
COLUMN_SIZE int => column size.
BUFFER_LENGTH isn't used.
DECIMAL_DIGITS int => the number of fractional digits. Null is
returned for data types where DECIMAL_DIGITS isn't applicable.
NUM_PREC_RADIX int => Radix (typically either 10 or 2)
NULLABLE int => is NULL allowed.
columnNoNulls - might not allow NULL values
columnNullable - definitely allows NULL values
columnNullableUnknown - nullability unknown
REMARKS String => comment describing column (may be null)
COLUMN_DEF String => default value for the column, which should be
interpreted as a string when the value is enclosed in single quotes
(may be null)
SQL_DATA_TYPE int => unused
SQL_DATETIME_SUB int => unused
CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in
the column
ORDINAL_POSITION int => index of column in table (starting at 1)
IS_NULLABLE String => ISO rules are used to determine the nullability
for a column.
YES --- if the parameter can include NULLs
NO --- if the parameter cannot include NULLs
empty string --- if the nullability for the parameter is unknown
SCOPE _CATLOG String => catalog of table that is the scope of a
reference attribute (null if DATA_TYPE is not REF)
SCOPE_SCHEMA String => schema of table that is the scope of a
reference attribute (null if the DATA_TYPE is not REF)
SCOPE_TABLE String => table name that this the scope of a reference
attribure (null if the DATA_TYPE is not REF)
SOURCE_DATA_TYPE short => source type of a distinct type or
user-generated Ref type, SQL type from JAVA.sql.Types (null if
DATA_TYPE is not DISTINCT or user-generated REF)
IS_AUTOINCREMENT String => Indicates whether this column is auto
incremented
YES --- if the column is auto incremented
NO --- if the column isn't auto incremented
empty string --- if it cannot be determined whether the column is auto
incremented parameter is unknown

The COLUMN_SIZE column the specified column size for the given column.
For numeric data, this is the maximum precision . For character data,
this is the length in characters. For datetime datatypes, this is the
length in characters of the String representation (assuming the
maximum allowed precision of the fractional seconds component). For
binary data, this is the length in bytes. For the ROWID datatype, this
is the length in bytes. Null is returned for data types where the
column size isn't applicable.

Parameters:
catalog - a catalog name; must match the catalog name as it is stored
in the database; "" retrieves those without a catalog; null means that
the catalog name shouldn't be used to narrow the search
schemaPattern - a schema name pattern; must match the schema name as
it is stored in the database; "" retrieves those without a schema;
null means that the schema name shouldn't be used to narrow the
search
tableNamePattern - a table name pattern; must match the table name as
it is stored in the database
columnNamePattern - a column name pattern; must match the column name
as it is stored in the database
Returns:
ResultSet - each row is a column description
Throws:
SQLException - if a database access error occurs
See Also:
getSearchStringEscape()
--
Roedy Green Canadian Mind Products
The JAVA Glossary
http://mindprod.com


From: Luis Angel Fdez. Fdez. Date:   Sunday, September 23, 2007
Hi!

With this code I get the columns names:

<!-- BEGIN CODE -->
public String[] getColumnsNames(String table) {
Vector<String> vNames = new Vector<String>();
String query = "select * from bornes";
String[] names;
try {
PreparedStatement pstmt;
ResultSetMetaData rsmd;
int numColumns, I = 1;
if (con == null) {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby:"+
"/home/koxo/tmp/db/tests/ubicharge");
}
pstmt = con.prepareStatement(query);
rsmd = pstmt.getMetaData();
numColumns = rsmd.getColumnCount();
while (i <= numColumns) {
String str = rsmd.getColumnName(i);
System.out.println(str);
vNames.add(str);
i++;
}
} catch (SQLException sqle) {
System.out.println(sqle.toString());
}
catch(Exception e) {
System.out.println("getColumnNames: "+e.toString());
e.printStackTrace();
}
names = new String[vNames.size()];
for (int i=0 ; i<vNames.size(); i++) {
names[i] = new String(vNames.elementAt(i));
}
try {
con.close();
} catch (SQLException sqle) {
System.out.println("close: "+sqle.toString());
sqle.printStackTrace();
}
return names;
}

<!-- END CODE -->


What I do not know is if there is any better option for making it.

Bye!

--
Slackware 11.0.0 (kernel 2.6.22 i686) Gnome 2.16.3
Intel(R) Core(TM)2 Quad CPU (2260.386 MHz) up 6 days, 4:42
Hattrick: Zanzabornín (1457021) X.1762 # Jabber: laffdez@xxxxxxxxxxx
Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754


From: Lew Date:   Sunday, September 23, 2007
wrote in message:
Hi!
With this code I get the columns names:
<!-- BEGIN CODE -->
public String[] getColumnsNames(String table) {
Vector<String> vNames = new Vector<String>();



Are you relying on the synchronization of Vector methods?

Vector is kind of long in the tooth, and has non-Collection methods that could
cause maintenance difficulty down the road.

Why did you choose Vector over, say, ArrayList?

--
Lew


From: Dyreatnews Date:   Monday, September 24, 2007
"Luis Angel Fdez. Fdez." <laffdez@xxxxxxxxxxx> writes:

Hi!
I'm trying this:
[...]
Vector<String> vNames = new Vector<String>();
String[] names;
try {
ResultSet myRs;
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
con = DriverManager.getConnection("jdbc:derby:/home/koxo/tmp/db/tests/
ubicharge");
myRs = con.getMetaData().getColumns(null, null, "bornes", "%");
while (myRs.next()) {
String str = myRs.getString("COLUMN_NAME");
vNames.add(str);
}
} catch (SQLException sqle) {
System.out.println(sqle.toString());
}
catch(Exception e) {
System.out.println("getColumnNames: "+e.toString());
e.printStackTrace();
}
[...]
The connection is made, and the 'bornes' table exists, but the loop is
never executed. What's wrong?



Derby stores table names as upper case, even if you create the table
with lower case. That is,

create table bornes(...);

is equivalent to

CREATE TABLE BORNES(...);

If you read the JAVAdoc for DatabaseMetaData.getColumns() carefully you will
see that the third argument, the table name pattern, "must match the
table name as it is stored in the database". Hence you must use upper
case in the metadata call, i.e.

con.getMetaData().getColumns(null, null, "BORNES", "%");

should work.

PS: For derby-specific questions it is usually better to ask on the
derby-user mailing list.

HTH

--
dt



Next Message: CLOB issue with JDBC, IBATIS and Oracle 10g


Blogs related to How to know columns names in Derby database table

International site. Check it now for a lot of offers.
English for hotel staff real college girls new pension scheme review in nigeria turkey gravy boat Anal sex derby great barrier reef map self-tapping washerhead screws 8 bullock corps Immresions beethoven piano trio op. 1 no. ...

Welcome to my awesome website! Word up, you all.
encilikl jugulator judas priest marine corp league condominiums in san francisco ca dcai auto play dice derby for pogo ilooking for great great great father his name is wrt jenkias he buried at dehart cemetery ...

Official club site with downloads, statistics and news.
Music group bond jack white National lift of arkansas little rock ar rhino productions thank you lord that you know my name. microsoft exchange hosting bayer & printer cable & cn36 fidelhardwoodfloors.com corelation between seizures and ...

Features, columns, style and food coverage.
how to integrate java and excel using netbeans voystsky theory on nature verse nuture murine earigator genealogy for levi h. robison what does anoerexia look like in last stages?. san simon az car hatfield, pa ballbox@cfdynamics 9232 ...

A magazine containing a variety of articles.
linux run bin Manufacturers reps grannies fucking girls le pain quotidien cafe step ambitions column inventory michelle pfeiffer pictures what are ossuaries? Vessel day rates apa have 39 non producing wells 947 570 78 00 riviera on the ...

Home of the funny and useful products.
which stars have pen names? detachment bed bug eradication mcminns restaurant. tataindicom broadband saucony silver white select unknown column name. 442 001 76 67 61. 44 203 412 46 53 9 23212 7073 Mkarin freeware cabs leather- sofa ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional