Bug #22215 Exception on TINYINT unsigned column type when the value is 255

Submitted: 9 Sep 2006 23:12 Modified: 21 Sep 2006 22:47
Reporter: Susanta Datta Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.0.0 GA OS:Windows (Windows)
Assigned to: Todd Farmer CPU Architecture:Any

[9 Sep 2006 23:12] Susanta Datta
Description:
I get SQLException on TINYINT unsigned column type when the row value is 255.

java.sql.SQLException: '255' in column '1' is outside valid range for the
datatype TINYINT.
	at com.mysql.jdbc.ResultSet.throwRangeException(ResultSet.java:6559)

How to repeat:
CREATE TABLE Test ( i TINYINT UNSIGNED );
insert into Test values (255);

import java.sql.DriverManager;
import com.mysql.jdbc.*;
class Test
{
        public static void main(String args[]) {
                try {
                        Connection con;
                        Class.forName("com.mysql.jdbc.Driver").newInstance();
                        con = (Connection)
DriverManager.getConnection("jdbc:mysql://127.0.0.1/mydb", "myuser", "mypwd");
                        String selectStmt =
                                "SELECT i FROM Test";
                        PreparedStatement pstmt = (PreparedStatement)
con.prepareStatement(selectStmt);
                        ResultSet rs = (ResultSet) pstmt.executeQuery();
                        if(rs.next()) {
                                short i = rs.getShort(1);
                                System.out.println(i);
                        }
                } catch(Exception e) {
                        e.printStackTrace();
                }
        }
}

I get back an exception:
java.sql.SQLException: '255' in column '1' is outside valid range for the
datatype TINYINT.
	at com.mysql.jdbc.ResultSet.throwRangeException(ResultSet.java:6559)
	at com.mysql.jdbc.ResultSet.getNativeByte(ResultSet.java:7500)
	at com.mysql.jdbc.ResultSet.getNativeByte(ResultSet.java:7444)
	at com.mysql.jdbc.ResultSet.getNativeShort(ResultSet.java:7900)
	at com.mysql.jdbc.ResultSet.getNativeShort(ResultSet.java:7868)
	at com.mysql.jdbc.ResultSet.getShort(ResultSet.java:4620)
	at Test.main(Test.java:16)

Suggested fix:
I think bug is in Result.java line 3022
-------------------------------------------
		switch (field.getMysqlType()) {
		case MysqlDefs.FIELD_TYPE_TINY:
			byte valueAsByte = ((byte[]) this.thisRow[columnIndex])[0];
			
			if (!field.isUnsigned()) {
				return valueAsByte;
			}
------------------------------------------

It should be 
if (field.isUnsigned()) {
				return valueAsByte;
			}
[21 Sep 2006 22:47] Todd Farmer
Hi Susanta,

Thank you for your detailed bug report!  I was, however, unable to reproduce
this bug using the current Connector/J version (5.0.3).  Below is information on
my test case:

Configuration:
Windows XP
MySQL 5.0.18-nt
JRE 1.5.0_06
mysql-connector-java-5.0.3

Java code:

package mytest;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import testsuite.BaseTestCase;

public class Bug22215 extends BaseTestCase {

	public Bug22215(String name) {
		super(name);
	}

	public static void main(String[] args) {
		junit.textui.TestRunner.run(Bug22215.class);
	}

	public void testBug22215() throws Exception {
		createTestTable();

		Connection myConn = this.conn;
		PreparedStatement s = myConn.prepareStatement("SELECT i FROM BUG_22215");
		ResultSet rs = s.executeQuery();
		boolean returnedValue = false;
		while (rs.next()) {
			short sh = rs.getShort("i");
			super.assertEquals(255, sh);
			returnedValue = true;
		}
		super.assertTrue(returnedValue);
		super.dropTable("BUG_22215");
	}

	private void createTestTable() throws Exception {

		super.dropTable("BUG_22215");
		this.stmt
		  .executeUpdate("CREATE TABLE BUG_22215 ( i TINYINT UNSIGNED )");
		this.stmt
		  .executeUpdate("INSERT INTO BUG_22215 VALUES (255)");
	}
}

Please try upgrading to the current version of Connector/J and see if that
resolves your issue.
[21 Sep 2006 22:49] Todd Farmer
Test case

Attachment: Bug22215.java (text/x-java), 1.02 KiB.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.038 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.