// Compile it by // javac -classpath /usr/local/pgsql/java/devpgjdbc3.jar psql_lob.java // // Run it by // java -classpath .:/usr/local/pgsql/java/devpgjdbc3.jar Hello import java.io.*; import java.sql.*; import org.postgresql.largeobject.*; class Hello { public static void main(String args[]) throws Exception { Class.forName("org.postgresql.Driver"); Connection conn = DriverManager.getConnection( "jdbc:postgresql://sql.csic.umd.edu/YOUR_USERNAME", "YOUR_USERNAME", "YOUR_PASSWROD"); Statement stat = conn.createStatement(); //For LargeObject API calls, they must be within a transaction conn.setAutoCommit(false); //create table, lobid stat.executeUpdate("CREATE TABLE hello (lobid int)"); //create and open a PostgreSQL large object LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); int lobid = lobj.create(); LargeObject obj = lobj.open(lobid, LargeObjectManager.WRITE); //write data to the large object //h+w may be very long, we divide it into short strings String h = "hello ", w = "world!"; obj.write(h.getBytes(), 0, h.length()); obj.write(w.getBytes(), 0, w.length()); obj.close(); //insert lobid stat.executeUpdate("INSERT INTO hello VALUES (" + lobid + ")"); //select query ResultSet res = stat.executeQuery("SELECT * FROM hello "); if (res != null) for(; res.next();) { lobid = res.getInt(1); obj = lobj.open(lobid, LargeObjectManager.READ); byte buf[] = new byte[4]; int n = obj.size(), k; for(int i = 1; i <= n; i += 4) //get large object part by part { k = obj.read(buf, 0, 4); System.out.println("lob: " + new String(buf, 0, k)); } obj.close(); } res.close(); stat.close(); conn.close(); } }