I am using jdbc CallableStatement to execute procedures in a MySQL database. While writing unit tests, I came across unusual behavior regarding the registerOutParameter method.
Used SQL Procedures
PROCEDURE TEST_DOUBLE( OUT val DOUBLE ) BEGIN SET val = 2.5; END
PROCEDURE TEST_TEXT( OUT val TEXT ) BEGIN SET val = 'test'; END
Test
statement = connection.prepareCall( "CALL TEST_DOUBLE(?)" );
statement.registerOutParameter( "val" , java.sql.Types.INTEGER );
statement.execute();
statement.getInt( "val" ); //Integer 2
statement.getDouble( "val" ); //Double 2.5
statement.getString( "val" ); //String "2.5"
statement = connection.prepareCall( "CALL TEST_TEXT(?)" );
statement.registerOutParameter( "val" , java.sql.Types.INTEGER );
statement.execute();
statement.getInt( "val" ); //Invalid value for getInt
statement.getDouble( "val" ); //Invalid value for getDouble
statement.getString( "val" ); //String "test"
statement = connection.prepareCall( "CALL TEST_TEXT(?)" );
statement.registerOutParameter( "val" , 0 );
statement.execute();
statement.getInt( "val" ); //Invalid value for getInt
statement.getDouble( "val" ); //Invalid value for getDouble
statement.getString( "val" ); //String "test"
etc.
My point is the values of java.sql.Types. *, passed to registerOutParameter do not seem to do anything ... No matter what type of sql I pass, it all depends on calling the correct getter method and the actual value returned by the database.
Can I just go through 0 everywhere and still work fine? Why is this parameter passed then? Or maybe this database is related, but does it make sense for other databases?