Re: Mysql: desc table -> JDBC rs.getString("COLUMN_SIZE")

Mark Matthews <[email protected]> Mon, 18 Sep 2017 09:16:26 -0500
Newsgroups gmane.comp.db.mysql.java
Message-ID <[email protected]>

On 09/17/2017 10:33 AM, Michael Niedermair wrote:
> Hello,
> I have the following table in MySQL:
>
> mysql> desc VEREIN;
> +---------+-------------+------+-----+---------+----------------+
> | Field   | Type        | Null | Key | Default | Extra          |
> +---------+-------------+------+-----+---------+----------------+
> | V_ID    | int(11)     | NO   | PRI | NULL    | auto_increment |
> | Name    | varchar(50) | NO   |     | NULL    |                |
> | Strasse | varchar(50) | YES  |     | NULL    |                |
> | O_ID    | int(11)     | YES  | MUL | NULL    |                |
> +---------+-------------+------+-----+---------+----------------+
> 4 rows in set (0,00 sec)
>
> The field V_ID  has a length of 11.
>
> Now I want to output the metadata via JDBC.
>
> DatabaseMetaData meta = con.getMetaData();
> ResultSet rs = meta.getColumns(catalog,schema,"VEREIN",null);
> System.out.println("Spalte\tType\tGröße\tDezimal\tNULL\tPosition");
> while(rs.next()) {
>    System.out.print(rs.getString("COLUMN_NAME")+"\t"+
>      rs.getString("TYPE_NAME")+"\t"+ rs.getString("COLUMN_SIZE")+"\t"+
>      rs.getString("DECIMAL_DIGITS")+"\t"+
>      rs.getString("NULLABLE")+"\t"+ rs.getString("ORDINAL_POSITION"));
>    System.out.println();
> }
>
> The output for V_ID is:
> V_ID  INT  10  0  0  1
>
> Why is a length of 10 displayed for JDBC and 11 for MySQL?
>
> Only this line was found in the documentation.
> COLUMN_SIZE int => column size.

Hi,

It returns 10 because that is what the JDBC specification requires for a 
SQL type of INTEGER. MySQL has a concept of "display length", which 
doesn't actually influence the precision of the data stored. If you need 
the "11", you'll have to issue an I_S or "SHOW COLUMNS" query on your own.

   - Mark

-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/java