~pbms-core/pbms/async_read

« back to all changes in this revision

Viewing changes to mybs/java/src/TestJDBC.java

  • Committer: paul-mccullagh
  • Date: 2008-03-26 11:35:17 UTC
  • Revision ID: paul-mccullagh-afb1610c21464a577ae428d72fc725eb986c05a5
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 SNAP Innovation GmbH
 
2
 *
 
3
 * BLOB Streaming for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Paul McCullagh
 
20
 *
 
21
 * 2007-10-02
 
22
 *
 
23
 * H&G2JCtL
 
24
 *
 
25
 * Test JDBC-based BLOB streaming.
 
26
 *
 
27
 */
 
28
 
 
29
import java.sql.*;
 
30
import java.io.Reader;
 
31
import java.io.InputStream;
 
32
import java.io.IOException;
 
33
import java.io.ByteArrayInputStream;
 
34
import java.io.Reader;
 
35
import java.io.CharArrayReader;
 
36
import javax.sql.rowset.serial.SerialException;
 
37
import javax.sql.rowset.serial.SerialBlob;
 
38
 
 
39
public class TestJDBC {
 
40
    public static void main (String args[]) {
 
41
        // insert code here...
 
42
        System.out.println("JDBC BLOB Streaming Test");
 
43
                System.out.println("java.class.path: "+System.getProperty("java.class.path"));
 
44
 
 
45
        try {
 
46
            // The newInstance() call is a work around for some
 
47
            // broken Java implementations
 
48
 
 
49
            Class.forName("com.mysql.jdbc.Driver").newInstance();
 
50
                        
 
51
        }
 
52
                catch (Exception ex) {
 
53
            // handle the error
 
54
        }
 
55
 
 
56
                Connection conn = null;
 
57
                try {
 
58
                        conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=&enableBlobStreaming=true");
 
59
 
 
60
                        execute(conn, "CREATE TABLE IF NOT EXISTS mybs_test_tab (n_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, n_text LONGBLOB) ENGINE=PBXT");
 
61
                        funcTest(conn);
 
62
                        bigDataTest(conn);
 
63
                }
 
64
                catch (SQLException ex) {
 
65
                        // handle any errors
 
66
                        System.out.println("SQLException: " + ex.getMessage());
 
67
                        System.out.println("SQLState: " + ex.getSQLState());
 
68
                        System.out.println("VendorError: " + ex.getErrorCode());
 
69
                        ex.printStackTrace();
 
70
                }
 
71
                catch (Exception e1) {
 
72
                        e1.printStackTrace();
 
73
                }
 
74
                finally {
 
75
                        if (conn != null) {
 
76
                                try {
 
77
                                        conn.close();
 
78
                                }
 
79
                                catch (Exception e2) {
 
80
                                }
 
81
                        }
 
82
                }
 
83
 
 
84
        }
 
85
 
 
86
        static final int SET_ASCII_STREAM_TEST = 1;             // setAsciiStream(int parameterIndex, InputStream x, long length) 
 
87
        static final int SET_BINARY_STREAM_TEST = 2;    // setBinaryStream(int parameterIndex, InputStream x, long length)
 
88
        static final int SET_BLOB_TEST = 3;                             // setBlob(int parameterIndex, Blob x) 
 
89
        static final int SET_BLOB_STREAM_TEST = 4;              // setBlob(int parameterIndex, InputStream inputStream, long length)
 
90
        static final int SET_CHAR_STREAM_TEST = 5;              // setCharacterStream(int parameterIndex, Reader reader, long length) 
 
91
        static final int SET_CLOB_TEST = 6;                             // setClob(int parameterIndex, Clob x) 
 
92
        static final int SET_CLOB_READER_TEST = 7;              // setClob(int parameterIndex, Reader reader, long length) 
 
93
 
 
94
        public static class BigByteStream extends InputStream {
 
95
                int size;
 
96
                int pos;
 
97
 
 
98
                public BigByteStream(int s) {
 
99
                        size = s;
 
100
                        pos = 0;
 
101
                }
 
102
                
 
103
                public int read() {
 
104
                        if (pos == size)
 
105
                                return -1;
 
106
                        int ch = pos % 256;
 
107
                        pos++;
 
108
                        return ch;
 
109
                }
 
110
                
 
111
                public int getLength()
 
112
                {
 
113
                        return size;
 
114
                }
 
115
        }
 
116
 
 
117
        public static class LocalBlob extends SerialBlob {
 
118
                public LocalBlob(byte[] b) throws SerialException, SQLException
 
119
                {
 
120
                        super(b);
 
121
                }
 
122
 
 
123
                /* Fix a bug! */
 
124
                public byte[] getBytes(long pos, int length) throws SerialException
 
125
                {
 
126
                        if (pos >= 1 && length == 0)
 
127
                                return new byte[0];
 
128
                        return super.getBytes(pos, length);
 
129
                }
 
130
        }
 
131
 
 
132
        public static class StreamBuffer {
 
133
                public int                      type;
 
134
                public byte                     buffer[];
 
135
 
 
136
                public StreamBuffer(int typ, byte buf[])
 
137
                {
 
138
                        type = typ;
 
139
                        buffer = buf;
 
140
                }
 
141
 
 
142
                public InputStream getStream()
 
143
                {
 
144
                        return new ByteArrayInputStream(buffer);
 
145
                }
 
146
                
 
147
                public long getLength()
 
148
                {
 
149
                        return (long) buffer.length;
 
150
                }
 
151
 
 
152
                public Blob getBlob() throws SerialException, SQLException
 
153
                {
 
154
                        return new LocalBlob(buffer);
 
155
                }
 
156
 
 
157
                public char[] getChars()
 
158
                {
 
159
                        char chars[] = new char[buffer.length];
 
160
                        
 
161
                        for (int i=0; i<buffer.length; i++)
 
162
                                chars[i] = (char) buffer[i];
 
163
                        return chars;
 
164
                }
 
165
 
 
166
                public Clob getClob() throws SerialException, SQLException
 
167
                {
 
168
                        return new javax.sql.rowset.serial.SerialClob(getChars());
 
169
                }
 
170
                
 
171
                public Reader getReader()
 
172
                {
 
173
                        return new CharArrayReader(getChars());
 
174
                }
 
175
        }
 
176
 
 
177
        public static class BigDownload extends Thread {
 
178
                public void run() {
 
179
                        Connection conn = null;
 
180
 
 
181
                        try {
 
182
                                conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=&enableBlobStreaming=true");
 
183
 
 
184
                                ResultSet rs = executeQuery(conn, "select n_text from mybs_test_tab order by n_id");
 
185
                                while (rs.next()) {
 
186
                                        Blob blob = rs.getBlob(1);
 
187
                                        InputStream in = blob.getBinaryStream();
 
188
                                        try {
 
189
                                                for (int i=0; i<blob.length(); i++) {
 
190
                                                        int ch = in.read();
 
191
                                                        if (ch != (i%256))
 
192
                                                                throw new IOException("Error reading stream");
 
193
                                                }
 
194
                                        }
 
195
                                        finally {
 
196
                                                in.close();
 
197
                                        }
 
198
                                }
 
199
                        }
 
200
                        catch (SQLException ex) {
 
201
                                // handle any errors
 
202
                                System.out.println("SQLException: " + ex.getMessage());
 
203
                                System.out.println("SQLState: " + ex.getSQLState());
 
204
                                System.out.println("VendorError: " + ex.getErrorCode());
 
205
                                ex.printStackTrace();
 
206
                        }
 
207
                        catch (Exception e2) {
 
208
                                e2.printStackTrace();
 
209
                        }
 
210
                        finally {
 
211
                                try {
 
212
                                        if (conn != null)
 
213
                                                conn.close();
 
214
                                }
 
215
                                catch (Exception e3) {
 
216
                                        e3.printStackTrace();
 
217
                                }
 
218
                        }
 
219
                }
 
220
        }
 
221
 
 
222
        public static void bigDataTest(Connection conn) throws IOException, SQLException
 
223
        {
 
224
                System.out.println("BIG DATA TEST:");
 
225
 
 
226
                execute(conn, "delete from mybs_test_tab");
 
227
 
 
228
                Object list[] = new Object[1];
 
229
 
 
230
                for (int i=0; i<10; i++) {
 
231
                        list[0] = new BigByteStream(100000);
 
232
                        execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
233
                }
 
234
 
 
235
                ResultSet rs = executeQuery(conn, "select n_text from mybs_test_tab order by n_id");
 
236
                while (rs.next()) {
 
237
                        Blob blob = rs.getBlob(1);
 
238
                        InputStream in = blob.getBinaryStream();
 
239
                        try {
 
240
                                for (int i=0; i<blob.length(); i++) {
 
241
                                        int ch = in.read();
 
242
                                        if (ch != (i%256))
 
243
                                                throw new IOException("Error reading stream");
 
244
                                }
 
245
                        }
 
246
                        finally {
 
247
                                in.close();
 
248
                        }
 
249
                }
 
250
 
 
251
                System.out.println("Getting data using multiple threads...");
 
252
 
 
253
                int threads = 5;
 
254
                BigDownload t[] = new BigDownload[threads];
 
255
 
 
256
                for (int i=0; i<threads; i++)
 
257
                        t[i] = new BigDownload();
 
258
 
 
259
                try {
 
260
                        for (int i=0; i<threads; i++)
 
261
                                t[i].start();
 
262
                        for (int i=0; i<threads; i++)
 
263
                                t[i].join();
 
264
                }
 
265
                catch (Exception e) {
 
266
                        e.printStackTrace();
 
267
                }
 
268
 
 
269
                System.out.println("Done OK");
 
270
        }
 
271
 
 
272
        public static void funcTest(Connection conn) throws IOException, SQLException
 
273
        {
 
274
                System.out.println("FUNCTIONALITY TEST:");
 
275
 
 
276
                execute(conn, "delete from mybs_test_tab");
 
277
 
 
278
                Object list[] = new Object[1];
 
279
 
 
280
                list[0] = new StreamBuffer(SET_ASCII_STREAM_TEST, "ABC This is a test stream DEF".getBytes());
 
281
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
282
 
 
283
                list[0] = new StreamBuffer(SET_BINARY_STREAM_TEST, "ABC This is a test stream DEF".getBytes());
 
284
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
285
 
 
286
                list[0] = new StreamBuffer(SET_BLOB_TEST, "ABC This is a test stream DEF".getBytes());
 
287
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
288
 
 
289
                list[0] = new StreamBuffer(SET_ASCII_STREAM_TEST, "".getBytes());
 
290
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
291
 
 
292
                list[0] = new StreamBuffer(SET_BINARY_STREAM_TEST, "".getBytes());
 
293
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
294
 
 
295
                list[0] = new StreamBuffer(SET_BLOB_TEST, "".getBytes());
 
296
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
297
 
 
298
                list[0] = null;
 
299
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
300
 
 
301
                list[0] = null;
 
302
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
303
 
 
304
                list[0] = null;
 
305
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
306
 
 
307
                /* Characters are not streamed
 
308
                list[0] = new StreamBuffer(SET_CLOB_TEST, "ABC This is a test stream DEF".getBytes());
 
309
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
310
 
 
311
                list[0] = new StreamBuffer(SET_CHAR_STREAM_TEST, "ABC This is a test stream DEF".getBytes());
 
312
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
313
                */
 
314
 
 
315
                /* Not working yet
 
316
                list[0] = new StreamBuffer(SET_BLOB_STREAM_TEST, "ABC This is a test stream DEF".getBytes());
 
317
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
318
                list[0] = new StreamBuffer(SET_CLOB_READER_TEST, "ABC This is a test stream DEF".getBytes());
 
319
                execute(conn, "insert mybs_test_tab(n_text) values(?)", list);
 
320
                */
 
321
 
 
322
                /* Read the back, in various ways: */
 
323
                System.out.println("getBinaryStream():");
 
324
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_BINARY_STREAM_TEST);
 
325
                System.out.println("getString():");
 
326
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_STRING_TEST);
 
