📄 lobtest.java
字号:
//jTDS JDBC Driver for Microsoft SQL Server and Sybase//Copyright (C) 2004 The jTDS Project////This library is free software; you can redistribute it and/or//modify it under the terms of the GNU Lesser General Public//License as published by the Free Software Foundation; either//version 2.1 of the License, or (at your option) any later version.////This library is distributed in the hope that it will be useful,//but WITHOUT ANY WARRANTY; without even the implied warranty of//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU//Lesser General Public License for more details.////You should have received a copy of the GNU Lesser General Public//License along with this library; if not, write to the Free Software//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//package net.sourceforge.jtds.test;import java.io.*;import java.sql.*;import java.util.*;//// MJH - Changes for new jTDS version// Amended many lines such as those in testBlobSetNull6// where ResultSet variable rs was used when rs2 is actually required.// Amazed old version did not fail also as rs was closed!// Changed get / set UnicodeStream tests to align with standard.///** * @version $Id: LOBTest.java,v 1.27 2005/04/17 18:41:29 alin_sinpalean Exp $ */public class LOBTest extends TestBase { private static final int LOB_LENGTH = 8000; private static final byte[] blobData = new byte[LOB_LENGTH]; private static final byte[] newBlobData = new byte[LOB_LENGTH]; private static final String clobData; private static final String newClobData; static { for (int i = 0; i < blobData.length; i++) { blobData[i] = (byte) (Math.random() * 255); newBlobData[i] = (byte) (Math.random() * 255); } StringBuffer data = new StringBuffer(); StringBuffer newData = new StringBuffer(); for (int i = 0; i < LOB_LENGTH; i++) { data.append((char) (Math.random() * 58) + 32); newData.append((char) (Math.random() * 58) + 32); } clobData = data.toString(); newClobData = newData.toString(); } public LOBTest(String name) { super(name); } /************************************************************************* ************************************************************************* ** BLOB TESTS ** ************************************************************************* *************************************************************************/ public void testBlobGet1() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobget1 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobget1 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobget1"); assertTrue(rs.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs.getBytes(1))); // Test ResultSet.getBinaryStream() InputStream is = rs.getBinaryStream(1); byte[] isTmpData = new byte[data.length]; assertEquals(data.length, is.read(isTmpData)); assertEquals(-1, is.read()); assertTrue(Arrays.equals(data, isTmpData)); // Test ResultSet.getBlob() Blob blob = rs.getBlob(1); assertNotNull(blob); // Test Blob.length() assertEquals(blob.length(), data.length); // Test Blob.getBytes(0, length); should fail try { blob.getBytes(0L, (int) blob.length()); fail("Blob.getBytes(0, length) should fail."); } catch (SQLException ex) { assertEquals("HY090", ex.getSQLState()); } // Test Blob.getBytes() byte[] tmpData2 = blob.getBytes(1L, (int) blob.length()); assertTrue(Arrays.equals(data, tmpData2)); // Test Blob.getBinaryStream() InputStream is2 = blob.getBinaryStream(); compareInputStreams(new ByteArrayInputStream(data), is2); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobGet2() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobget2 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobget2 (data) VALUES (?)"); // Test PreparedStatement.setBinaryStream() pstmt.setBinaryStream(1, new ByteArrayInputStream(data), data.length); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobget2"); assertTrue(rs.next()); // Test ResultSet.getObject() - Blob Object result = rs.getObject(1); assertTrue(result instanceof Blob); Blob blob = (Blob) result; assertEquals(data.length, blob.length()); // Test Blob.getBytes() assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob.length()))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobSet1() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset1 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset1 (data) VALUES (?)"); // Test PreparedStatement.setBinaryStream() pstmt.setBinaryStream(1, new ByteArrayInputStream(data), data.length); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset1"); assertTrue(rs.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs.getBytes(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobSet2() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset2 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset2 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset2"); assertTrue(rs.next()); Blob blob = rs.getBlob(1); data = getNewBlobTestData(); // Test Blob.setBytes() blob.setBytes(1, data); assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob.length()))); assertFalse(rs.next()); PreparedStatement pstmt2 = con.prepareStatement("UPDATE #blobset2 SET data = ?"); // Test PreparedStatement.setBlob() pstmt2.setBlob(1, blob); assertEquals(1, pstmt2.executeUpdate()); pstmt2.close(); stmt2.close(); rs.close(); Statement stmt3 = con.createStatement(); ResultSet rs2 = stmt3.executeQuery("SELECT data FROM #blobset2"); assertTrue(rs2.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs2.getBytes(1))); assertFalse(rs2.next()); stmt3.close(); rs2.close(); } public void testBlobSet3() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset3 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset3 (data) VALUES (?)"); // Test PreparedStatement.setObject(int,byte[]) pstmt.setObject(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset3"); assertTrue(rs.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs.getBytes(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobSet4() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset4 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset4 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset4"); assertTrue(rs.next()); Blob blob = rs.getBlob(1); data = getNewBlobTestData(); // Test Blob.setBytes() blob.setBytes(1, data); assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob.length()))); assertFalse(rs.next()); PreparedStatement pstmt2 = con.prepareStatement("UPDATE #blobset4 SET data = ?"); // Test PreparedStatement.setObject(int,Blob) pstmt2.setObject(1, blob); assertEquals(1, pstmt2.executeUpdate()); pstmt2.close(); stmt2.close(); rs.close(); Statement stmt3 = con.createStatement(); ResultSet rs2 = stmt3.executeQuery("SELECT data FROM #blobset4"); assertTrue(rs2.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs2.getBytes(1))); assertFalse(rs2.next()); stmt3.close(); rs2.close(); } public void testBlobSet5() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset5 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset5 (data) VALUES (?)"); // Test PreparedStatement.setObject(int,byte[],int) pstmt.setObject(1, data, Types.BINARY); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset5"); assertTrue(rs.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs.getBytes(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobSet6() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset6 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset6 (data) VALUES (?)"); // Test PreparedStatement.setObject(int,byte[],int) pstmt.setObject(1, data, Types.VARBINARY); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset6"); assertTrue(rs.next()); // Test ResultSet.getBytes() assertTrue(Arrays.equals(data, rs.getBytes(1))); assertFalse(rs.next()); stmt2.close(); rs.close(); } public void testBlobSet7() throws Exception { byte[] data = getBlobTestData(); Statement stmt = con.createStatement(); stmt.execute("CREATE TABLE #blobset7 (data IMAGE)"); stmt.close(); PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset7 (data) VALUES (?)"); // Test PreparedStatement.setBytes() pstmt.setBytes(1, data); assertEquals(pstmt.executeUpdate(), 1); pstmt.close(); Statement stmt2 = con.createStatement(); ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset7"); assertTrue(rs.next()); Blob blob = rs.getBlob(1); data = getNewBlobTestData(); // Test Blob.setBytes() blob.setBytes(1, data); assertTrue(Arrays.equals(data, blob.getBytes(1L, (int) blob.length()))); assertFalse(rs.next()); PreparedStatement pstmt2 = con.prepareStatement("UPDATE #blobset7 SET data = ?"); // Test PreparedStatement.setObject(int,Blob,int) pstmt2.setObject(1, blob, Types.BLOB); assertEquals(1, pstmt2.executeUpdate()); pstmt2.close(); stmt2.close(); rs.close(); Statement stmt3 = con.createStatement(); ResultSet rs2 = stmt3.executeQuery("SELECT data FROM #blobset7");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -