Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jdbc: Get the SQL Type Name from java.sql.Type code

Tags:

java

jython

jdbc

I have an array with Field Names and jdbc Type codes. (Those int codes that you can find in

http://download.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.sql.Types.BIT

I use a level 4 Driver.

I can't figure out how to ask the driver for the corresponding SQL (DDL) Type names. It would be useful in jdbc and in native dialects.

I have
(CustomerId, 1) (CustomerName, -8)

and I want

(customerId, INT) (customerId, VARCHAR(200))

Where can I find functions that help me with that? I am using jdbc in jython via zxJDBC, so I can use all java and python DB API 2.0 functionality.

like image 453
AndreasT Avatar asked Jun 22 '11 09:06

AndreasT


People also ask

What is JDBCType?

public static final JDBCType BOOLEAN. Identifies the generic SQL type BOOLEAN .

What type of SQL is JDBC?

The JDBC driver provides support for the JDBC 4.0 API, which includes the SQLXML data type, and National (Unicode) data types, such as NCHAR, NVARCHAR, LONGNVARCHAR, and NCLOB.

How does JDBC handle the data types of Java in database?

The JDBC driver converts the Java data type to the appropriate JDBC type, before sending it to the database. It uses a default mapping for most data types. For example, a Java int is converted to an SQL INTEGER. Default mappings were created to provide consistency between drivers.


1 Answers

Java 8 and later: JDBCType & SQLType

With improvements in the API's, as of Java 8 and JDBC 4.2, we have JDBCType and SQLType, and in the same spirit as some of the other examples can be simply used as follows:

String typeName = JDBCType.valueOf(-5).getName(); 

But of course, why using the numeric types to begin with. Make a habit, and switch over from numeric's to the enum constants defined in JDBCType:

String typeName = JDBCType.BIGINT.getName(); 

et voilà!

However, that might not be enough to have something good enough for using in a DDL ... you might need to implement vendor specific translation. As an example, you might need to consider to translate VARCHAR to VARCHAR2 in the case of Oracle.

like image 144
YoYo Avatar answered Sep 21 '22 04:09

YoYo