Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions

  




 

 

26.3.3.3. Java, JDBC and MySQL Types

MySQL Connector/J is flexible in the way it handles conversions between MySQL data types and Java data types.

In general, any MySQL data type can be converted to a java.lang.String, and any numerical type can be converted to any of the Java numerical types, although round-off, overflow, or loss of precision may occur.

Starting with Connector/J 3.1.0, the JDBC driver will issue warnings or throw DataTruncation exceptions as is required by the JDBC specification unless the connection was configured not to do so by using the property "jdbcCompliantTruncation" and setting it to "false".

The conversions that are always guaranteed to work are listed in the following table:

Table 26.2. Conversion Table

These MySQL Data Types Can always be converted to these Java types
CHAR, VARCHAR, BLOB, TEXT, ENUM, and SET java.lang.String, java.io.InputStream, java.io.Reader, java.sql.Blob, java.sql.Clob
FLOAT, REAL, DOUBLE PRECISION, NUMERIC, DECIMAL, TINYINT, SMALLINT, MEDIUMINT, INTEGER, BIGINT java.lang.String, java.lang.Short, java.lang.Integer, java.lang.Long, java.lang.Double, java.math.BigDecimal

Note

round-off, overflow or loss of precision may occur if you choose a Java numeric data type that has less precision or capacity than the MySQL data type you are converting to/from.

DATE, TIME, DATETIME, TIMESTAMP java.lang.String, java.sql.Date, java.sql.Timestamp

The ResultSet.getObject() method uses the following type conversions between MySQL and Java types, following the JDBC specification where appropriate:

Table 26.3. MySQL Types to Java Types for ResultSet.getObject()

MySQL Type Name Returned as Java Class
BIT(1) (new in MySQL-5.0) java.lang.Boolean
BIT( > 1) (new in MySQL-5.0) byte[]
TINYINT java.lang.Boolean if the configuration property "tinyInt1isBit" is set to "true" (the default) and the storage size is "1", or java.lang.Integer if not.
BOOL , BOOLEAN See TINYINT, above as these are aliases for TINYINT(1), currently.
SMALLINT[(M)] [UNSIGNED] java.lang.Integer (regardless if UNSIGNED or not)
MEDIUMINT[(M)] [UNSIGNED] java.lang.Integer, if UNSIGNED java.lang.Long
INT,INTEGER[(M)] [UNSIGNED] java.lang.Integer, if UNSIGNED java.lang.Long
BIGINT[(M)] [UNSIGNED] java.lang.Long, if UNSIGNED java.math.BigInteger
FLOAT[(M,D)] java.lang.Float
DOUBLE[(M,B)] java.lang.Double
DECIMAL[(M[,D])] java.math.BigDecimal
DATE java.sql.Date
DATETIME java.sql.Timestamp
TIMESTAMP[(M)] java.sql.Timestamp
TIME java.sql.Time
YEAR[(2|4)] java.sql.Date (with the date set two January 1st, at midnight)
CHAR(M) java.lang.String (unless the character set for the column is BINARY, then byte[] is returned.
VARCHAR(M) [BINARY] java.lang.String (unless the character set for the column is BINARY, then byte[] is returned.
BINARY(M) byte[]
VARBINARY(M) byte[]
TINYBLOB byte[]
TINYTEXT java.lang.String
BLOB byte[]
TEXT java.lang.String
MEDIUMBLOB byte[]
MEDIUMTEXT java.lang.String
LONGBLOB byte[]
LONGTEXT java.lang.String
ENUM('value1','value2',...) java.lang.String
SET('value1','value2',...) java.lang.String


 
 
  Published under the terms of the GNU General Public License Design by Interspire