0

I am in the process of creating code generator for my CRUD java web database application. In the making of its Form page, I want to be able to present ENUM field data type, into a combo box. So, having this defined in a MySQL script :

`Employment Status` enum('CPNS','PNS') COLLATE latin1_general_ci NOT NULL,

I want to make an Employment Status as Combo Box (using <SELEC/> html input type) with CPNS,PNS as its content.

How can I enumerate the content of that field from database using JDBC??? Many thanks!

PS : Some my ask, why do I want to create my own code generator??? Well, I think it will be very fun to create such, and add it to our wide choice of alternative in Java Web universe.. :)

swdev
  • 4,997
  • 8
  • 64
  • 106

2 Answers2

1
 DatabaseMetaData meta = conn.getMetaData();
 rsColumns = meta.getColumns(null, "%", "", "%");
/*getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)*/

 while (rsColumns.next()) 
{
      String columnType = rsColumns.getString("TYPE_NAME");
      String columnName = rsColumns.getString("COLUMN_NAME");
}

I think this can help you.

Ankur
  • 788
  • 6
  • 13
  • Nice! I already use meta.getPrimaryKeys(), and able to get all its primary keys. I'll try that, and report it here. Btw, this is my java web framework : http://code.google.com/p/recite18th. It may not the best solution there is, but I learn a lot in the process of making it! :) thanks – swdev Mar 28 '11 at 03:59
  • sorry, I just got time to continue this project. Now, it seems that your answer is not quite what I expected. Here I answered my own question. thanks – swdev Jul 25 '11 at 05:33
0

I just got this link : Re: JDBC and ENUM field types, I already test using show columns from table_x where field='employment status'. It show the enum content. But I still must parse it...

swdev
  • 4,997
  • 8
  • 64
  • 106
  • Currently, I will accept my own answer, and if you can show a better way, I'll accept yours :) thanks – swdev Jul 25 '11 at 05:36
  • 1
    I will use this code :`http://stackoverflow.com/questions/2350052/how-can-i-get-enum-possible-values-in-a-mysql-database` and already test this : `SELECT SUBSTRING(COLUMN_TYPE,6,length(SUBSTRING(COLUMN_TYPE,6))-1) as enum_content FROM information_schema.COLUMNS WHERE TABLE_NAME='pegawai' AND COLUMN_NAME='golongan_darah'` – swdev Jul 25 '11 at 05:46
  • The link given in the original answer is broken but the comment re using the INFORMATION_SCHEMA database is a useful pointer. – Clarius Apr 14 '22 at 10:41