327
                System.out.println("getBytes():");
 
328
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_BYTES_TEST);
 
329
                System.out.println("getAsciiStream():");
 
330
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_ASCII_STREAM_TEST);
 
331
                System.out.println("getBlob():");
 
332
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_BLOB_TEST);
 
333
                System.out.println("getCharacterStream():");
 
334
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_CHAR_STREAM_TEST);
 
335
                System.out.println("getClob():");
 
336
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_CLOB_TEST);
 
337
                System.out.println("getObject():");
 
338
                execute(conn, "select n_id, n_text from mybs_test_tab order by n_id", GET_OBJECT_TEST);
 
339
        }
 
340
 
 
341
        static final int GET_ASCII_STREAM_TEST = 1;
 
342
        static final int GET_BINARY_STREAM_TEST = 2;
 
343
        static final int GET_BLOB_TEST = 3;
 
344
        static final int GET_BYTES_TEST = 4;
 
345
        static final int GET_CHAR_STREAM_TEST = 5;
 
346
        static final int GET_CLOB_TEST = 6;
 
347
        static final int GET_STRING_TEST = 7;
 
348
        static final int GET_OBJECT_TEST = 8;
 
349
 
 
350
        public static class ByteBuffer {
 
351
                int             pos = 0;
 
352
                byte    buffer[] = null;
 
353
                
 
354
                public void append(int val)
 
355
                {
 
356
                        if (buffer == null)
 
357
                                buffer = new byte[10];
 
358
                        else if (pos ==  buffer.length) {
 
359
                                byte new_buffer[];
 
360
                                
 
361
                                new_buffer = new byte[buffer.length + 10];
 
362
                                System.arraycopy(buffer, 0, new_buffer, 0, buffer.length);
 
363
                                buffer = new_buffer;
 
364
                        }
 
365
                        buffer[pos] = (byte) val;
 
366
                        pos++;
 
367
                }
 
368
                
 
369
                byte[] getBytes()
 
370
                {
 
371
                        byte new_buffer[];
 
372
                        
 
373
                        if (buffer == null)
 
374
                                return null;
 
375
                        new_buffer = new byte[pos];
 
376
                        System.arraycopy(buffer, 0, new_buffer, 0, pos);
 
377
                        return new_buffer;
 
378
                }
 
379
        }
 
