본문 바로가기

DB/DB2

DB2 Data Type Java Mapping

Data types that map to database data types in Java applications

To write efficient JDBC and SQLJ programs, you need to use the best mappings between Java™ data types and table column data types.

The following tables summarize the mappings of Java data types to JDBC and database data types for a DB2® for Linux, UNIX, and WindowsDB2 for z/OS®, or IBM® Informix® system.

Data types for updating table columns

The following table summarizes the mappings of Java data types to database data types for PreparedStatement.setXXXor ResultSet.updateXXX methods in JDBC programs, and for input host expressions in SQLJ programs. When more than one Java data type is listed, the first data type is the recommended data type.

Table 1. Mappings of Java data types to database server data types for updating database tables
Java data typeDatabase data type
short, java.lang.ShortSMALLINT
boolean1, byte1, java.lang.Boolean, java.lang.ByteSMALLINT
int, java.lang.IntegerINTEGER
long, java.lang.LongBIGINT12
java.math.BigIntegerBIGINT11
java.math.BigIntegerCHAR(n)11,5
float, java.lang.FloatREAL
double, java.lang.DoubleDOUBLE
java.math.BigDecimalDECIMAL(p,s)2
java.math.BigDecimalDECFLOAT(n)3,4
java.lang.StringCHAR(n)5
java.lang.StringGRAPHIC(m)6
java.lang.StringVARCHAR(n)7
java.lang.StringVARGRAPHIC(m)8
java.lang.StringCLOB9
java.lang.StringXML10
byte[]CHAR(n) FOR BIT DATA5
byte[]VARCHAR(n) FOR BIT DATA7
byte[]BINARY(n)513
byte[]VARBINARY(n)713
byte[]BLOB9
byte[]ROWID
byte[]XML10
java.sql.BlobBLOB
java.sql.BlobXML10
java.sql.ClobCLOB
java.sql.ClobDBCLOB9
java.sql.ClobXML10
java.sql.DateDATE
java.sql.TimeTIME
java.sql.TimestampTIMESTAMP
java.io.ByteArrayInputStreamBLOB
java.io.StringReaderCLOB
java.io.ByteArrayInputStreamCLOB
java.io.InputStreamXML10
com.ibm.db2.jcc.DB2RowID (deprecated)ROWID
java.sql.RowIdROWID
com.ibm.db2.jcc.DB2Xml (deprecated)XML10
java.sql.SQLXMLXML10
java.util.DateCHAR(n)11,5
java.util.DateVARCHAR(n)11,5
java.util.DateDATE11
java.util.DateTIME11
java.util.DateTIMESTAMP11
java.util.CalendarCHAR(n)11,5
java.util.CalendarVARCHAR(n)11,5
java.util.CalendarDATE11
java.util.CalendarTIME11
java.util.CalendarTIMESTAMP11
Notes:
  1. For column updates, the data server has no exact equivalent for the Java boolean or byte data types, but the best fit is SMALLINT.
  2. p is the decimal precision and s is the scale of the table column.

    You should design financial applications so that java.math.BigDecimal columns map to DECIMAL columns. If you know the precision and scale of a DECIMAL column, updating data in the DECIMAL column with data in a java.math.BigDecimal variable results in better performance than using other combinations of data types.

  3. n=16 or n=34.
  4. DECFLOAT is valid for connections to DB2 Version 9.1 for z/OS, DB2 V9.5 for Linux, UNIX, and Windows, or DB2 for i V6R1, or later database servers. Use of DECFLOAT requires the SDK for Java Version 5 (1.5) or later.
  5. n<=255.
  6. m<=127.
  7. n<=32704.
  8. m<=16352.
  9. This mapping is valid only if the database server can determine the data type of the column.
  10. XML is valid for connections to DB2 Version 9.1 for z/OS or later database servers or DB2V9.1 for Linux, UNIX, and Windows or later database servers.
  11. This mapping is valid only for IBM Data Server Driver for JDBC and SQLJ version 4.13 or later.
  12. BIGINT is valid for connections to DB2 Version 9.1 for z/OS or later database servers, DB2V9.1 for Linux, UNIX, and Windows or later database servers, and all supported DB2 for i database servers.
  13. BINARY and VARBINARY are valid for connections to DB2 Version 9.1 for z/OS or later database servers or DB2 for i5/OS™ V5R3 and later database servers.

Data types for retrieval from table columns

The following table summarizes the mappings of DB2 or IBM Informix data types to Java data types forResultSet.getXXX methods in JDBC programs, and for iterators in SQLJ programs. This table does not list Java numeric wrapper object types, which are retrieved using ResultSet.getObject.

