apache derby clob example - JDBC JAVA
This is a discussion on apache derby clob example - JDBC JAVA ; If anyone could post a short and easy example of inserting a clob via
jdbc, that would be great. The docs don't have this....
-
apache derby clob example
If anyone could post a short and easy example of inserting a clob via
jdbc, that would be great. The docs don't have this.
-
Re: apache derby clob example
supermail99@fastmail.fm writes:
> If anyone could post a short and easy example of inserting a clob via
> jdbc, that would be great. The docs don't have this.
>
Actually, they do, but it is not that easy to find:
http://db.apache.org/derby/docs/10.1/ref/rrefclob.html
Btw. Not many derby people follow this group, so you will probably get
a quicker (and maybe better) response if you subscribe to
derby-user@db.apache.org
--
dt
-
Re: apache derby clob example
// Insert Record
void insertRecord(Connection derbyCon, String id, String clobContent) throws Exception
{ PreparedStatement ps = derbyCon.prepareStatement("INSERT INTO my_table VALUES (?,?)");
ps.setString(1, id);
ps.setAsciiStream(2, derbyAsciiStream(clobContent));
try
{ ps.execute();
}
finally
{ if (ps != null) ps.close();
}
}
ByteArrayInputStream derbyAsciiStream(String content) throws Exception
{ ByteArrayOutputStream b = new ByteArrayOutputStream();
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(b, "UTF8"));
w.write(content);
w.close();
return new ByteArrayInputStream(b.toByteArray());
}
// Read Record
String getClobContent(Connection derbyCon, String id) throws Exception
{ ResultSet rs = null;
PreparedStatement ps = null;
StringBuffer sb = new StringBuffer(
"SELECT clobContent FROM my_table WHERE UserID = '").append(id).append("'");
try
{ ps = derbyCon.prepareStatement(sb.toString());
rs = ps.executeQuery();
if (rs.next())
{ return getDerbyClobContent(rs.getClob("clobContent"));
}
throw new Exception("not found");
}
finally
{ if (rs != null) try { rs.close(); } catch (Exception ex) { }
if (ps != null) try { ps.close(); } catch (Exception ex) { }
}
}
String getDerbyClobContent(Clob derbyClob) throws Exception
{ BufferedInputStream in = new BufferedInputStream(derbyClob.getAsciiStream());
ByteArrayOutputStream bs = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(bs);
byte[] ioBuf = new byte[4096];
int bytesRead;
while ((bytesRead = in.read(ioBuf)) != -1) out.write(ioBuf, 0, bytesRead);
out.close();
in.close();
return new String(bs.toString());
}
Similar Threads
-
By Application Development in forum JDBC JAVA
Replies: 1
Last Post: 05-02-2007, 03:23 AM
-
By Application Development in forum JDBC JAVA
Replies: 1
Last Post: 07-08-2006, 01:25 PM
-
By Application Development in forum JDBC JAVA
Replies: 2
Last Post: 05-12-2006, 11:09 AM
-
By Application Development in forum JDBC JAVA
Replies: 1
Last Post: 03-20-2006, 12:58 PM
-
By Application Development in forum JDBC JAVA
Replies: 2
Last Post: 11-28-2004, 12:51 PM