380
 
 
381
        public static void printValue(Object obj) throws SQLException, IOException
 
382
        {
 
383
                if (obj instanceof Blob) {
 
384
                        // getBlob
 
385
                        obj = ((Blob) obj).getBinaryStream();
 
386
                }
 
387
                else if (obj instanceof Clob) {
 
388
                        //      getClob
 
389
                        obj = ((Clob) obj).getCharacterStream();
 
390
                }
 
391
 
 
392
                if (obj instanceof InputStream) {
 
393
                        // getAsciiStream, getBinaryStream
 
394
                        ByteBuffer      buf = new ByteBuffer();
 
395
 
 
396
                        try {
 
397
                                int ch;
 
398
                                
 
399
                                for (;;) {
 
400
                                        ch = ((InputStream) obj).read();
 
401
                                        if (ch == -1)
 
402
                                                break;
 
403
                                        buf.append(ch);
 
404
                                }
 
405
                        }
 
406
                        finally {
 
407
                                ((InputStream) obj).close();
 
408
                        }
 
409
                        obj = buf.getBytes();
 
410
                }
 
411
 
 
412
                if (obj instanceof Reader) {
 
413
                        // getCharacterStream
 
414
                        try {
 
415
                                int ch;
 
416
                                
 
417
                                for (;;) {
 
418
                                        ch = ((Reader) obj).read();
 
419
                                        if (ch == -1)
 
420
                                                break;
 
421
                                        System.out.print((char) ch);
 
422
                                }
 
423
                                System.out.flush();
 
424
                        }
 
425
                        finally {
 
426
                                ((Reader) obj).close();
 
427
                        }
 
428
                }
 
429
                else if (obj instanceof String) {
 
430
                        //      getString
 
431
                        System.out.print((String) obj);
 
432
                }
 
433
                else if (obj instanceof byte[]) {
 
434
                        //      getBytes
 
435
                        byte    buffer[] = (byte[]) obj;
 
436
                        boolean binary = false;
 
437
 
 
438
                        for (int i=0; i<buffer.length; i++) {
 
439
                                if (buffer[i] != '\n' && buffer[i] != '\r' && !(buffer[i] >= ' ' && buffer[i] <= 127)) {
 
440
                                        binary = true;
 
441
                                        break;
 
442
                                }
 
443
                        }
 
444
                        
 
445
                        if (binary) {
 
446
                                int ch;
 
447
 
 
448
                                for (int i=0; i<buffer.length; i++) {
 
449
                                        ch = buffer[i] >> 4;
 
450
                                        if (ch >= 0 && ch <= 9)
 
451
                                                System.out.print((char) ('0' + ch));
 
452
                                        else 
 
453
                                                System.out.print((char) ('A' + ch - 10));
 
454
                                        ch = buffer[i] & 0xF;
 
455
                                        if (ch >= 0 && ch <= 9)
 
456
                                                System.out.print((char) ('0' + ch));
 
457
                                        else 
 
458
                                                System.out.print((char) ('A' + ch - 10));
 
459
                                }
 
460
                        }
 
461
                        else {
 
462
                                for (int i=0; i<buffer.length; i++)
 
463
                                        System.out.print((char) buffer[i]);
 
464
                        }
 
465
                                
 
466
                }
 
467
                else if (obj == null)
 
468
                        System.out.print("null");
 
469
                else
 
470
                        System.out.print(obj.toString());
 
471
                System.out.flush();
 
472
        }
 
