Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4234356

Large strings being truncated.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.1.7
    • 1.1.7
    • core-libs
    • 005
    • x86
    • windows_nt

        9646
           The preparedStatement.setString() method truncates strings that are
           longer than 254 bytes.

           The testcase completes sucessfully when the string length is 254 and
           fails when it is >=255.

        --------------------------------------------------------------------------
        import java.sql.*;

        /**
         * preparedStatement.setString() truncate string is its lenght > 254.
         *
         * usage: LargeInsertProblemInJDBC_ODBCBridige DBURL STR_LEN
         *
         * DBURL -- database to use
         * STR_LEN -- length of string to insert
         *
         * It seems that JdbcOdbc bridge are convert setString() to SQL_CHAR which has
         * a length limit of 254. According to JDBC, the
         * preparedStatement.setString(int parameterIndex, String x) set a parameter
         * to a Java String value. The driver should convert this to a SQL VARCHAR
         * or LONGVARCHAR value,
         */

        public class LargeInsert
        {
          private String url = null;
          private int len = 0;

         
          public LargeInsert( String dbUrl, String insertLen )
          {
            url = dbUrl;
            try {
        len = Integer.parseInt( insertLen );
        if ( len > 2000 ) len = 2000;
            }
            catch( NumberFormatException e ) {
        len = 2000;
        }
          }

         
          public void doIt()
          {
            String createTableStmt = "create table mytable (longestr varchar(2000))";
            String insertStmt = "insert into mytable values( ? )";
            String drobTableStmt = "drop table mytable";
            boolean tableCreated = false;
            Connection con = null;

            try {
        //
        // Load the driver and get a connection
        //

        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
        con = DriverManager.getConnection( "jdbc:odbc:PMB2", "pmb", "coalport" );
        System.out.println( "got a connection" );

        //
        // Create the table
        //

        Statement stmt = con.createStatement();
        stmt.executeUpdate( createTableStmt );
        stmt.close();
        con.commit();
        tableCreated = true;
        System.out.println( "table created" );

        //
        // Attempt an insert of a large string
        //

        PreparedStatement pstmt = con.prepareStatement( insertStmt );
        char buffer[] = new char[ len ];
        for( int i = 0; i < len; ++i ) { buffer[i] = 'z'; }
        String bufferStr = new String( buffer );
        pstmt.setString( 1, bufferStr );
        pstmt.executeUpdate();
        pstmt.close();
        con.commit();
        System.out.println( "insert complete" );
        }
            catch( Exception e) {
        System.out.println( "Test failed with:" );
        e.printStackTrace();
            }
            finally {
        if ( tableCreated) {
        try {
        //
        // Drop the table
        //

        Statement stmt = con.createStatement();
        stmt.executeUpdate( drobTableStmt );
        stmt.close();
        con.commit();
        System.out.println( "table dropped" );

        //
        // Close the connection
        //

        con.close();
        System.out.println( "connection closed" );
        }
        catch( Exception e ) {
        System.out.println( "cleanup failed with:" );
        e.printStackTrace();
        }
        }
        }
          }


          public static void main( String args[] )
          {
            if ( args.length < 2 ) {
        System.out.println( "usage: LargeInsertProblemInJDBC_ODBCBridige DBURL STR_LEN" );
            } else {
        LargeInsert test = new LargeInsert( args[0], args[1] );
        test.doIt();
            }
          }
        }

              duke J. Duke
              miflemi Mick Fleming
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: