/** * <p>Closes this input stream and releases any system resources associated with the stream.</p> * * <p>The <code>close</code> method of <code>InputStream</code> does nothing.</p> * * @throws IOException if an I/O error occurs. */ public void close() throws IOException { if (lo != null) { try { lo.close(); lo = null; } catch (SQLException se) { throw new IOException(se.toString()); } } }
public void close() throws IOException { if (lo != null) { try { flush(); lo.close(); lo = null; } catch (SQLException se) { throw new IOException(se.toString()); } } }
/** * Closes this input stream and releases any system resources associated * with the stream. * * <p> The <code>close</code> method of <code>InputStream</code> does * nothing. * * @exception IOException if an I/O error occurs. */ public void close() throws IOException { if (lo != null) { try { lo.close(); lo = null; } catch (SQLException se) { throw new IOException(se.toString()); } } }
/** * Closes this output stream and releases any system resources * associated with this stream. The general contract of <code>close</code> * is that it closes the output stream. A closed stream cannot perform * output operations and cannot be reopened. * <p> * The <code>close</code> method of <code>OutputStream</code> does nothing. * * @exception IOException if an I/O error occurs. */ public void close() throws IOException { if (lo != null) { try { flush(); lo.close(); lo = null; } catch (SQLException se) { throw new IOException(se.toString()); } } }
public void setBytes(int parameterIndex, byte[] x) throws SQLException { checkClosed(); if (null == x) { setNull(parameterIndex, Types.VARBINARY); return ; } if (connection.haveMinimumCompatibleVersion("7.2")) { //Version 7.2 supports the bytea datatype for byte arrays byte[] copy = new byte[x.length]; System.arraycopy(x, 0, copy, 0, x.length); preparedParameters.setBytea( parameterIndex, copy, 0, x.length); } else { //Version 7.1 and earlier support done as LargeObjects LargeObjectManager lom = connection.getLargeObjectAPI(); long oid = lom.createLO(); LargeObject lob = lom.open(oid); lob.write(x); lob.close(); setLong(parameterIndex, oid); } }
LargeObject lob = lom.open(getLong(columnIndex)); byte buf[] = lob.read(lob.size()); lob.close(); return trimBytes(columnIndex, buf);
/** * Closes this input stream and releases any system resources associated * with the stream. * * <p> The <code>close</code> method of <code>InputStream</code> does * nothing. * * @exception IOException if an I/O error occurs. */ public void close() throws IOException { if (lo != null) { try { lo.close(); lo = null; } catch (SQLException se) { throw new IOException(se.toString()); } } }
@Override public void close() throws IOException { if (!closed) { try { obj.close(); } catch (SQLException e) { throw new IOException("Error closing large object", e); } finally { try { con.commit(); con.setAutoCommit(commitMode); con.close(); } catch (Exception e) { throw new IOException("Error closing connection.", e); } finally { closed = true; } } } } }
@Override public void close() throws IOException { if (!closed) { try { obj.close(); } catch (SQLException e) { throw new IOException("Error closing large object", e); } finally { try { con.commit(); con.setAutoCommit(commitMode); con.close(); } catch (Exception e) { throw new IOException("Error closing connection.", e); } finally { closed = true; } } } } }
@Override public void close() throws IOException { try { if (obj != null) { obj.close(); } obj = null; } catch (SQLException e) { throw new IOException("Error closing large object", e); } finally { if (con != null) { try { con.commit(); con.setAutoCommit(commitMode); con.close(); } catch (SQLException e) { throw new IOException("Error closing connection", e); } con = null; } } } }
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException { Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test01", "postgres", "postgres"); conn.setAutoCommit(false); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select id, name, content from test"); LargeObjectManager lom = PGConnection.class.cast(conn).getLargeObjectAPI(); byte[] buffer = new byte[2048]; int s = 0; while(rs.next()) { File file = new File("/tmp", rs.getLong("id") + "_" + rs.getString("name")); FileOutputStream fos = new FileOutputStream(file); LargeObject lob = lom.open(rs.getLong("content"), LargeObjectManager.READ); while((s = lob.read(buffer, 0, buffer.length)) > 0) { fos.write(buffer, 0, buffer.length); } lob.close(); fos.close(); } conn.close(); }
// Get the Large Object Manager to perform operations with LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); PreparedStatement ps = conn.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?"); ps.setString(1, "myimage.gif"); ResultSet rs = ps.executeQuery(); while (rs.next()) { // Open the large object for reading int oid = rs.getInt(1); LargeObject obj = lobj.open(oid, LargeObjectManager.READ); // Read the data byte buf[] = new byte[obj.size()]; obj.read(buf, 0, obj.size()); // Do something with the data read here // Close the object obj.close(); } rs.close(); ps.close();
LargeObjectManager largeObjectManager = null; LargeObject largeObject = null; Long largeObjectRef = null; largeObjectManager = ((org.postgresql.PGConnection) connection).getLargeObjectAPI(); largeObjectRef = largeObjectManager.createLO(LargeObjectManager.READ | LargeObjectManager.WRITE); largeObject = largeObjectManager.open(largeObjectRef, LargeObjectManager.WRITE); largeObject.write("string/file content to write into oid column".getBytes()); largeObject.close();
// All LargeObject API calls must be within a transaction block conn.setAutoCommit(false); // Get the Large Object Manager to perform operations with LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); PreparedStatement ps = conn.prepareStatement("SELECT imgoid FROM imageslo WHERE imgname = ?"); ps.setString(1, "myimage.gif"); ResultSet rs = ps.executeQuery(); while (rs.next()) { // Open the large object for reading int oid = rs.getInt(1); LargeObject obj = lobj.open(oid, LargeObjectManager.READ); // Read the data byte buf[] = new byte[obj.size()]; obj.read(buf, 0, obj.size()); // Do something with the data read here // Close the object obj.close(); } rs.close(); ps.close(); // Finally, commit the transaction. conn.commit();
private int getPostGresLargeObjectSize(JDBCSequentialFile file) throws SQLException { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); int size = 0; Long oid = getOID(file); if (oid != null) { synchronized (connection) { try { connection.setAutoCommit(false); LargeObject largeObject = lobjManager.open(oid, LargeObjectManager.READ); size = largeObject.size(); largeObject.close(); connection.commit(); } catch (SQLException e) { connection.rollback(); throw e; } } } return size; } }
@Override public int writeToFile(JDBCSequentialFile file, byte[] data) throws SQLException { synchronized (connection) { LargeObjectManager lobjManager = ((PGConnection) connection).getLargeObjectAPI(); LargeObject largeObject = null; Long oid = getOID(file); try { connection.setAutoCommit(false); largeObject = lobjManager.open(oid, LargeObjectManager.WRITE); largeObject.seek(largeObject.size()); largeObject.write(data); largeObject.close(); connection.commit(); } catch (Exception e) { connection.rollback(); throw e; } return data.length; } }