📄 lobstreams.java
字号:
} } /** * Tests the ClobOutputStream.write(int b) method **/ private static void testClobAsciiWrite1Param(Connection conn) { try { System.out.println(START + "testClobAsciiWrite1Param"); PreparedStatement stmt3 = conn.prepareStatement( "SELECT c FROM testBlobX1 WHERE a = 1"); ResultSet rs3 = stmt3.executeQuery(); rs3.next(); Clob clob = rs3.getClob(1); File file = new File(fileName[1]); fileLength = file.length(); InputStream fileIn = new FileInputStream(file); if (clob != null) { int buffer; OutputStream outstream = clob.setAsciiStream(1L); while ((buffer = fileIn.read()) != -1) { outstream.write(buffer); } outstream.close(); fileIn.close(); PreparedStatement stmt4 = conn.prepareStatement( "UPDATE testBlobX1 SET c = ? WHERE a = 1"); stmt4.setClob(1, clob); stmt4.executeUpdate(); stmt4.close(); } else { System.out.println("FAIL -- clob is NULL"); } rs3.close(); rs3 = stmt3.executeQuery(); if (rs3.next()) { long new_length = rs3.getClob(1).length(); if (new_length != fileLength) { System.out.println( "FAIL -- wrong clob length; original: " + fileLength + " clob length: " + new_length); } else { // Check contents ... InputStream fStream = new FileInputStream(file); InputStream lStream = rs3.getClob(1).getAsciiStream(); if (!compareLob2File(fStream, lStream)) System.out.println("FAIL - Clob and file contents do not match"); fStream.close(); lStream.close(); } } else { System.out.println("FAIL -- clob not found"); } rs3.close(); stmt3.close(); System.out.println("testClobAsciiWrite1Param finished"); } catch (SQLException e) { TestUtil.dumpSQLExceptions(e); } catch (Throwable e) { if (debug) e.printStackTrace(); } } /** * Tests the ClobWriter.write(char cbuf[], int off, int len) method **/ private static void testClobCharacterWrite3ParamChar(Connection conn) { try { System.out.println(START + "testClobCharacterWrite3ParamChar"); PreparedStatement stmt3 = conn.prepareStatement( "SELECT c FROM testBlobX1 WHERE a = 1"); ResultSet rs3 = stmt3.executeQuery(); rs3.next(); Clob clob = rs3.getClob(1); char[] testdata = unicodeTestString.toCharArray(); if (clob != null) { Writer clobWriter = clob.setCharacterStream(1L); clobWriter.write(testdata, 0, testdata.length); clobWriter.close(); PreparedStatement stmt4 = conn.prepareStatement( "UPDATE testBlobX1 SET c = ? WHERE a = 1"); stmt4.setClob(1, clob); stmt4.executeUpdate(); stmt4.close(); } else { System.out.println("FAIL -- clob is NULL"); } rs3.close(); rs3 = stmt3.executeQuery(); if (rs3.next()) { long new_length = rs3.getClob(1).length(); if (new_length != testdata.length) { System.out.println( "FAIL -- wrong clob length; original: " + testdata.length + " clob length: " + new_length); } else { // Check contents ... Reader lStream = rs3.getClob(1).getCharacterStream(); if (!compareClobReader2CharArray(testdata, lStream)) System.out.println("FAIL - Clob and buffer contents do not match"); lStream.close(); } } else { System.out.println("FAIL -- clob not found"); } rs3.close(); stmt3.close(); System.out.println("testClobCharacterWrite3ParamChar finished"); } catch (SQLException e) { TestUtil.dumpSQLExceptions(e); } catch (Throwable e) { if (debug) e.printStackTrace(); } } /** * Tests the ClobWriter.write(String str, int off, int len) method **/ private static void testClobCharacterWrite3ParamString(Connection conn) { try { System.out.println(START + "testClobCharacterWrite3ParamString"); PreparedStatement stmt3 = conn.prepareStatement( "SELECT c FROM testBlobX1 WHERE a = 1"); ResultSet rs3 = stmt3.executeQuery(); rs3.next(); Clob clob = rs3.getClob(1); if (clob != null) { Writer clobWriter = clob.setCharacterStream(1L); clobWriter.write(unicodeTestString, 0, unicodeTestString.length()); clobWriter.close(); PreparedStatement stmt4 = conn.prepareStatement( "UPDATE testBlobX1 SET c = ? WHERE a = 1"); stmt4.setClob(1, clob); stmt4.executeUpdate(); stmt4.close(); } else { System.out.println("FAIL -- clob is NULL"); } rs3.close(); rs3 = stmt3.executeQuery(); if (rs3.next()) { long new_length = rs3.getClob(1).length(); if (new_length != unicodeTestString.length()) { System.out.println( "FAIL -- wrong clob length; original: " + unicodeTestString.length() + " clob length: " + new_length); } else { // Check contents ... Reader lStream = rs3.getClob(1).getCharacterStream(); if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream)) System.out.println("FAIL - Clob and buffer contents do not match"); lStream.close(); } } else { System.out.println("FAIL -- clob not found"); } rs3.close(); stmt3.close(); System.out.println("testClobCharacterWrite3ParamString finished"); } catch (SQLException e) { TestUtil.dumpSQLExceptions(e); } catch (Throwable e) { if (debug) e.printStackTrace(); } } /** * Tests the ClobWriter.write(String str) method **/ private static void testClobCharacterWrite1ParamString(Connection conn) { try { System.out.println(START + "testClobCharacterWrite1ParamString"); PreparedStatement stmt3 = conn.prepareStatement( "SELECT c FROM testBlobX1 WHERE a = 1"); ResultSet rs3 = stmt3.executeQuery(); rs3.next(); Clob clob = rs3.getClob(1); if (clob != null) { Writer clobWriter = clob.setCharacterStream(1L); clobWriter.write(unicodeTestString); clobWriter.close(); PreparedStatement stmt4 = conn.prepareStatement( "UPDATE testBlobX1 SET c = ? WHERE a = 1"); stmt4.setClob(1, clob); stmt4.executeUpdate(); stmt4.close(); } else { System.out.println("FAIL -- clob is NULL"); } rs3.close(); rs3 = stmt3.executeQuery(); if (rs3.next()) { long new_length = rs3.getClob(1).length(); if (new_length != unicodeTestString.length()) { System.out.println( "FAIL -- wrong clob length; original: " + unicodeTestString.length() + " clob length: " + new_length); } else { // Check contents ... Reader lStream = rs3.getClob(1).getCharacterStream(); if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream)) System.out.println("FAIL - Clob and buffer contents do not match"); lStream.close(); } } else { System.out.println("FAIL -- clob not found"); } rs3.close(); stmt3.close(); System.out.println("testClobCharacterWrite1ParamString finished"); } catch (SQLException e) { TestUtil.dumpSQLExceptions(e); } catch (Throwable e) { if (debug) e.printStackTrace(); } } /** * Tests the ClobWriter.write(int c) method **/ private static void testClobCharacterWrite1Char(Connection conn) { try { System.out.println(START + "testClobCharacterWrite1Char"); PreparedStatement stmt3 = conn.prepareStatement( "SELECT c FROM testBlobX1 WHERE a = 1"); ResultSet rs3 = stmt3.executeQuery(); rs3.next(); Clob clob = rs3.getClob(1); char testchar = 'a'; if (clob != null) { Writer clobWriter = clob.setCharacterStream(1L); clobWriter.write(testchar); clobWriter.close(); PreparedStatement stmt4 = conn.prepareStatement( "UPDATE testBlobX1 SET c = ? WHERE a = 1"); stmt4.setClob(1, clob); stmt4.executeUpdate(); stmt4.close(); } else { System.out.println("FAIL -- clob is NULL"); } rs3.close(); rs3 = stmt3.executeQuery(); if (rs3.next()) { long new_length = rs3.getClob(1).length(); Clob fish = rs3.getClob(1); if (new_length != 1) { System.out.println( "FAIL -- wrong clob length; original: " + 1 + " clob length: " + new_length); } else { // Check contents ... Reader lStream = rs3.getClob(1).getCharacterStream(); char clobchar = (char) lStream.read(); if (clobchar != testchar) System.out.println("FAIL - fetched Clob and original contents do not match"); lStream.close(); } } else { System.out.println("FAIL -- clob not found"); } rs3.close(); stmt3.close(); System.out.println("testClobCharacterWrite1Char finished"); } catch (SQLException e) { TestUtil.dumpSQLExceptions(e); } catch (Throwable e) { if (debug) e.printStackTrace(); } } private static boolean compareLob2File(InputStream fStream, InputStream lStream) { byte[] fByte = new byte[1024]; byte[] lByte = new byte[1024]; int lLength = 0, fLength = 0; String fString, lString; try { do { fLength = fStream.read(fByte, 0, 1024); lLength = lStream.read(lByte, 0, 1024); if (!java.util.Arrays.equals(fByte, lByte)) return false; } while (fLength > 0 && lLength > 0); fStream.close(); lStream.close(); } catch (Throwable e) { if (debug) e.printStackTrace(); } return true; } private static boolean compareClobReader2CharArray(char[] cArray, Reader charReader) { char[] clobChars = new char[cArray.length]; int readChars = 0; int totalCharsRead = 0; try { do { readChars = charReader.read(clobChars, totalCharsRead, cArray.length - totalCharsRead); if (readChars != -1) totalCharsRead += readChars; } while (readChars != -1 && totalCharsRead < cArray.length); charReader.close(); if (!java.util.Arrays.equals(cArray, clobChars)) return false; } catch (Throwable e) { if (debug) e.printStackTrace(); } return true; } private static void cleanUp(Connection conn) throws SQLException { String[] testObjects = {"table testBlobX1"}; Statement cleanupStmt = conn.createStatement(); TestUtil.cleanUpTest(cleanupStmt, testObjects); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -