private void testClob(int length) throws Exception {
    Random r = new Random(length);
    char[] data = new char[length];

    // Unicode problem:
    // The UCS code values 0xd800-0xdfff (UTF-16 surrogates)
    // as well as 0xfffe and 0xffff (UCS non-characters)
    // should not appear in conforming UTF-8 streams.
    // (String.getBytes("UTF-8") only returns 1 byte for 0xd800-0xdfff)
    for (int i = 0; i < length; i++) {
      char c;
      do {
        c = (char) r.nextInt();
      } while (c >= 0xd800 && c <= 0xdfff);
      data[i] = c;
    }
    Clob c = conn.createClob();
    Writer out = c.setCharacterStream(1);
    out.write(data, 0, data.length);
    out.close();
    stat.execute("delete from test");
    PreparedStatement prep = conn.prepareStatement("insert into test values(?, ?)");

    prep.setInt(1, 1);
    prep.setClob(2, c);
    prep.execute();

    c = conn.createClob();
    c.setString(1, new String(data));
    prep.setInt(1, 2);
    prep.setClob(2, c);
    prep.execute();

    prep.setInt(1, 3);
    prep.setCharacterStream(2, new StringReader(new String(data)));
    prep.execute();

    prep.setInt(1, 4);
    prep.setCharacterStream(2, new StringReader(new String(data)), -1);
    prep.execute();

    NClob nc;
    nc = conn.createNClob();
    nc.setString(1, new String(data));
    prep.setInt(1, 5);
    prep.setNClob(2, nc);
    prep.execute();

    prep.setInt(1, 5);
    prep.setNClob(2, new StringReader(new String(data)));
    prep.execute();

    prep.setInt(1, 6);
    prep.setNClob(2, new StringReader(new String(data)), -1);
    prep.execute();

    prep.setInt(1, 7);
    prep.setNString(2, new String(data));
    prep.execute();

    ResultSet rs;
    rs = stat.executeQuery("select * from test");
    rs.next();
    Clob c2 = rs.getClob(2);
    assertEquals(length, c2.length());
    String s = c.getSubString(1, length);
    String s2 = c2.getSubString(1, length);
    while (rs.next()) {
      c2 = rs.getClob(2);
      assertEquals(length, c2.length());
      s2 = c2.getSubString(1, length);
      assertEquals(s, s2);
    }
  }