Bug #10156 Unsigned SMALLINT treated as signed
Submitted: 25 Apr 2005 19:47 Modified: 27 Apr 2005 19:15
Reporter: o s Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.1.8 OS:Windows (w2k)
Assigned to: Mark Matthews CPU Architecture:Any

[25 Apr 2005 19:47] o s
Description:
I have an unsigned smallint(5) column that is being treated as signed by the JDBC driver. This results in an incorrect value of -32768 when selecting 32768. 

I am using MySQL server version 4.1.11. 

How to repeat:
1. Load the following data dump below.

2. Execute the following code:

        Connection conn = MySQLDAOFactory.getConnection();
        PreparedStatement stmt = conn
            .prepareStatement( "SELECT method FROM bug GROUP BY method;" );
        ResultSet rs = stmt.executeQuery();

        while ( rs.next() ) {
            System.out.println( rs.getInt( 1 ) );
        }

        conn.close();

The last value prints as -32768, when it should be 32768.

----------------BEGIN DATA DUMP:
-- MySQL dump 10.9
--
-- Host: localhost    Database: zzz
-- ------------------------------------------------------
-- Server version	4.1.11

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `bug`
--

DROP TABLE IF EXISTS `bug`;
CREATE TABLE `bug` (
  `scoreId` mediumint(8) unsigned NOT NULL auto_increment,
  `method` smallint(5) unsigned NOT NULL default '0',
  PRIMARY KEY  (`scoreId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `bug`
--

/*!40000 ALTER TABLE `bug` DISABLE KEYS */;
LOCK TABLES `bug` WRITE;
INSERT INTO `bug` VALUES (1,32768),(8,512),(9,4096),(11,2048),(13,2),(15,256),(20,1),(21,1024),(39,16),(113,128),(131,16384),(233,32),(390,8192),(1118,8),(2759,64),(4678,4);
UNLOCK TABLES;
/*!40000 ALTER TABLE `bug` ENABLE KEYS */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

---------------END DATA DUMP

Suggested fix:
Seems like a simple mixup between signed and unsigned.
[27 Apr 2005 19:15] Mark Matthews
Fixed for 3.1.9. This should be in tonight's nightly build after 00:00 GMT at http://downloads.mysql.com/snapshots.php.