5
import org.postgresql.largeobject.*;
8
* This test attempts to create a blob in the database, then to read
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.
17
* Under normal circumstances, code using any jdbc driver only needs to import
18
* java.sql, so this isn't a problem.
20
* It's only if you use the non jdbc facilities, do you have to take this into
29
LargeObjectManager lobj;
31
public blobtest(String args[]) throws ClassNotFoundException, FileNotFoundException, IOException, SQLException
38
Class.forName("org.postgresql.Driver");
40
// Connect to database
41
System.out.println("Connecting to Database URL = " + url);
42
db = DriverManager.getConnection(url, usr, pwd);
44
// This is required for all LargeObject calls
45
System.out.println("Connected... First turn off autoCommit()");
46
db.setAutoCommit(false);
48
System.out.println("Now creating a statement");
49
s = db.createStatement();
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.
57
// Now run tests using JDBC methods
60
// Finally close the database
61
System.out.println("Now closing the connection");
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
71
public void ownapi() throws FileNotFoundException, IOException, SQLException
73
System.out.println("\n----------------------------------------------------------------------\nTesting postgresql large object api\n----------------------------------------------------------------------\n");
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();
81
int oid = ownapi_test1();
84
// Now call the jdbc2api test
87
// finally delete the large object
89
System.out.println("\n\nOID=" + oid);
92
private int ownapi_test1() throws FileNotFoundException, IOException, SQLException
94
System.out.println("Test 1 Creating a large object\n");
96
// Ok, test 1 is to create a large object. To do this, we use the create
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);
102
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);
103
DriverManager.println("got large object obj=" + obj);
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");
110
System.out.println("Copying file to large object");
111
byte buf[] = new byte[2048];
113
while ((s = fis.read(buf, 0, 2048)) > 0)
115
System.out.println("Block size=" + s + " offset=" + tl);
116
//System.out.write(buf);
117
obj.write(buf, 0, s);
120
DriverManager.println("Copied " + tl + " bytes");
123
System.out.println("Closing object");
129
private void ownapi_test2(int oid) throws FileNotFoundException, IOException, SQLException
131
System.out.println("Test 2 Reading a large object and save as a file\n");
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);
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");
143
System.out.println("Copying large object to file");
144
byte buf[] = new byte[512];
152
obj.read(buf, 0, rs);
153
fos.write(buf, 0, rs);
157
DriverManager.println("Copied " + tl + "/" + obj.size() + " bytes");
160
System.out.println("Closing object");
164
private void ownapi_test3(int oid) throws SQLException
166
System.out.println("Test 3 Deleting a large object\n");
168
// Now open the large object
169
System.out.println("Deleting large object " + oid);
173
// This tests the Blob interface of the JDBC 2.0 specification
174
public void jdbc2api(int oid) throws SQLException, IOException
176
System.out.println("Testing JDBC2 Blob interface:");
179
System.out.println("Creating Blob on large object " + oid);
180
s.executeUpdate("create table basic (a oid)");
182
System.out.println("Inserting row");
183
s.executeUpdate("insert into basic values (" + oid + ")");
185
System.out.println("Selecting row");
186
ResultSet rs = s.executeQuery("select a from basic");
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();
201
System.out.println("Cleaning up");
205
private void jdbc2api_cleanup() throws SQLException
207
db.setAutoCommit(true);
210
s.executeUpdate("drop table basic");
214
// We ignore any errors here
216
db.setAutoCommit(false);
219
public static void instructions()
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");
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.");
229
public static void main(String args[])
231
System.out.println("PostgreSQL blobtest v7.0 rev 1\n");
239
// This line outputs debug information to stderr. To enable this, simply
240
// add an extra parameter to the command line
242
DriverManager.setLogStream(System.err);
247
blobtest test = new blobtest(args);
251
System.err.println("Exception caught.\n" + ex);
252
ex.printStackTrace();