KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > lobStreams


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.lobStreams
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
23
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.Reader JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.sql.Blob JavaDoc;
34 import java.sql.Clob JavaDoc;
35 import java.sql.Connection JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40
41 import org.apache.derby.tools.ij;
42 import org.apache.derbyTesting.functionTests.util.TestUtil;
43
44 public class lobStreams {
45     
46         static String JavaDoc[] fileName = new String JavaDoc[2];
47         static String JavaDoc sep;
48         static long fileLength;
49
50         static boolean debug = true;
51         private static final String JavaDoc START = "\nSTART: ";
52         
53         private static final String JavaDoc unicodeTestString = "This is a test string containing a few " +
54                 "non-ascii characters:\nÆØÅ and æøå are used in norwegian: 'Blåbærsyltetøy' means" +
55                 "'blueberry jam', and tastes great on pancakes. =)";
56
57         static
58         {
59 // fileName[0] = "extin" + sep + "aclob.utf";
60
// fileName[1] = "extin" + sep + "littleclob.utf";
61
fileName[0] = "aclob.utf";
62             fileName[1] = "littleclob.utf";
63         }
64         
65         public static void main(String JavaDoc[] args)
66         {
67             System.out.println("Test lob stream with multiple writes starting");
68
69             // check to see if we have the correct extin path, if the files aren't right here, try one more time
70
boolean exists = (new File JavaDoc("extin", fileName[0])).exists();
71             String JavaDoc sep = System.getProperty("file.separator");
72         if (!exists)
73             {
74                 // assume it's in a dir up, if that's wrong too, too bad...
75
String JavaDoc userdir = System.getProperty("user.dir");
76                 fileName[0] = userdir + sep + ".." + sep + "extin" + sep + fileName[0];
77                 fileName[1] = userdir + sep + ".." + sep + "extin" + sep + fileName[1];
78             }
79             else
80             {
81                 // assume it's in a dir up, if that's wrong too, too bad...
82
fileName[0] = "extin" + sep + fileName[0];
83                 fileName[1] = "extin" + sep + fileName[1];
84             }
85
86
87             try
88             {
89                 // use the ij utility to read the property file and
90
// make the initial connection.
91
ij.getPropertyArg(args);
92                 Connection JavaDoc conn = ij.startJBMS();
93                 // turn off autocommit, otherwise blobs/clobs cannot hang around
94
// until end of transaction
95
conn.setAutoCommit(false);
96
97                 prepareTable(conn);
98                 testBlobWrite3Param(conn);
99                 resetBlobClob(conn);
100                 testBlobWrite1Param(conn);
101                 resetBlobClob(conn);
102                 testClobAsciiWrite3Param(conn);
103                 resetBlobClob(conn);
104                 testClobAsciiWrite1Param(conn);
105                 resetBlobClob(conn);
106                 testClobCharacterWrite3ParamChar(conn);
107                 resetBlobClob(conn);
108                 testClobCharacterWrite3ParamString(conn);
109                 resetBlobClob(conn);
110                 testClobCharacterWrite1ParamString(conn);
111                 resetBlobClob(conn);
112                 testClobCharacterWrite1Char(conn);
113
114                 // restart the connection
115
conn.commit();
116                 cleanUp(conn);
117                 conn.commit();
118                 conn.close();
119
120             }
121             catch (Throwable JavaDoc e)
122             {
123                 System.out.println("FAIL -- unexpected exception:" + e.toString());
124                 if (debug) e.printStackTrace();
125             }
126             System.out.println("Test lob stream with multiple writes finished\n");
127         }
128         
129         private static void prepareTable(Connection JavaDoc conn) {
130             try {
131                 Statement JavaDoc stmt1 = conn.createStatement();
132                 stmt1.execute("create table testBlobX1 (a integer, b blob(300K), c clob(300K))");
133                 stmt1.close();
134
135                 byte[] b2 = new byte[1];
136                 b2[0] = (byte)64;
137                 String JavaDoc c2 = "c";
138                 PreparedStatement JavaDoc stmt2 = conn.prepareStatement(
139                         "INSERT INTO testBlobX1(a, b, c) VALUES (?, ?, ?)");
140                 stmt2.setInt(1, 1);
141                 stmt2.setBytes(2, b2);
142                 stmt2.setString(3, c2);
143                 stmt2.execute();
144                 stmt2.close();
145
146             } catch (SQLException JavaDoc e) {
147                 TestUtil.dumpSQLExceptions(e);
148             } catch (Throwable JavaDoc e) {
149                 if (debug) e.printStackTrace();
150             }
151             
152         }
153
154         private static void resetBlobClob(Connection JavaDoc conn) {
155             try {
156                 byte[] b2 = new byte[1];
157                 b2[0] = (byte)64;
158                 String JavaDoc c2 = "a";
159                 PreparedStatement JavaDoc stmt = conn.prepareStatement(
160                         "UPDATE testBlobX1 SET b = ?, c = ? WHERE a = 1");
161                 stmt.setBytes(1, b2);
162                 stmt.setString(2, c2);
163                 stmt.execute();
164                 stmt.close();
165
166             } catch (SQLException JavaDoc e) {
167                 TestUtil.dumpSQLExceptions(e);
168             } catch (Throwable JavaDoc e) {
169                 if (debug) e.printStackTrace();
170             }
171             
172         }
173
174         /**
175          * Tests the BlobOutputStream.write(byte b[], int off, int len) method
176          **/

177         private static void testBlobWrite3Param(Connection JavaDoc conn)
178         {
179             try {
180                 System.out.println(START + "testBlobWrite3Param");
181                
182                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
183                     "SELECT b FROM testBlobX1 WHERE a = 1");
184                 
185                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
186                 
187                 rs3.next();
188
189                 Blob JavaDoc blob = rs3.getBlob(1);
190
191                 File JavaDoc file = new File JavaDoc(fileName[0]);
192                 fileLength = file.length();
193                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
194
195                 if (blob != null) {
196                     int count = 0;
197                     byte[] buffer = new byte[1024];
198                     OutputStream JavaDoc outstream = blob.setBinaryStream(1L);
199                     while ((count = fileIn.read(buffer)) != -1) {
200                         outstream.write(buffer, 0, count);
201                     }
202                     outstream.close();
203                     fileIn.close();
204                     
205                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
206                         "UPDATE testBlobX1 SET b = ? WHERE a = 1");
207                     stmt4.setBlob(1, blob);
208                     stmt4.executeUpdate();
209                     stmt4.close();
210                     
211                 } else {
212                     System.out.println("FAIL -- blob is NULL");
213                 }
214
215                 rs3.close();
216                 rs3 = stmt3.executeQuery();
217                 
218                 if (rs3.next()) {
219                     long new_length = rs3.getBlob(1).length();
220                     if (new_length != fileLength) {
221                         System.out.println(
222                                 "FAIL -- wrong blob length; original: " +
223                                 fileLength + " blob length: " + new_length);
224                     } else {
225                         // Check contents ...
226
InputStream JavaDoc fStream = new FileInputStream JavaDoc(file);
227                         InputStream JavaDoc lStream = rs3.getBlob(1).getBinaryStream();
228
229                         if (!compareLob2File(fStream, lStream))
230                             System.out.println("FAIL - Blob and file contents do not match");
231
232                         fStream.close();
233                         lStream.close();
234                         
235                     }
236                 } else {
237                     System.out.println("FAIL -- blob not found");
238                 }
239                 rs3.close();
240                 stmt3.close();
241                
242                 System.out.println("testBlobWrite3Param finished");
243             } catch (SQLException JavaDoc e) {
244                 TestUtil.dumpSQLExceptions(e);
245             } catch (Throwable JavaDoc e) {
246                 if (debug) e.printStackTrace();
247             }
248         }
249
250         /**
251          * Tests the BlobOutputStream.write(int b) method
252          **/

