Java Code Examples for java.sql.Clob#truncate()

The following examples show how to use java.sql.Clob#truncate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClobTruncateTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void checkTruncate (int size, Clob clob, int newSize)
        throws SQLException {
    assertEquals ("unexpected clob size", size, clob.length());
    clob.truncate (newSize);
    assertEquals ("truncate failed ", newSize, clob.length());
    //try once more
    clob.truncate (newSize/2);
    assertEquals ("truncate failed ", newSize/2, clob.length());
}
 
Example 2
Source File: ClobTruncateTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private void checkTruncate (int size, Clob clob, int newSize)
        throws SQLException {
    assertEquals ("unexpected clob size", size, clob.length());
    clob.truncate (newSize);
    assertEquals ("truncate failed ", newSize, clob.length());
    //try once more
    clob.truncate (newSize/2);
    assertEquals ("truncate failed ", newSize/2, clob.length());
}
 
Example 3
Source File: ClobTruncateTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
private void checkTruncate (int size, Clob clob, int newSize)
        throws SQLException {
    assertEquals ("unexpected clob size", size, clob.length());
    clob.truncate (newSize);
    assertEquals ("truncate failed ", newSize, clob.length());
    //try once more
    clob.truncate (newSize/2);
    assertEquals ("truncate failed ", newSize/2, clob.length());
}
 
Example 4
Source File: StringRegressionTest.java    From r-course with MIT License 4 votes vote down vote up
/**
 * Tests fix for BUG#11614 - StringUtils.getBytes() doesn't work when using
 * multibyte character encodings and a length in _characters_ is specified.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug11614() throws Exception {
    if (versionMeetsMinimum(4, 1)) {
        createTable("testBug11614",
                "(`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `text` TEXT NOT NULL," + "PRIMARY KEY(`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci");

        Properties props = new Properties();
        props.setProperty("characterEncoding", "utf8");

        Connection utf8Conn = null;

        try {
            utf8Conn = getConnectionWithProps(props);

            utf8Conn.createStatement().executeUpdate("INSERT INTO testBug11614  (`id`,`text`) values (1,'')");
            this.rs = utf8Conn.createStatement().executeQuery("SELECT `text` FROM testBug11614 WHERE id=1");
            assertTrue(this.rs.next());

            Clob c = this.rs.getClob(1);
            c.truncate(0);
            int blockSize = 8192;
            int sizeToTest = blockSize + 100;

            StringBuilder blockBuf = new StringBuilder(sizeToTest);

            for (int i = 0; i < sizeToTest; i++) {
                blockBuf.append('\u00f6');
            }

            String valueToTest = blockBuf.toString();

            c.setString(1, valueToTest);
            this.pstmt = utf8Conn.prepareStatement("UPDATE testBug11614 SET `text` = ? WHERE id=1");
            this.pstmt.setClob(1, c);
            this.pstmt.executeUpdate();
            this.pstmt.close();

            String fromDatabase = getSingleIndexedValueWithQuery(utf8Conn, 1, "SELECT `text` FROM testBug11614").toString();
            assertEquals(valueToTest, fromDatabase);
        } finally {
            if (utf8Conn != null) {
                utf8Conn.close();
            }

        }
    }
}
 
Example 5
Source File: StringRegressionTest.java    From Komondor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests fix for BUG#11614 - StringUtils.getBytes() doesn't work when using
 * multibyte character encodings and a length in _characters_ is specified.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug11614() throws Exception {
    if (versionMeetsMinimum(4, 1)) {
        createTable("testBug11614",
                "(`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `text` TEXT NOT NULL," + "PRIMARY KEY(`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci");

        Properties props = new Properties();
        props.setProperty("characterEncoding", "utf8");

        Connection utf8Conn = null;

        try {
            utf8Conn = getConnectionWithProps(props);

            utf8Conn.createStatement().executeUpdate("INSERT INTO testBug11614  (`id`,`text`) values (1,'')");
            this.rs = utf8Conn.createStatement().executeQuery("SELECT `text` FROM testBug11614 WHERE id=1");
            assertTrue(this.rs.next());

            Clob c = this.rs.getClob(1);
            c.truncate(0);
            int blockSize = 8192;
            int sizeToTest = blockSize + 100;

            StringBuilder blockBuf = new StringBuilder(sizeToTest);

            for (int i = 0; i < sizeToTest; i++) {
                blockBuf.append('\u00f6');
            }

            String valueToTest = blockBuf.toString();

            c.setString(1, valueToTest);
            this.pstmt = utf8Conn.prepareStatement("UPDATE testBug11614 SET `text` = ? WHERE id=1");
            this.pstmt.setClob(1, c);
            this.pstmt.executeUpdate();
            this.pstmt.close();

            String fromDatabase = getSingleIndexedValueWithQuery(utf8Conn, 1, "SELECT `text` FROM testBug11614").toString();
            assertEquals(valueToTest, fromDatabase);
        } finally {
            if (utf8Conn != null) {
                utf8Conn.close();
            }

        }
    }
}
 
Example 6
Source File: ResultSetIT.java    From snowflake-jdbc with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetClob() throws Throwable
{
  Connection connection = getConnection();
  Statement statement = connection.createStatement();
  statement.execute("create or replace table testClob(cola text)");
  statement.execute("insert into testClob values('hello world')");
  statement.execute("insert into testClob values('hello world1')");
  statement.execute("insert into testClob values('hello world2')");
  statement.execute("insert into testClob values('hello world3')");
  ResultSet resultSet = statement.executeQuery("select * from testClob");
  resultSet.next();
  // test reading Clob
  char[] chars = new char[100];
  Reader reader = resultSet.getClob(1).getCharacterStream();
  int charRead;
  charRead = reader.read(chars, 0, chars.length);
  assertEquals(charRead, 11);
  assertEquals("hello world", resultSet.getClob(1).toString());

  // test reading truncated clob
  resultSet.next();
  Clob clob = resultSet.getClob(1);
  assertEquals(clob.length(), 12);
  clob.truncate(5);
  reader = clob.getCharacterStream();

  charRead = reader.read(chars, 0, chars.length);
  assertEquals(charRead, 5);

  // read from input stream
  resultSet.next();
  final InputStream input = resultSet.getClob(1).getAsciiStream();

  Reader in = new InputStreamReader(input, StandardCharsets.UTF_8);
  charRead = in.read(chars, 0, chars.length);
  assertEquals(charRead, 12);

  statement.close();
  connection.close();
}
 
Example 7
Source File: StringRegressionTest.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Tests fix for BUG#11614 - StringUtils.getBytes() doesn't work when using
 * multibyte character encodings and a length in _characters_ is specified.
 * 
 * @throws Exception
 *             if the test fails.
 */
