Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map standard Java types to SQL types?

I want to write a program, which creates RDBMS-tables through JDBC automatically. To add columns to a table, I need to know the name of the column and the column's RDBMS datatype. I already have the names of the columns. I don't have the RDBMS types for the columns, but I have Java types for those column. So I need to map those Java types to RDBMS datatypes. The Java type can be one of the following:

  • primitve types
  • wrapper types of primitive types
  • String

  • So my question is: How to map those java types to RDBMS types?

  • Is there a part of JDBC or library that already handles this mapping?
  • Are there any classes which can help me partially?

Especially I am working with PostgreSQL. So if there is no genenic way to do it, it would be important for the moment to get it running with PG.

Thanks in advance

like image 371
user573215 Avatar asked Feb 15 '11 19:02

user573215


People also ask

Which of the following Java types is mapped to the SQL data type bit?

In Derby, the java. sql. Types are mapped to SQL data types.

How can I use Java and SQL together?

STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.

What is the Java type mapping for the SQL type CLOB?

Blob is the mapping for the SQL BLOB (binary large object) type; java. sql. Clob is the mapping for the SQL CLOB (character large object) type. BLOB and CLOB objects are collectively referred to as LOBs (large objects).

What SQL data type corresponds with the string type in Java?

The Java String and byte[ ] datatypes correspond to SQL varchar and varbinary, where the maximum length value of 16K bytes is defined by Adaptive Server.


2 Answers

Well, there there's always the java.sql.Types class which contains the generic SQL type mappings, but you'd be better served using something like Hibernate to do all of this for you.

like image 53
Steven Fines Avatar answered Sep 21 '22 18:09

Steven Fines


getTypeInfo() is intended to get the driver's view on which (native) DBMS type should be mapped to which JDBC type. But these mappings aren't always precise so you will need to find some way of detecting the "best match"

like image 20
a_horse_with_no_name Avatar answered Sep 24 '22 18:09

a_horse_with_no_name