253         private static void testBlobWrite1Param(Connection JavaDoc conn)
254         {
255             try {
256                 System.out.println(START + "testBlobWrite1Param");
257                
258                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
259                     "SELECT b FROM testBlobX1 WHERE a = 1");
260                 
261                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
262                 
263                 rs3.next();
264
265                 Blob JavaDoc blob = rs3.getBlob(1);
266
267                 File JavaDoc file = new File JavaDoc(fileName[1]);
268                 fileLength = file.length();
269                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
270
271                 if (blob != null) {
272                     int buffer;
273                     OutputStream JavaDoc outstream = blob.setBinaryStream(1L);
274                     while ((buffer = fileIn.read()) != -1) {
275                         outstream.write(buffer);
276                     }
277                     outstream.close();
278                     fileIn.close();
279                     
280                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
281                         "UPDATE testBlobX1 SET b = ? WHERE a = 1");
282                     stmt4.setBlob(1, blob);
283                     stmt4.executeUpdate();
284                     stmt4.close();
285                     
286                 } else {
287                     System.out.println("FAIL -- blob is NULL");
288                 }
289
290                 rs3.close();
291                 rs3 = stmt3.executeQuery();
292                 
293                 if (rs3.next()) {
294                     long new_length = rs3.getBlob(1).length();
295                     if (new_length != fileLength) {
296                         System.out.println(
297                                 "FAIL -- wrong blob length; original: " +
298                                 fileLength + " blob length: " + new_length);
299                     } else {
300                         // Check contents ...
301
InputStream JavaDoc fStream = new FileInputStream JavaDoc(file);
302                         InputStream JavaDoc lStream = rs3.getBlob(1).getBinaryStream();
303
304                         if (!compareLob2File(fStream, lStream))
305                             System.out.println("FAIL - Blob and file contents do not match");
306
307                         fStream.close();
308                         lStream.close();
309                         
310                     }
311                 } else {
312                     System.out.println("FAIL -- blob not found");
313                 }
314                 rs3.close();
315                 stmt3.close();
316                
317                 System.out.println("testBlobWrite1Param finished");
318             } catch (SQLException JavaDoc e) {
319                 TestUtil.dumpSQLExceptions(e);
320             } catch (Throwable JavaDoc e) {
321                 if (debug) e.printStackTrace();
322             }
323         }
324
325         /**
326          * Tests the ClobOutputStream.write(byte b[], int off, int len) method
327          **/

328         private static void testClobAsciiWrite3Param(Connection JavaDoc conn)
329         {
330             try {
331                 System.out.println(START + "testClobAsciiWrite3Param");
332                
333                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
334                     "SELECT c FROM testBlobX1 WHERE a = 1");
335                 
336                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
337                 
338                 rs3.next();
339
340                 Clob JavaDoc clob = rs3.getClob(1);
341
342                 File JavaDoc file = new File JavaDoc(fileName[0]);
343                 fileLength = file.length();
344                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
345                 
346                 if (clob != null) {
347                     int count = 0;
348                     byte[] buffer = new byte[1024];
349                     OutputStream JavaDoc outstream = clob.setAsciiStream(1L);
350                     while ((count = fileIn.read(buffer)) != -1) {
351                         outstream.write(buffer, 0, count);
352                     }
353                     outstream.close();
354                     fileIn.close();
355                     
356                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
357                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
358                     stmt4.setClob(1, clob);
359                     stmt4.executeUpdate();
360                     stmt4.close();
361                 } else {
362                     System.out.println("FAIL -- clob is NULL");
363                 }
364
365                 rs3.close();
366                 rs3 = stmt3.executeQuery();
367                 
368                 if (rs3.next()) {
369                     long new_length = rs3.getClob(1).length();
370                     if (new_length != fileLength) {
371                         System.out.println(
372                                 "FAIL -- wrong clob length; original: " +
373                                 fileLength + " clob length: " + new_length);
374                     } else {
375                         // Check contents ...
376
InputStream JavaDoc fStream = new FileInputStream JavaDoc(file);
377                         InputStream JavaDoc lStream = rs3.getClob(1).getAsciiStream();
378
379                         if (!compareLob2File(fStream, lStream))
380                             System.out.println("FAIL - Clob and file contents do not match");
381
382                         fStream.close();
383                         lStream.close();
384                         
385                     }
386                 } else {
387                     System.out.println("FAIL -- clob not found");
388                 }
389                 rs3.close();
390                 stmt3.close();
391                
392                 System.out.println("testClobAsciiWrite3Param finished");
393             } catch (SQLException JavaDoc e) {
394                 TestUtil.dumpSQLExceptions(e);
395             } catch (Throwable JavaDoc e) {
396                 if (debug) e.printStackTrace();
397             }
398         }
399
400         /**
401          * Tests the ClobOutputStream.write(int b) method
402          **/

