~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/example/blobtest.java

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package example;
 
2
 
 
3
import java.io.*;
 
4
import java.sql.*;
 
5
import org.postgresql.largeobject.*;
 
6
 
 
7
/*
 
8
 * This test attempts to create a blob in the database, then to read
 
9
 * it back.
 
10
 *
 
11
 * Important note: You will notice we import the org.postgresql.largeobject
 
12
 * package, but don't import the org.postgresql package. The reason for this is
 
13
 * that importing postgresql can confuse javac (we have conflicting class names
 
14
 * in org.postgresql.* and java.sql.*). This doesn't cause any problems, as
 
15
 * long as no code imports org.postgresql.
 
16
 *
 
17
 * Under normal circumstances, code using any jdbc driver only needs to import
 
18
 * java.sql, so this isn't a problem.
 
19
 *
 
20
 * It's only if you use the non jdbc facilities, do you have to take this into
 
21
 * account.
 
22
 *
 
23
 */
 
24
 
 
25
public class blobtest
 
26
{
 
27
        Connection db;
 
28
        Statement s;
 
29
        LargeObjectManager lobj;
 
30
 
 
31
        public blobtest(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
 
32
        {
 
33
                String url = args[0];
 
34
                String usr = args[1];
 
35
                String pwd = args[2];
 
36
 
 
37
                // Load the driver
 
38
                Class.forName("org.postgresql.Driver");
 
39
 
 
40
                // Connect to database
 
41
                System.out.println("Connecting to Database URL = " + url);
 
42
                db = DriverManager.getConnection(url, usr, pwd);
 
43
 
 
44
                // This is required for all LargeObject calls
 
45
                System.out.println("Connected... First turn off autoCommit()");
 
46
                db.setAutoCommit(false);
 
47
 
 
48
                System.out.println("Now creating a statement");
 
49
                s = db.createStatement();
 
50
 
 
51
                // Now run tests using postgresql's own Large object api
 
52
                // NOTE: The methods shown in this example are _NOT_ JDBC, but are
 
53
                // an implementation of the calls found in libpq. Unless you need to
 
54
                // use this functionality, look at the jdbc tests on how to access blobs.
 
55
                ownapi();
 
56
 
 
57
                // Now run tests using JDBC methods
 
58
                //jdbcapi(db,s);
 
59
 
 
60
                // Finally close the database
 
61
                System.out.println("Now closing the connection");
 
62
                s.close();
 
63
                db.close();
 
64
        }
 
65
 
 
66
        /*
 
67
         * Now this is an extension to JDBC, unique to postgresql. Here we fetch
 
68
         * an PGlobj object, which provides us with access to postgresql's
 
69
         * large object api.
 
70
         */
 
71
        public void ownapi() throws FileNotFoundException, IOException, SQLException
 
72
        {
 
73
                System.out.println("\n----------------------------------------------------------------------\nTesting postgresql large object api\n----------------------------------------------------------------------\n");
 
74
 
 
75
                // Internally, the driver provides JDBC compliant methods to access large
 
76
                // objects, however the unique methods available to postgresql makes
 
77
                // things a little easier.
 
78
                System.out.println("Gaining access to large object api");
 
79
                lobj = ((org.postgresql.Connection)db).getLargeObjectAPI();
 
80
 
 
81
                int oid = ownapi_test1();
 
82
                ownapi_test2(oid);
 
83
 
 
84
                // Now call the jdbc2api test
 
85
                jdbc2api(oid);
 
86
 
 
87
                // finally delete the large object
 
88
                ownapi_test3(oid);
 
89
                System.out.println("\n\nOID=" + oid);
 
90
        }
 
91
 
 
92
        private int ownapi_test1() throws FileNotFoundException, IOException, SQLException
 
93
        {
 
94
                System.out.println("Test 1 Creating a large object\n");
 
95
 
 
96
                // Ok, test 1 is to create a large object. To do this, we use the create
 
97
                // method.
 
98
                System.out.println("Creating a large object");
 
99
                int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
 
100
                DriverManager.println("got large object oid=" + oid);
 
101
 
 
102
                LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
 
103
                DriverManager.println("got large object obj=" + obj);
 
104
 
 
105
                // Now open a test file - this class will do
 
106
                System.out.println("Opening test source object");
 
107
                FileInputStream fis = new FileInputStream("example/blobtest.java");
 
108
 
 
109
                // copy the data
 
110
                System.out.println("Copying file to large object");
 
111
                byte buf[] = new byte[2048];
 
112
                int s, tl = 0;
 
113
                while ((s = fis.read(buf, 0, 2048)) > 0)
 
114
                {
 
115
                        System.out.println("Block size=" + s + " offset=" + tl);
 
116
                        //System.out.write(buf);
 
117
                        obj.write(buf, 0, s);
 
118
                        tl += s;
 
119
                }
 
120
                DriverManager.println("Copied " + tl + " bytes");
 
121
 
 
122
                // Close the object
 
123
                System.out.println("Closing object");
 
124
                obj.close();
 
125
 
 
126
                return oid;
 
127
        }
 
128
 
 
129
        private void ownapi_test2(int oid) throws FileNotFoundException, IOException, SQLException
 
130
        {
 
131
                System.out.println("Test 2 Reading a large object and save as a file\n");
 
132
 
 
133
                // Now open the large object
 
134
                System.out.println("Opening large object " + oid);
 
135
                LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
 
136
                DriverManager.println("got obj=" + obj);
 
137
 
 
138
                // Now open a test file - this class will do
 
139
                System.out.println("Opening test destination object");
 
140
                FileOutputStream fos = new FileOutputStream("blob_testoutput");
 
141
 
 
142
                // copy the data
 
143
                System.out.println("Copying large object to file");
 
144
                byte buf[] = new byte[512];
 
145
                int s = obj.size();
 
146
                int tl = 0;
 
147
                while (s > 0)
 
148
                {
 
149
                        int rs = buf.length;
 
150
                        if (s < rs)
 
151
                                rs = s;
 
152
                        obj.read(buf, 0, rs);
 
153
                        fos.write(buf, 0, rs);
 
154
                        tl += rs;
 
155
                        s -= rs;
 
156
                }
 
157
                DriverManager.println("Copied " + tl + "/" + obj.size() + " bytes");
 
158
 
 
159
                // Close the object
 
160
                System.out.println("Closing object");
 
161
                obj.close();
 
162
        }
 
163
 
 
164
        private void ownapi_test3(int oid) throws SQLException
 
165
        {
 
166
                System.out.println("Test 3 Deleting a large object\n");
 
167
 
 
168
                // Now open the large object
 
169
                System.out.println("Deleting large object " + oid);
 
170
                lobj.unlink(oid);
 
171
        }
 
172
 
 
173
        // This tests the Blob interface of the JDBC 2.0 specification
 
174
        public void jdbc2api(int oid) throws SQLException, IOException
 
175
        {
 
176
                System.out.println("Testing JDBC2 Blob interface:");
 
177
                jdbc2api_cleanup();
 
178
 
 
179
                System.out.println("Creating Blob on large object " + oid);
 
180
                s.executeUpdate("create table basic (a oid)");
 
181
 
 
182
                System.out.println("Inserting row");
 
183
                s.executeUpdate("insert into basic values (" + oid + ")");
 
184
 
 
185
                System.out.println("Selecting row");
 
186
                ResultSet rs = s.executeQuery("select a from basic");
 
187
                if (rs != null)
 
188
                {
 
189
                        while (rs.next())
 
190
                        {
 
191
                                System.out.println("Fetching Blob");
 
192
                                Blob b = rs.getBlob("a");
 
193
                                System.out.println("Blob.length() = " + b.length());
 
194
                                System.out.println("Characters 400-500:");
 
195
                                System.out.write(b.getBytes(400l, 100));
 
196
                                System.out.println();
 
197
                        }
 
198
                        rs.close();
 
199
                }
 
200
 
 
201
                System.out.println("Cleaning up");
 
202
                jdbc2api_cleanup();
 
203
        }
 
204
 
 
205
        private void jdbc2api_cleanup() throws SQLException
 
206
        {
 
207
                db.setAutoCommit(true);
 
208
                try
 
209
                {
 
210
                        s.executeUpdate("drop table basic");
 
211
                }
 
212
                catch (Exception ex)
 
213
                {
 
214
                        // We ignore any errors here
 
215
                }
 
216
                db.setAutoCommit(false);
 
217
        }
 
218
 
 
219
        public static void instructions()
 
220
        {
 
221
                System.err.println("java example.blobtest jdbc-url user password [debug]");
 
222
                System.err.println("\nExamples:\n");
 
223
                System.err.println("java -Djdbc.driver=org.postgresql.Driver example.blobtest jdbc:postgresql:test postgres password\nThis will run the tests on the database test on the local host.\n");
 
224
                System.err.println("java -Djdbc.driver=org.postgresql.Driver example.blobtest jdbc:postgresql:test postgres password debug\nThis is the same as above, but will output debug information.\n");
 
225
 
 
226
                System.err.println("This example tests the binary large object api of the driver.\nThis allows images or java objects to be stored in the database, and retrieved\nusing both postgresql's own api, and the standard JDBC api.");
 
227
        }
 
228
 
 
229
        public static void main(String args[])
 
230
        {
 
231
                System.out.println("PostgreSQL blobtest v7.0 rev 1\n");
 
232
 
 
233
                if (args.length < 3)
 
234
                {
 
235
                        instructions();
 
236
                        System.exit(1);
 
237
                }
 
238
 
 
239
                // This line outputs debug information to stderr. To enable this, simply
 
240
                // add an extra parameter to the command line
 
241
                if (args.length > 3)
 
242
                        DriverManager.setLogStream(System.err);
 
243
 
 
244
                // Now run the tests
 
245
                try
 
246
                {
 
247
                        blobtest test = new blobtest(args);
 
248
                }
 
249
                catch (Exception ex)
 
250
                {
 
251
                        System.err.println("Exception caught.\n" + ex);
 
252
                        ex.printStackTrace();
 
253
                }
 
254
        }
 
255
}