Java Forum / Databases / September 2007
How to know columns names in Derby database table
Luis Angel Fdez. Fdez. - 23 Sep 2007 08:54 GMT 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?
Thanks in advance.
Bye!
 Signature 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@gmail.com Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754
Roedy Green - 23 Sep 2007 10:24 GMT On Sun, 23 Sep 2007 09:54:50 +0200, "Luis Angel Fdez. Fdez." <laffdez@gmail.com> wrote, 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 databases 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.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Luis Angel Fdez. Fdez. - 23 Sep 2007 10:33 GMT Hi!
El Sun, 23 Sep 2007 09:24:40 +0000, Roedy Green escribió:
> DESCRIBE animals; -- look at column descriptions in the animals table I guess I will have to do it that way. But I hoped there was any other way.
Bye and thank you for your answer.
 Signature Ubuntu (gutsy) (kernel 2.6.22-11-386 i686) GNOME 2.20.0 Intel(R) Pentium(R) M processor 1.60GHz (600.000 MHz) up 12:53, 6 users Hattrick: Zanzabornín (1457021) X.1762 # Jabber: laffdez@gmail.com Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754
Roedy Green - 23 Sep 2007 11:41 GMT On Sun, 23 Sep 2007 09:54:50 +0200, "Luis Angel Fdez. Fdez." <laffdez@gmail.com> wrote, quoted or indirectly quoted someone who said :
> myRs = con.getMetaData().getColumns(null, null, "bornes", "%"); This is a quite tricky bit of code. I would 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 is not 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 is not used. DECIMAL_DIGITS int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not 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 isn't REF) SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF) SCOPE_TABLE String => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't 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 isn't 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 is not 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 is not 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 should not 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 should not 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()
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Luis Angel Fdez. Fdez. - 23 Sep 2007 12:10 GMT 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 don't know is if there is any better option for making it.
Bye!
 Signature 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@gmail.com Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754
Lew - 23 Sep 2007 17:12 GMT > Hi! > [quoted text clipped - 3 lines] > 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?
 Signature Lew
Luis Angel Fdez. Fdez. - 23 Sep 2007 18:03 GMT El Sun, 23 Sep 2007 12:12:50 -0400, Lew escribió:
Hi!
>> Vector<String> vNames = new Vector<String>(); > > Are you relying on the synchronization of Vector methods? I guess so :)
> Vector is kind of long in the tooth, and has non-Collection methods that > could cause maintenance difficulty down the road. Ok.
> Why did you choose Vector over, say, ArrayList? Well, there is no reason... I want to return a String[] and Vector was the only data type/class I could think with and add method. But after reading your post I think I'll avoid Vector. As you talk about it, I think I'll use ArrayList.
Thanks for you answer.
Bye!.
 Signature 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, 10:36 Hattrick: Zanzabornín (1457021) X.1762 # Jabber: laffdez@gmail.com Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754
Bjorn Abelli - 23 Sep 2007 19:21 GMT "Luis Angel Fdez. Fdez." ...
> El Sun, 23 Sep 2007 12:12:50 -0400, Lew escribió: > [quoted text clipped - 4 lines] > reading your post I think I'll avoid Vector. As you talk about it, I > think I'll use ArrayList. So, why did you not just simply use a String[] to begin with?
int i = 1; int numColumns = rsmd.getColumnCount(); String[] names = new String[numColumns];
while (i <= numColumns) { names[i-1] = rsmd.getColumnName(i); System.out.println(names[i-1]); i++; }
Luis Angel Fdez. Fdez. - 23 Sep 2007 20:18 GMT El Sun, 23 Sep 2007 20:21:56 +0200, Bjorn Abelli escribió:
> So, why did you not just simply use a String[] to begin with? > [quoted text clipped - 7 lines] > i++; > } Oops :(. Now I'm feeling bad :(. That's true, why am I not using the right way? :(
Thanks. Perhaps I would be happier if I didn't see your post ;-)
Bye!
 Signature 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, 12:53 Hattrick: Zanzabornín (1457021) X.1762 # Jabber: laffdez@gmail.com Sokker: C.D. Arrancatapinos (18088) IV.57 # Linux User #99754
Lew - 23 Sep 2007 20:44 GMT > El Sun, 23 Sep 2007 20:21:56 +0200, Bjorn Abelli escribió: > [quoted text clipped - 14 lines] > > Thanks. Perhaps I would be happier if I didn't see your post ;-) There really is no argument against using a List rather than an array, and many in favor of it.
A List lets you adapt the implementation, not worry much about the length of it, and is amenable to copying into other Collections, as well as to shared use among threads.
It may be a slight overkill for this particular use, but hardly a glaring error. It may also be appropriate in today's generics world always to think in terms of List where once one would have reached first for an array. The few times when it overshoots the mark may not add up against the consistency of a useful practice.
So go ahead and use a List, fella.
 Signature Lew
Dyreatnews@sun.com - 24 Sep 2007 11:04 GMT > Hi! > [quoted text clipped - 26 lines] > 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'll 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
 Signature dt
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|