db-derby-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dag.wan...@oracle.com (Dag H. Wanvik)
Subject silent truncation, correct?
Date Tue, 08 May 2012 16:40:44 GMT


I see that Derby silently truncates a too long string argument to a
stored procedure with a formal argument of VARCHAR(n), cf. enclosed
program. Is this correct behavior? I'll try to grok the standard on
this, but it looks suspect to me.. The program prints 5 at the
"cs.execute", but throws an exception as expected at the "ps.execute".


package silentvarcharargtruncation;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class SilentVarcharArgTruncation {

    public static void main(String[] args) throws SQLException {
        Connection c = DriverManager.getConnection("jdbc:derby:wombat;create=true");
        Statement s = c.createStatement();
        try {
            s.executeUpdate("drop table t");
        } catch (SQLException e) {}
        try {
            s.executeUpdate("drop procedure p");
        } catch (SQLException e) {}
        s.executeUpdate("create table t(v varchar(5))");
        s.executeUpdate("create procedure p (a varchar(5)) modifies sql data " + 
            "external name 'silentvarcharargtruncation.SilentVarcharArgTruncation.testLength'
" + 
            "language java parameter style java");
        CallableStatement cs = c.prepareCall("call p(?)");
        cs.setString(1, "123456");
        // This silently truncates
        PreparedStatement ps = c.prepareStatement("insert into t values(?)");
        ps.setString(1, "123456");
        // This does not truncate, throws
    public static void testLength (String s) throws SQLException {

View raw message