403         private static void testClobAsciiWrite1Param(Connection JavaDoc conn)
404         {
405             try {
406                 System.out.println(START + "testClobAsciiWrite1Param");
407                
408                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
409                     "SELECT c FROM testBlobX1 WHERE a = 1");
410                 
411                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
412                 
413                 rs3.next();
414
415                 Clob JavaDoc clob = rs3.getClob(1);
416
417                 File JavaDoc file = new File JavaDoc(fileName[1]);
418                 fileLength = file.length();
419                 InputStream JavaDoc fileIn = new FileInputStream JavaDoc(file);
420
421                 if (clob != null) {
422                     int buffer;
423                     OutputStream JavaDoc outstream = clob.setAsciiStream(1L);
424                     while ((buffer = fileIn.read()) != -1) {
425                         outstream.write(buffer);
426                     }
427                     outstream.close();
428                     fileIn.close();
429                     
430                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
431                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
432                     stmt4.setClob(1, clob);
433                     stmt4.executeUpdate();
434                     stmt4.close();
435                     
436                 } else {
437                     System.out.println("FAIL -- clob is NULL");
438                 }
439
440                 rs3.close();
441                 rs3 = stmt3.executeQuery();
442                 
443                 if (rs3.next()) {
444                     long new_length = rs3.getClob(1).length();
445                     if (new_length != fileLength) {
446                         System.out.println(
447                                 "FAIL -- wrong clob length; original: " +
448                                 fileLength + " clob length: " + new_length);
449                     } else {
450                         // Check contents ...
451
InputStream JavaDoc fStream = new FileInputStream JavaDoc(file);
452                         InputStream JavaDoc lStream = rs3.getClob(1).getAsciiStream();
453
454                         if (!compareLob2File(fStream, lStream))
455                             System.out.println("FAIL - Clob and file contents do not match");
456
457                         fStream.close();
458                         lStream.close();
459                         
460                     }
461                 } else {
462                     System.out.println("FAIL -- clob not found");
463                 }
464                 rs3.close();
465                 stmt3.close();
466                
467                 System.out.println("testClobAsciiWrite1Param finished");
468             } catch (SQLException JavaDoc e) {
469                 TestUtil.dumpSQLExceptions(e);
470             } catch (Throwable JavaDoc e) {
471                 if (debug) e.printStackTrace();
472             }
473         }
474
475         /**
476          * Tests the ClobWriter.write(char cbuf[], int off, int len) method
477          **/

478         private static void testClobCharacterWrite3ParamChar(Connection JavaDoc conn)
479         {
480             try {
481                 System.out.println(START + "testClobCharacterWrite3ParamChar");
482                
483                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
484                     "SELECT c FROM testBlobX1 WHERE a = 1");
485                 
486                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
487                 
488                 rs3.next();
489
490                 Clob JavaDoc clob = rs3.getClob(1);
491                 char[] testdata = unicodeTestString.toCharArray();
492                 
493
494                 if (clob != null) {
495                     Writer JavaDoc clobWriter = clob.setCharacterStream(1L);
496                     clobWriter.write(testdata, 0, testdata.length);
497                     clobWriter.close();
498                     
499                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
500                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
501                     stmt4.setClob(1, clob);
502                     stmt4.executeUpdate();
503                     stmt4.close();
504                 } else {
505                     System.out.println("FAIL -- clob is NULL");
506                 }
507
508                 rs3.close();
509                 rs3 = stmt3.executeQuery();
510                 
511                 if (rs3.next()) {
512                     long new_length = rs3.getClob(1).length();
513                     if (new_length != testdata.length) {
514                         System.out.println(
515                                 "FAIL -- wrong clob length; original: " +
516                                 testdata.length + " clob length: " + new_length);
517                     } else {
518                         // Check contents ...
519
Reader JavaDoc lStream = rs3.getClob(1).getCharacterStream();
520
521                         if (!compareClobReader2CharArray(testdata, lStream))
522                             System.out.println("FAIL - Clob and buffer contents do not match");
523
524                         lStream.close();
525                         
526                     }
527                 } else {
528                     System.out.println("FAIL -- clob not found");
529                 }
530                 rs3.close();
531                 stmt3.close();
532                
533                 System.out.println("testClobCharacterWrite3ParamChar finished");
534             } catch (SQLException JavaDoc e) {
535                 TestUtil.dumpSQLExceptions(e);
536             } catch (Throwable JavaDoc e) {
537                 if (debug) e.printStackTrace();
538             }
539         }
540
541         /**
542          * Tests the ClobWriter.write(String str, int off, int len) method
543          **/