473
 
 
474
        public static void printResult(ResultSet rs, int testType) throws SQLException, IOException, SerialException
 
475
        {
 
476
                ResultSetMetaData md = rs.getMetaData();
 
477
                int colsPerRow = md.getColumnCount();
 
478
                int rows = 0;
 
479
                int cnt = 1;
 
480
 
 
481
                while (rs.next()) {
 
482
                        rows++;
 
483
                        for (int i=1; i<=colsPerRow; i++) {
 
484
                                String name = md.getTableName(i)+"."+md.getColumnName(i);
 
485
                                String type = md.getColumnTypeName(i) + "(" + md.getScale(i) + "," + md.getPrecision(i) + ")";
 
486
                                System.out.print(i + "> " + name + " " + type + " ");
 
487
                                
 
488
                                Object obj = null;
 
489
                                switch (testType) {
 
490
                                        case GET_ASCII_STREAM_TEST:
 
491
                                                obj = rs.getAsciiStream(i);
 
492
                                                break;
 
493
                                        case GET_BINARY_STREAM_TEST:
 
494
                                                obj = rs.getBinaryStream(i);
 
495
                                                break;
 
496
                                        case GET_BLOB_TEST:
 
497
                                                obj = rs.getBlob(i);
 
498
                                                break;
 
499
                                        case GET_BYTES_TEST:
 
500
                                                obj = rs.getBytes(i);
 
501
                                                break;
 
502
                                        case GET_CHAR_STREAM_TEST:
 
503
                                                obj = rs.getCharacterStream(i);
 
504
                                                break;
 
505
                                        case GET_CLOB_TEST:
 
506
                                                obj = rs.getClob(i);
 
507
                                                break;
 
508
                                        case GET_STRING_TEST:
 
509
                                                obj = rs.getString(i);
 
510
                                                break;
 
511
                                        case GET_OBJECT_TEST:
 
512
                                                obj = rs.getObject(i);
 
513
                                                break;
 
514
                                }
 
515
                                if (rs.wasNull())
 
516
                                        System.out.println("NULL");
 
517
                                else {
 
518
                                        printValue(obj);
 
519
                                        System.out.println("");
 
520
                                }
 
521
                        }
 
522
                        System.out.println("");
 
523
                }
 
524
                System.out.println("Row count = "+rows);
 
525
        }
 
