日期:2014-05-16  浏览次数:20465 次

Oracle数据库Clobl类型的插入
int id =1;
PreparedStatement ps = conn.prepareStatement("select knowledge_SEQ.nextval from dual");
ResultSet rs = ps.executeQuery();
if (rs.next()) {// 查询序列值:Id
id = rs.getInt(1);
}
rs.close();

String sql = "insert into knowledge(id,title,knowledgeClass,addtime,userid,departlist,content,keyword,sh,introduction,click";
if (knowledge.getFileName() != null
&& !"".equals(knowledge.getFileName())&&knowledge.getFilePath() != null
&& !"".equals(knowledge.getFilePath())) {
sql+=",fileName,filePath) values("+id+",?,?,?,?,?,?,?,?,?,?,'"+knowledge.getFileName()+"','"+knowledge.getFilePath()+"')";
} else {
sql+=") values("+id+",?,?,?,?,?,?,?,?,?,?)";
}
ps = conn.prepareStatement(sql);
ps.setString(1, knowledge.getTitle());
ps.setInt(2, knowledgeClassId);
ps.setString(3, knowledge.getAddtime());
ps.setInt(4, userId);
ps.setInt(5, departId);
ps.setClob(6, CLOB.empty_lob());
ps.setString(7, knowledge.getKeyword());
ps.setInt(8, knowledge.getSh());
ps.setString(9, knowledge.getIntroduction());
ps.setInt(10, knowledge.getClick());

ps.executeUpdate();

sql= "select content from knowledge where id="+id+" for update";
ps = conn.prepareStatement(sql);
ResultSet rs_clob = ps.executeQuery();
while ( rs_clob .next()) {
/* 取出clob数据*/
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs_clob .getClob(1);

/* 向clob中写入数据*/
clob.putString(1, knowledge.getContent());
}
rs_clob.close();

ps.close();
注意:若是本身添加信息内容较少时,不使用clob的相关操作,虽暂时可添加成功,但大量数据量时有异常