544         private static void testClobCharacterWrite3ParamString(Connection JavaDoc conn)
545         {
546             try {
547                 System.out.println(START + "testClobCharacterWrite3ParamString");
548                
549                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
550                     "SELECT c FROM testBlobX1 WHERE a = 1");
551                 
552                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
553                 
554                 rs3.next();
555
556                 Clob JavaDoc clob = rs3.getClob(1);
557                 
558
559                 if (clob != null) {
560                     Writer JavaDoc clobWriter = clob.setCharacterStream(1L);
561                     clobWriter.write(unicodeTestString, 0, unicodeTestString.length());
562                     clobWriter.close();
563                     
564                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
565                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
566                     stmt4.setClob(1, clob);
567                     stmt4.executeUpdate();
568                     stmt4.close();
569                 } else {
570                     System.out.println("FAIL -- clob is NULL");
571                 }
572
573                 rs3.close();
574                 rs3 = stmt3.executeQuery();
575                 
576                 if (rs3.next()) {
577                     long new_length = rs3.getClob(1).length();
578                     if (new_length != unicodeTestString.length()) {
579                         System.out.println(
580                                 "FAIL -- wrong clob length; original: " +
581                                 unicodeTestString.length() + " clob length: " + new_length);
582                     } else {
583                         // Check contents ...
584
Reader JavaDoc lStream = rs3.getClob(1).getCharacterStream();
585
586                         if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream))
587                             System.out.println("FAIL - Clob and buffer contents do not match");
588
589                         lStream.close();
590                         
591                     }
592                 } else {
593                     System.out.println("FAIL -- clob not found");
594                 }
595                 rs3.close();
596                 stmt3.close();
597                
598                 System.out.println("testClobCharacterWrite3ParamString finished");
599             } catch (SQLException JavaDoc e) {
600                 TestUtil.dumpSQLExceptions(e);
601             } catch (Throwable JavaDoc e) {
602                 if (debug) e.printStackTrace();
603             }
604         }
605
606         /**
607          * Tests the ClobWriter.write(String str) method
608          **/

609         private static void testClobCharacterWrite1ParamString(Connection JavaDoc conn)
610         {
611             try {
612                 System.out.println(START + "testClobCharacterWrite1ParamString");
613                
614                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
615                     "SELECT c FROM testBlobX1 WHERE a = 1");
616                 
617                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
618                 
619                 rs3.next();
620
621                 Clob JavaDoc clob = rs3.getClob(1);
622                 
623
624                 if (clob != null) {
625                     Writer JavaDoc clobWriter = clob.setCharacterStream(1L);
626                     clobWriter.write(unicodeTestString);
627                     clobWriter.close();
628                     
629                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
630                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
631                     stmt4.setClob(1, clob);
632                     stmt4.executeUpdate();
633                     stmt4.close();
634                 } else {
635                     System.out.println("FAIL -- clob is NULL");
636                 }
637
638                 rs3.close();
639                 rs3 = stmt3.executeQuery();
640                 
641                 if (rs3.next()) {
642                     long new_length = rs3.getClob(1).length();
643                     if (new_length != unicodeTestString.length()) {
644                         System.out.println(
645                                 "FAIL -- wrong clob length; original: " +
646                                 unicodeTestString.length() + " clob length: " + new_length);
647                     } else {
648                         // Check contents ...
649
Reader JavaDoc lStream = rs3.getClob(1).getCharacterStream();
650
651                         if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream))
652                             System.out.println("FAIL - Clob and buffer contents do not match");
653
654                         lStream.close();
655                         
656                     }
657                 } else {
658                     System.out.println("FAIL -- clob not found");
659                 }
660                 rs3.close();
661                 stmt3.close();
662                
663                 System.out.println("testClobCharacterWrite1ParamString finished");
664             } catch (SQLException JavaDoc e) {
665                 TestUtil.dumpSQLExceptions(e);
666             } catch (Throwable JavaDoc e) {
667                 if (debug) e.printStackTrace();
668             }
669         }
670         /**
671          * Tests the ClobWriter.write(int c) method
672          **/

