package acspg; import javax.activation.*; import java.sql.*; import java.io.*; public class ClobOutputStream extends ByteArrayOutputStream { String isql = null; Pg_Query query = null; public ClobOutputStream(String str, String isql, Pg_Query q) throws IOException { super(); if(str != null) this.write(str.getBytes()); this.isql = isql; this.query = q; } public ClobOutputStream(int i, String str, String isql, Pg_Query q) throws IOException { super(i); if(str != null) this.write(str.getBytes()); this.isql = isql; this.query = q; } public void close() throws IOException { try { int i = this.isql.indexOf("%s"); String q = this.isql.substring(0,i) + "'" + escapeSql(this.toString()) + "'" + this.isql.substring(i+2); System.out.println("Query: " + q); this.query.executeUpdate(q); } catch (Exception e) { System.out.println("err: " + e.getMessage()); throw new IOException("Insert failed: " + e.getMessage()); } } public String escapeSql(String q) { int len = q.length(); StringBuffer sb = new StringBuffer(); char c; for(int i = 0; i < len; i++) { c = q.charAt(i); if(c == '\'') sb.append("\'\'"); else sb.append(c); } return sb.toString(); } }