526
 
 
527
        public static void printAll(Statement stat, boolean more, int testType) throws SQLException, IOException, SerialException
 
528
        {
 
529
                ResultSet rs;
 
530
 
 
531
                for(;;) {
 
532
                        if (more) {
 
533
                                rs = stat.getResultSet();
 
534
                                printResult(rs, testType);
 
535
                        }
 
536
                        else {
 
537
                                int cnt = stat.getUpdateCount();
 
538
                                if (cnt == -1)
 
539
                                        break;
 
540
                                System.out.println("Update count = " + cnt);
 
541
                        }
 
542
                        more = stat.getMoreResults();
 
543
                }
 
544
                System.out.println("Execution completed.");
 
545
        }
 
546
 
 
547
        public static void execute(Connection conn, String sql, int testType, Object list[]) throws SQLException, IOException, SerialException
 
548
        {
 
549
                Statement       st;
 
550
                boolean         more;
 
551
 
 
552
                if (list == null) {
 
553
                        st = conn.createStatement();
 
554
                        more = st.execute(sql);
 
555
                }
 
556
                else {
 
557
                        PreparedStatement ps;
 
558
 
 
559
                        ps = conn.prepareStatement(sql);
 
560
                        for (int i=0; i<list.length; i++) {
 
561
                                if (list[i] instanceof StreamBuffer) {
 
562
                                        StreamBuffer bb = (StreamBuffer) list[i];
 
563
                                        switch (bb.type) {
 
564
                                                case SET_ASCII_STREAM_TEST:
 
565
                                                        ps.setAsciiStream(i+1, bb.getStream(), (int) bb.getLength());
 
566
                                                        break;
 
567
                                                case SET_BINARY_STREAM_TEST:
 
568
                                                        ps.setBinaryStream(i+1, bb.getStream(), (int) bb.getLength());
 
569
                                                        break;
 
570
                                                case SET_BLOB_TEST:
 
571
                                                        ps.setBlob(i+1, bb.getBlob());
 
572
                                                        break;
 
573
                                                case SET_BLOB_STREAM_TEST:
 
574
                                                        //ps.setBlob(i+1, bb.getStream(), bb.getLength());
 
575
                                                        break;
 
576
                                                case SET_CHAR_STREAM_TEST:
 
577
                                                        ps.setCharacterStream(i+1, bb.getReader(), (int) bb.getLength());
 
578
                                                        break;
 
579
                                                case SET_CLOB_TEST:
 
580
                                                        ps.setClob(i+1, bb.getClob());
 
581
                                                        break;
 
582
                                                case SET_CLOB_READER_TEST:
 
583
                                                        //ps.setClob(i+1, bb.getReader(), bb.getLength());
 
584
                                                        break;
 
585
                                        }
 
586
                                }
 
587
                                else if (list[i] instanceof BigByteStream)
 
588
                                        ps.setBinaryStream(i+1, (InputStream) list[i], (int) ((BigByteStream) list[i]).getLength());
 
589
                                else
 
590
                                        ps.setObject(i+1, list[i]);
 
591
                        }
 
592
                        more = ps.execute();
 
593
                        st = ps;
 
594
                }
 
595
                
 
596
                try { 
 
597
                        printAll(st, more, testType);
 
598
                }
 
599
                finally {
 
600
                        st.close();
 
601
                }
 
602
        }
 
