lobtest.java
来自「jtds的源码 是你学习java的好东西」· Java 代码 · 共 2,099 行 · 第 1/5 页
JAVA
2,099 行
// 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");
assertTrue(rs2.next());
// Test ResultSet.getBytes()
assertTrue(Arrays.equals(data, rs2.getBytes(1)));
assertFalse(rs2.next());
stmt3.close();
rs2.close();
}
/**
* Test inserting from an <code>InputStream</code> that doesn't fill the
* buffer on <code>read()</code>.
* <p>
* For bug #1008816 - "More data in stream ..." error when inserting an image.
*
* @throws Exception if an error condition occurs
*/
public void testBlobSet8() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobset8 (data IMAGE)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobset8 (data) VALUES (?)");
// Test PreparedStatement.setBinaryStream()
pstmt.setBinaryStream(1, new RealInputStream(), RealInputStream.LENGTH);
assertEquals(pstmt.executeUpdate(), 1);
pstmt.close();
Statement stmt2 = con.createStatement();
ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobset8");
assertTrue(rs.next());
// Test ResultSet.getBinaryStream()
compareInputStreams(new RealInputStream(), rs.getBinaryStream(1));
assertFalse(rs.next());
stmt2.close();
rs.close();
}
public void testBlobUpdate1() throws Exception {
byte[] data = getBlobTestData();
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobupdate1 (id NUMERIC IDENTITY, data IMAGE, "
+ "CONSTRAINT pk_blobupdate1 PRIMARY KEY CLUSTERED (id))");
stmt.close();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT id, data FROM #blobupdate1");
rs.moveToInsertRow();
// Test ResultSet.updateBytes()
rs.updateBytes(2, data);
rs.insertRow();
stmt.close();
rs.close();
Statement stmt2 = con.createStatement();
ResultSet rs2 = stmt2.executeQuery("SELECT data FROM #blobupdate1");
assertTrue(rs2.next());
// Test ResultSet.getBytes()
assertTrue(Arrays.equals(data, rs2.getBytes(1)));
assertFalse(rs2.next());
stmt2.close();
rs2.close();
}
public void testBlobUpdate2() throws Exception {
byte[] data = getBlobTestData();
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobupdate2 (id NUMERIC IDENTITY, data IMAGE, "
+ "CONSTRAINT pk_blobupdate2 PRIMARY KEY CLUSTERED (id))");
stmt.close();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT id, data FROM #blobupdate2");
rs.moveToInsertRow();
// Test ResultSet.updateBinaryStream()
rs.updateBinaryStream(2, new ByteArrayInputStream(data), data.length);
rs.insertRow();
stmt.close();
rs.close();
Statement stmt2 = con.createStatement();
ResultSet rs2 = stmt2.executeQuery("SELECT data FROM #blobupdate2");
assertTrue(rs2.next());
// Test ResultSet.getBytes()
assertTrue(Arrays.equals(data, rs2.getBytes(1)));
assertFalse(rs2.next());
stmt2.close();
rs2.close();
}
public void testBlobUpdate3() throws Exception {
byte[] data = getBlobTestData();
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobupdate3 (id NUMERIC IDENTITY, data IMAGE, "
+ "CONSTRAINT pk_blobupdate3 PRIMARY KEY CLUSTERED (id))");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobupdate3 (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 #blobupdate3");
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());
Statement stmt3 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs2 = stmt3.executeQuery("SELECT id, data FROM #blobupdate3");
assertTrue(rs2.next());
// Test ResultSet.updateBlob()
rs2.updateBlob(2, blob);
rs2.updateRow();
assertFalse(rs2.next());
stmt2.close();
rs.close();
stmt3.close();
rs2.close();
Statement stmt4 = con.createStatement();
ResultSet rs3 = stmt4.executeQuery("SELECT data FROM #blobupdate3");
assertTrue(rs3.next());
// Test ResultSet.getBytes()
assertTrue(Arrays.equals(data, rs3.getBytes(1)));
assertFalse(rs3.next());
stmt4.close();
rs3.close();
}
public void testBlobUpdate4() throws Exception {
byte[] data = getBlobTestData();
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobupdate4 (id NUMERIC IDENTITY, data IMAGE, "
+ "CONSTRAINT pk_blobupdate4 PRIMARY KEY CLUSTERED (id))");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobupdate4 (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 #blobupdate4");
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());
Statement stmt3 = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs2 = stmt3.executeQuery("SELECT id, data FROM #blobupdate4");
assertTrue(rs2.next());
// Test ResultSet.updateBlob()
rs2.updateObject(2, blob);
rs2.updateRow();
assertFalse(rs2.next());
stmt2.close();
rs.close();
stmt3.close();
rs2.close();
Statement stmt4 = con.createStatement();
ResultSet rs3 = stmt4.executeQuery("SELECT data FROM #blobupdate4");
assertTrue(rs3.next());
// Test ResultSet.getBytes()
assertTrue(Arrays.equals(data, rs3.getBytes(1)));
assertFalse(rs3.next());
stmt4.close();
rs3.close();
}
/**
* Test Long blob manipulation including updates to the middle of the
* <code>Blob</code>.
*/
public void testBlobUpdate5() throws Exception {
byte[] data = new byte[100000];
for (int i = 0; i < data.length; i++) {
data[i] = (byte)('A'+i%10);
}
//
// Construct a blob
//
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 0x00");
assertNotNull(rs);
assertTrue(rs.next());
Blob blob = rs.getBlob(1);
blob.setBytes(1, data);
byte[] tmp = blob.getBytes(1, (int)blob.length());
assertTrue(compare(data, tmp));
blob.setBytes(1, data);
tmp = blob.getBytes(1, (int)blob.length());
assertTrue(compare(data, tmp));
data[100] = 'a';
data[101] = 'b';
blob.setBytes(101, data, 100, 2);
tmp = blob.getBytes(1, (int)blob.length());
assertTrue(compare(data, tmp));
InputStream is = blob.getBinaryStream();
tmp = new byte[data.length];
int b;
int p = 0;
while ((b = is.read()) >= 0) {
tmp[p++] = (byte)b;
}
is.close();
assertTrue(compare(data, tmp));
tmp = blob.getBytes(101, 2);
assertTrue(compare(new byte[]{'a','b'}, tmp));
blob = rs.getBlob(1);
OutputStream os = blob.setBinaryStream(1);
for (int i = 0; i < data.length; i++) {
os.write(('A'+i%10));
}
os.close();
os = blob.setBinaryStream(101);
os.write('a');
os.write('b');
os.close();
tmp = blob.getBytes(1, (int)blob.length());
assertTrue(compare(data, tmp));
tmp = new byte[5000];
for (int i = 0; i < 5000; i++) {
tmp[i] = (byte)(0x80 + (i % 10));
}
blob.setBytes(100000-5000, tmp);
assertTrue(compare(tmp, blob.getBytes(100000-5000, 5000)));
assertEquals(100000L, blob.length());
assertEquals(100000-5000, blob.position(tmp, 100000-5000));
Blob blob2 = rs.getBlob(1);
blob2.setBytes(1, tmp);
assertEquals(100000-5000, blob.position(blob2, 1));
assertEquals(101, blob.position(new byte[]{'a','b'}, 1));
blob.truncate(10);
assertEquals(10L, blob.length());
tmp = new byte[10];
System.arraycopy(data, 0, tmp, 0, 10);
assertTrue(compare(tmp, blob.getBytes(1, (int)blob.length())));
}
public void testBlobSetNull1() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobsetnull1 (data IMAGE NULL)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobsetnull1 (data) VALUES (?)");
// Test PreparedStatement.setBinaryStream()
pstmt.setBinaryStream(1, null, 0);
assertEquals(pstmt.executeUpdate(), 1);
pstmt.close();
Statement stmt2 = con.createStatement();
ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobsetnull1");
assertTrue(rs.next());
// Test ResultSet.getBinaryStream()
assertNull(rs.getBinaryStream(1));
assertTrue(rs.wasNull());
// Test ResultSet.getBlob()
assertNull(rs.getBlob(1));
assertTrue(rs.wasNull());
// Test ResultSet.getBytes()
assertNull(rs.getBytes(1));
assertTrue(rs.wasNull());
// Test ResultSet.getObject()
assertNull(rs.getObject(1));
assertTrue(rs.wasNull());
assertFalse(rs.next());
stmt2.close();
rs.close();
}
public void testBlobSetNull2() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobsetnull2 (data IMAGE NULL)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobsetnull2 (data) VALUES (?)");
// Test PreparedStatement.setBlob()
pstmt.setBlob(1, null);
assertEquals(pstmt.executeUpdate(), 1);
pstmt.close();
Statement stmt2 = con.createStatement();
ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobsetnull2");
assertTrue(rs.next());
// Test ResultSet.getBinaryStream()
assertNull(rs.getBinaryStream(1));
assertTrue(rs.wasNull());
// Test ResultSet.getBlob()
assertNull(rs.getBlob(1));
assertTrue(rs.wasNull());
// Test ResultSet.getBytes()
assertNull(rs.getBytes(1));
assertTrue(rs.wasNull());
// Test ResultSet.getObject()
assertNull(rs.getObject(1));
assertTrue(rs.wasNull());
assertFalse(rs.next());
stmt2.close();
rs.close();
}
public void testBlobSetNull3() throws Exception {
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE #blobsetnull3 (data IMAGE NULL)");
stmt.close();
PreparedStatement pstmt = con.prepareStatement("INSERT INTO #blobsetnull3 (data) VALUES (?)");
// Test PreparedStatement.setBytes()
pstmt.setBytes(1, null);
assertEquals(pstmt.executeUpdate(), 1);
pstmt.close();
Statement stmt2 = con.createStatement();
ResultSet rs = stmt2.executeQuery("SELECT data FROM #blobsetnull3");
assertTrue(rs.next());
// Test ResultSet.getBinaryStream()
assertNull(rs.getBinaryStream(1));
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?