Bug #25361 Only first digit of Integer gets stored in a data column of type char(30)
Submitted: 2 Jan 2007 11:38 Modified: 5 Jan 2007 10:10
Reporter: Neha Khaitan Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:5.0.27 OS:Windows (Windows XP Professional SP2)
Assigned to: CPU Architecture:Any

[2 Jan 2007 11:38] Neha Khaitan
Description:
I call setObject on a callable statement, which updates a column of type char(30).
I pass an java.lang.Integer object to the setObject. Only the first digit of the Integer gets stored.

Driver used: mysql connector/j 3.1.14
Database: mysql 5.0.27

How to repeat:
I do the following:
I have a table Char_Tab with the following DDL:

create table Char_Tab (COFFEE_NAME CHAR(30), NULL_VAL CHAR(30) NULL) !

There is a stored procedure ::

create procedure Char_In_Null(IN NULL_PARAM CHAR) begin update Char_Tab set NULL_VAL=NULL_PARAM; end; 

Connection conn = getConnection(); //code omitted.
    Statement statement = conn.createStatement();
    statement.executeUpdate("insert into Char_Tab values('Test Coffee', null)");
    CallableStatement cstmt = conn.prepareCall("{call Char_In_Null(?)}");
    Integer maxIntegerVal = new Integer(34);
    cstmt.setObject(1, maxIntegerVal);
    int i = cstmt.executeUpdate();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("Select NULL_VAL from Char_Tab");
    rs.next();
    Object object = rs.getObject(1);
    String rStringVal = object.toString();
    rStringVal = rStringVal.trim();

    if (!rStringVal.equals(maxStringVal)) {
        throw new Exception("Expected [" + maxIntegerVal + "] != Actual  [" + rStringVal + "]");
    }

The string rStringVal is expected to be 34. But we get 3 instead. I tried this for negative integer values as well. In the case of negative integer values only the "-" gets stored. For example if the integer is -34 only -3 gets stored.
[5 Jan 2007 10:10] Tonci Grgin
Hi Neha and thanks for your report. This is not a bug. You failed to declare length of your in (CHAR) parameter thus it defaults to 1. Try the same from mysql cl client and the result will be the same. Please declare length of CHAR parameter like this:
CREATE PROCEDURE testBug25361(IN NULL_PARAM CHAR(2))