java 操作CLOB类型数据
clob类型,但对于这个类型处理起来还是比较麻烦的,varchar2长度为4000bytes,如果varchar2能满足楼主的需求,建议使用varchar2,下面提供了在Java 中读取clob类型的方式,我主要讲的是JDBC的操作,Hibernate目前不讲,操作这个Clob列主要分为插入和获取俩个操作:
1、Java程序插入Clob值到Oracle数据库表中:
String clob= "这里主要是要插入到表中的值";
//前面具体数据库连接代码这里省略,直接进正题
String sql = "insert into clob_test(id,str) values (1,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
// 将clob转成流形式
Reader clobReader = new StringReader(clob);
stmt.setCharacterStream(1,clobReader,clob.length());// 替换sql语句中的?
stmt.executeUpdate();
2、读取Clob列的值
String sql = "select str from clob_test where id=1";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
//最后获得的结果
String resultClob;
if(rs.next()){
//导入oracle.sql.CLOB包,或者直接引用这个包
oracle.sql.CLOB clob = rs.getClob("str");
Reader rd= clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(rd);
String s = br.readLine();
StringBuffer sb= new StringBuffer();
while (s != null) {
// 循环赋值
sb.append(s);
s = br.readLine();
}
clobResult = sb.toString();
}
http://zhidao.baidu.com/question/390951260.html
1、Java程序插入Clob值到Oracle数据库表中:
String clob= "这里主要是要插入到表中的值";
//前面具体数据库连接代码这里省略,直接进正题
String sql = "insert into clob_test(id,str) values (1,?)";
PreparedStatement stmt = conn.prepareStatement(sql);
// 将clob转成流形式
Reader clobReader = new StringReader(clob);
stmt.setCharacterStream(1,clobReader,clob.length());// 替换sql语句中的?
stmt.executeUpdate();
2、读取Clob列的值
String sql = "select str from clob_test where id=1";
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
//最后获得的结果
String resultClob;
if(rs.next()){
//导入oracle.sql.CLOB包,或者直接引用这个包
oracle.sql.CLOB clob = rs.getClob("str");
Reader rd= clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(rd);
String s = br.readLine();
StringBuffer sb= new StringBuffer();
while (s != null) {
// 循环赋值
sb.append(s);
s = br.readLine();
}
clobResult = sb.toString();
}
http://zhidao.baidu.com/question/390951260.html