public void testBug11614() throws Exception {
    createTable("testBug11614",
            "(`id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `text` TEXT NOT NULL," + "PRIMARY KEY(`id`)) CHARACTER SET utf8 COLLATE utf8_general_ci");

    Properties props = new Properties();
    props.setProperty(PropertyKey.characterEncoding.getKeyName(), "utf8");

    Connection utf8Conn = null;

    try {
        utf8Conn = getConnectionWithProps(props);

        utf8Conn.createStatement().executeUpdate("INSERT INTO testBug11614  (`id`,`text`) values (1,'')");
        this.rs = utf8Conn.createStatement().executeQuery("SELECT `text` FROM testBug11614 WHERE id=1");
        assertTrue(this.rs.next());

        Clob c = this.rs.getClob(1);
        c.truncate(0);
        int blockSize = 8192;
        int sizeToTest = blockSize + 100;

        StringBuilder blockBuf = new StringBuilder(sizeToTest);

        for (int i = 0; i < sizeToTest; i++) {
            blockBuf.append('\u00f6');
        }

        String valueToTest = blockBuf.toString();

        c.setString(1, valueToTest);
        this.pstmt = utf8Conn.prepareStatement("UPDATE testBug11614 SET `text` = ? WHERE id=1");
        this.pstmt.setClob(1, c);
        this.pstmt.executeUpdate();
        this.pstmt.close();

        String fromDatabase = getSingleIndexedValueWithQuery(utf8Conn, 1, "SELECT `text` FROM testBug11614").toString();
        assertEquals(valueToTest, fromDatabase);
    } finally {
        if (utf8Conn != null) {
            utf8Conn.close();
        }

    }
}