Table 2. Mappings of database server data types to Java data types for retrieving data from database server tables
SQL data typeRecommended Java data type or Java object typeOther supported Java data types
SMALLINTshortbyte, int, long, float, double, java.math.BigDecimal, boolean, java.lang.String
INTEGERintshort, byte, long, float, double, java.math.BigDecimal, boolean, java.lang.String
BIGINT5longint, short, byte, float, double, java.math.BigDecimal, boolean, java.lang.String
DECIMAL(p,s) or NUMERIC(p,s)java.math.BigDecimallong, int, short, byte, float, double, boolean, java.lang.String
DECFLOAT(n)1,2java.math.BigDecimallong, int, short, byte, float, double, java.math.BigDecimal, boolean, java.lang.String
REALfloatlong, int, short, byte, double, java.math.BigDecimal, boolean, java.lang.String
DOUBLEdoublelong, int, short, byte, float, java.math.BigDecimal, boolean, java.lang.String
CHAR(n)java.lang.Stringlong, int, short, byte, float, double, java.math.BigDecimal, boolean, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.io.InputStream, java.io.Reader
VARCHAR(n)java.lang.Stringlong, int, short, byte, float, double, java.math.BigDecimal, boolean, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.io.InputStream, java.io.Reader
CHAR(n) FOR BIT DATAbyte[]java.lang.String, java.io.InputStream, java.io.Reader
VARCHAR(n) FOR BIT DATAbyte[]java.lang.String, java.io.InputStream, java.io.Reader
BINARY(n)6byte[]None
VARBINARY(n)6byte[]None
GRAPHIC(m)java.lang.Stringlong, int, short, byte, float, double, java.math.BigDecimal, boolean, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.io.InputStream, java.io.Reader
VARGRAPHIC(m)java.lang.Stringlong, int, short, byte, float, double, java.math.BigDecimal, boolean, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.io.InputStream, java.io.Reader
CLOB(n)java.sql.Clobjava.lang.String
BLOB(n)java.sql.Blobbyte[]3
DBCLOB(m)No exact equivalent. Use java.sql.Clob. 
ROWIDjava.sql.RowIdbyte[], com.ibm.db2.jcc.DB2RowID (deprecated)
XML4java.sql.SQLXMLbyte[], java.lang.String, java.io.InputStream, java.io.Reader
DATEjava.sql.Datejava.sql.String, java.sql.Timestamp
TIMEjava.sql.Timejava.sql.String, java.sql.Timestamp
TIMESTAMPjava.sql.Timestampjava.sql.String, java.sql.Date, java.sql.Time, java.sql.Timestamp
Notes:
  1. n=16 or n=34.
  2. DECFLOAT is valid for connections to DB2 Version 9.1 for z/OS, DB2 V9.5 for Linux, UNIX, and Windows, or DB2 for i V6R1, or later database servers. Use of DECFLOAT requires the SDK for Java Version 5 (1.5) or later.
  3. This mapping is valid only if the database server can determine the data type of the column.
  4. XML is valid for connections to DB2 Version 9.1 for z/OS or later database servers or DB2V9.1 for Linux, UNIX, and Windows or later database servers.
  5. BIGINT is valid for connections to DB2 Version 9.1 for z/OS or later database servers, DB2V9.1 for Linux, UNIX, and Windows or later database servers, and all supported DB2 for i database servers.
  6. BINARY and VARBINARY are valid for connections to DB2 Version 9.1 for z/OS or later database servers or DB2 for i5/OS V5R3 or later database servers.

Data types for calling stored procedures and user-defined functions

The following table summarizes mappings of Java data types to JDBC data types and DB2 or IBM Informix data types for calling user-defined function and stored procedure parameters. The mappings of Java data types to JDBC data types are for CallableStatement.registerOutParameter methods in JDBC programs. The mappings of Java data types to database server data types are for parameters in stored procedure or user-defined function invocations.

If more than one Java data type is listed in the following table, the first data type is the recommended data type.

