Paul Wang
Paul Wang

Reputation: 83

why does mysql jdbc driver map mysql type smallint to int?

I found the code in ResultSetMetaData:

case Types.SMALLINT:
if (isUnsigned) {
    return "java.lang.Integer";
}
return "java.lang.Integer";

But in many document , smallint should be mapped to short.

Upvotes: 1

Views: 620

Answers (1)

Zelldon
Zelldon

Reputation: 5516

In the JDBC 1.0 specification the mapping from SMALLINT and TINYINT was defined to an integer. For backwards compatibility they use also in the new version these conversion.

See JDBC spec page 187:

http://download.oracle.com/otn-pub/jcp/jdbc-4_1-mrel-spec/jdbc4.1-fr-spec.pdf?AuthParam=1438159353_fad6114e3a27f1770c1a955db13718c2

Note – The JDBC 1.0 specification defined the Java object mapping for the SMALLINT and TINYINT JDBC types to be Integer. The Java language did not include the Byte and Short data types when the JDBC 1.0 specification was finalized. The mapping of SMALLINT and TINYINT to Integer is maintained to preserve backwards compatibility

The MySQL jdbc developers followed the jdbc spec and implemented the conversation from SMALLINT to an Integer.

Also you can see in the mysql doucmentation the conversion from smallint to int.

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

See the table:

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

MySQL Type Name              Return value of GetColumnClassNameReturned as Java Class                                       
SMALLINT[(M)] [UNSIGNED] SMALLINT [UNSIGNED]                          java.lang.Integer (regardless if UNSIGNED or not)

Upvotes: 1

Related Questions