673         private static void testClobCharacterWrite1Char(Connection JavaDoc conn)
674         {
675             try {
676                 System.out.println(START + "testClobCharacterWrite1Char");
677                
678                 PreparedStatement JavaDoc stmt3 = conn.prepareStatement(
679                     "SELECT c FROM testBlobX1 WHERE a = 1");
680                 
681                 ResultSet JavaDoc rs3 = stmt3.executeQuery();
682                 
683                 rs3.next();
684
685                 Clob JavaDoc clob = rs3.getClob(1);
686                 
687                 char testchar = 'a';
688
689                 if (clob != null) {
690                     Writer JavaDoc clobWriter = clob.setCharacterStream(1L);
691                     clobWriter.write(testchar);
692                     clobWriter.close();
693                     
694                     PreparedStatement JavaDoc stmt4 = conn.prepareStatement(
695                         "UPDATE testBlobX1 SET c = ? WHERE a = 1");
696                     stmt4.setClob(1, clob);
697                     stmt4.executeUpdate();
698                     stmt4.close();
699                 } else {
700                     System.out.println("FAIL -- clob is NULL");
701                 }
702
703                 rs3.close();
704                 rs3 = stmt3.executeQuery();
705                 
706                 if (rs3.next()) {
707                     long new_length = rs3.getClob(1).length();
708                     Clob JavaDoc fish = rs3.getClob(1);
709                     if (new_length != 1) {
710                         System.out.println(
711                                 "FAIL -- wrong clob length; original: " +
712                                 1 + " clob length: " + new_length);
713                     } else {
714                         // Check contents ...
715
Reader JavaDoc lStream = rs3.getClob(1).getCharacterStream();
716                         char clobchar = (char) lStream.read();
717                         
718                         if (clobchar != testchar)
719                             System.out.println("FAIL - fetched Clob and original contents do not match");
720
721                         lStream.close();
722                         
723                     }
724                 } else {
725                     System.out.println("FAIL -- clob not found");
726                 }
727                 rs3.close();
728                 stmt3.close();
729                
730                 System.out.println("testClobCharacterWrite1Char finished");
731             } catch (SQLException JavaDoc e) {
732                 TestUtil.dumpSQLExceptions(e);
733             } catch (Throwable JavaDoc e) {
734                 if (debug) e.printStackTrace();
735             }
736         }
737
738         
739         private static boolean compareLob2File(InputStream JavaDoc fStream, InputStream JavaDoc lStream) {
740             byte[] fByte = new byte[1024];
741             byte[] lByte = new byte[1024];
742             int lLength = 0, fLength = 0;
743             String JavaDoc fString, lString;
744             
745             try {
746                 do {
747                     fLength = fStream.read(fByte, 0, 1024);
748                     lLength = lStream.read(lByte, 0, 1024);
749                     if (!java.util.Arrays.equals(fByte, lByte))
750                         return false;
751                 } while (fLength > 0 && lLength > 0);
752
753                 fStream.close();
754                 lStream.close();
755             } catch (Throwable JavaDoc e) {
756                 if (debug) e.printStackTrace();
757             }
758             return true;
759         }
760
761         private static boolean compareClobReader2CharArray(char[] cArray, Reader JavaDoc charReader) {
762             char[] clobChars = new char[cArray.length];
763
764             int readChars = 0;
765             int totalCharsRead = 0;
766
767             try {
768                 do {
769                     readChars = charReader.read(clobChars, totalCharsRead, cArray.length - totalCharsRead);
770                     if (readChars != -1)
771                         totalCharsRead += readChars;
772                 } while (readChars != -1 && totalCharsRead < cArray.length);
773                 charReader.close();
774                 if (!java.util.Arrays.equals(cArray, clobChars))
775                     return false;
776
777             } catch (Throwable JavaDoc e) {
778                 if (debug) e.printStackTrace();
779             }
780             return true;
781         }
782
783         
784         private static void cleanUp(Connection JavaDoc conn) throws SQLException JavaDoc {
785             String JavaDoc[] testObjects = {"table testBlobX1"};
786             Statement JavaDoc cleanupStmt = conn.createStatement();
787             TestUtil.cleanUpTest(cleanupStmt, testObjects);
788         }
789
790 }
791
Popular Tags