Table 3. Mappings of Java, JDBC, and SQL data types for calling stored procedures and user-defined functions
Java data typeJDBC data typeSQL data type1
boolean2, java.lang.BooleanBITSMALLINT
byte2, java.lang.ByteTINYINTSMALLINT
short, java.lang.ShortSMALLINTSMALLINT
int, java.lang.IntegerINTEGERINTEGER
long, java.lang.LongBIGINTBIGINT6
float, java.lang.FloatREALREAL
float, java.lang.FloatFLOATREAL
double, java.lang.DoubleDOUBLEDOUBLE
java.math.BigDecimalDECIMALDECIMAL
java.math.BigDecimaljava.types.OTHERDECFLOATn3
java.math.BigDecimalcom.ibm.db2.jcc.DB2Types.DECFLOATDECFLOATn3
java.lang.StringCHARCHAR
java.lang.StringCHARGRAPHIC
java.lang.StringVARCHARVARCHAR
java.lang.StringVARCHARVARGRAPHIC
java.lang.StringLONGVARCHARVARCHAR
java.lang.StringVARCHARCLOB
java.lang.StringLONGVARCHARCLOB
java.lang.StringCLOBCLOB
byte[]BINARYCHAR FOR BIT DATA
byte[]VARBINARYVARCHAR FOR BIT DATA
byte[]BINARYBINARY5
byte[]VARBINARYVARBINARY5
byte[]LONGVARBINARYVARCHAR FOR BIT DATA
byte[]VARBINARYBLOB4
byte[]LONGVARBINARYBLOB4
java.sql.DateDATEDATE
java.sql.TimeTIMETIME
java.sql.TimestampTIMESTAMPTIMESTAMP
java.sql.BlobBLOBBLOB
java.sql.ClobCLOBCLOB
java.sql.ClobCLOBDBCLOB
java.io.ByteArrayInputStreamNoneBLOB
java.io.StringReaderNoneCLOB
java.io.ByteArrayInputStreamNoneCLOB
com.ibm.db2.jcc.DB2RowID (deprecated)com.ibm.db2.jcc.DB2Types.ROWIDROWID
java.sql.RowIdjava.sql.Types.ROWIDROWID
Notes:
  1. DB2 for z/OS stored procedure or user-defined function parameter cannot have the XML data type.
  2. A stored procedure or user-defined function that is defined with a SMALLINT parameter can be invoked with a boolean or byte parameter. However, this is not recommended.
  3. DECFLOAT parameters in Java routines are valid only for connections to DB2 Version 9.1 for z/OS or later database servers. DECFLOAT parameters in Java routines are not supported for connections to for Linux, UNIX, and Windows or DB2 for i. Use of DECFLOAT requires the SDK for Java Version 5 (1.5) or later.
  4. This mapping is valid only if the database server can determine the data type of the column.
  5. BINARY and VARBINARY are valid for connections to DB2 Version 9.1 for z/OS or later database servers or DB2 for i5/OS V5R3 and later database servers.
  6. BIGINT is valid for connections to DB2 Version 9.1 for z/OS or later database servers, DB2V9.1 for Linux, UNIX, and Windows or later database servers, and all supported DB2 for i database servers.

Data types in Java stored procedures and user-defined functions

The following table summarizes mappings of the SQL parameter data types in a CREATE PROCEDURE or CREATE FUNCTION statement to the data types in the corresponding Java stored procedure or user-defined function method.

For DB2 for Linux, UNIX, and Windows, if more than one Java data type is listed for an SQL data type, only thefirst Java data type is valid.

For DB2 for z/OS, if more than one Java data type is listed, and you use a data type other than the first data type as a method parameter, you need to include a method signature in the EXTERNAL clause of your CREATE PROCEDURE or CREATE FUNCTION statement that specifies the Java data types of the method parameters.

Table 4. Mappings of SQL data types in a CREATE PROCEDURE or CREATE FUNCTION statement to data types in the corresponding Java stored procedure or user-defined function program
SQL data type in CREATE PROCEDURE or CREATE FUNCTION1Data type in Java stored procedure or user-defined function method2
SMALLINTshort, java.lang.Integer
INTEGERint, java.lang.Integer
BIGINT3long, java.lang.Long
REALfloat, java.lang.Float
DOUBLEdouble, java.lang.Double
DECIMALjava.math.BigDecimal
DECFLOAT4java.math.BigDecimal
CHARjava.lang.String
VARCHARjava.lang.String
CHAR FOR BIT DATAbyte[]
VARCHAR FOR BIT DATAbyte[]
BINARY5byte[]
VARBINARY5byte[]
DATEjava.sql.Date
TIMEjava.sql.Time
TIMESTAMPjava.sql.Timestamp
BLOBjava.sql.Blob
CLOBjava.sql.Clob
DBCLOBjava.sql.Clob
ROWIDjava.sql.Types.ROWID
Notes:
  1. DB2 for z/OS stored procedure or user-defined function parameter cannot have the XML data type.
  2. For a stored procedure or user-defined function on a DB2 for Linux, UNIX, and Windowsserver, only the first data type is valid.
  3. BIGINT is valid for connections to DB2 Version 9.1 for z/OS or later database servers orDB2 V9.1 for Linux, UNIX, and Windows or later database servers.
  4. DECFLOAT parameters in Java routines are valid only for connections to DB2 Version 9.1 for z/OS or later database servers. DECFLOAT parameters in Java routines are not supported for connections to for Linux, UNIX, and Windows or DB2 for i. Use of DECFLOAT requires the SDK for Java Version 5 (1.5) or later.
  5. BINARY and VARBINARY are valid for connections to DB2 Version 9.1 for z/OS or later database servers.


'DB > DB2' 카테고리의 다른 글

DB2 DATE FORMAT  (0) 2016.10.12
DB2 함수 모음  (0) 2014.03.05
DB2 SQL error codes  (0) 2014.02.26