603
 
 
604
        public static void insertBLOB(Connection conn, InputStream in, int length) throws SQLException
 
605
        {
 
606
                PreparedStatement ps = null;
 
607
 
 
608
                try {
 
609
                        ps = conn.prepareStatement("insert mybs_test_tab(n_text) values(?)");
 
610
                        ps.setBinaryStream(1, in, (int) length);
 
611
                        int rows = ps.executeUpdate();
 
612
                }
 
613
                finally {
 
614
                        if (ps != null)
 
615
                                ps.close();
 
616
                }
 
617
        }
 
618
 
 
619
        public static InputStream selectBLOB(Connection conn, int id) throws SQLException
 
620
        {
 
621
                Statement   st = null;
 
622
                InputStream in = null;
 
623
 
 
624
                try {
 
625
                        st = conn.createStatement();
 
626
                        ResultSet rs = st.executeQuery("select n_text from mybs_test_tab where n_id = "+id);
 
627
                        rs.next();
 
628
                        in = rs.getBinaryStream("n_text");
 
629
                }
 
630
                finally {
 
631
                        if (st != null)
 
632
                                st.close();
 
633
                }
 
634
                return in;
 
635
        }
 
636
 
 
637
        public static void execute(Connection conn, String sql, int testType) throws SQLException, IOException
 
638
        {
 
639
                execute(conn, sql, testType, null);
 
640
        }
 
641
 
 
642
        public static void execute(Connection conn, String sql) throws SQLException, IOException
 
643
        {
 
644
                execute(conn, sql, GET_STRING_TEST, null);
 
645
        }
 
646
 
 
647
        public static void execute(Connection conn, String sql, Object list[]) throws SQLException, IOException
 
648
        {
 
649
                execute(conn, sql, GET_STRING_TEST, list);
 
650
        }
 
651
 
 
652
        public static ResultSet executeQuery(Connection conn, String sql) throws SQLException, IOException
 
653
        {
 
654
                Statement       st;
 
655
 
 
656
                st = conn.createStatement();
 
657
                return st.executeQuery(sql);
 
658
        }
 
659
 
 
660
}