package com.db;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import oracle.sql.CLOB;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.entity.Article;
public class CrudClob {
public boolean save(Article article){
boolean result = true;
Connection conn = ConnectionUntils.getInstance();
String sql = "insert into article values(?,?,empty_clob())";
//锁住该列,防止并发写入时候该字段同时被多次写入造成错误
String sqlClob = "select a_content from article where a_id=? for update";
PreparedStatement pst =null;
ResultSet rs = null;
Writer writer = null;
try {
conn.setAutoCommit(false);//设置不自动提交,开启事务
pst = conn.prepareStatement(sql);
pst.setInt(1,article.getId());
pst.setString(2,article.getName());
pst.executeUpdate();
pst= conn.prepareStatement(sqlClob);
pst.setInt(1, article.getId());
rs = pst.executeQuery();
CLOB clob = null;
if(rs.next()){
try {
clob = (CLOB) rs.getClob(1);
writer = clob.getCharacterOutputStream();
writer.write(article.getContent());
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
conn.commit();
} catch (SQLException e) {
result = false;
try {
conn.rollback();//当commit或者rollback后会自动释放该列的锁定
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
ConnectionUntils.close(rs, pst, conn);
}
return result;
}
public boolean del(int id){
int result = 0;
Connection conn = ConnectionUntils.getInstance();
String sql = "delete article where a_id=?";
PreparedStatement pst =null;
try {
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
result = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionUntils.close(null, pst, conn);
}
if(result==0)
return false;
return true;
}
/**
* 生成一个clob对象,通过预处理的setClob来更新
*/
public boolean update(int id,String content){
int result = 0;
Connection conn = ConnectionUntils.getInstance();
String sql = "update article set a_content=? where a_id=?";
PreparedStatement pst =null;
try {
CLOB clob = oracle.sql.CLOB.createTemporary(conn, false,oracle.sql.CLOB.DURATION_SESSION);
clob.setString(1L, content);
pst = conn.prepareStatement(sql);
pst.setClob(1, clob);
pst.setInt(2,id);
result = pst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
ConnectionUntils.close(null, pst, conn);
}
if(result==0)
return false;
return true;
}
public Article select(int id){
Article article = new Article();
Connection conn = ConnectionUntils.getInstance();
String sql = "select a_id,a_name,a_content from article where a_id = ?";
PreparedStatement pst =null;
ResultSet rs = null;
try {
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
rs = pst.executeQuery();
StringBuilder builder = new StringBuilder();
if(rs.next()){
Clob clob = rs.getClob("a_content");
Reader rd = clob.getCharacterStream();
char [] str = new char[12];
while(rd.read(str) != -1) {
builder.append(new String(str));
}
article.setContent(builder.toString());
article.setId(id);
article.setName(rs.getString("a_name"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
ConnectionUntils.close(rs, pst, conn);
}
return article;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DealClobWidthJdbc.zip (20个子文件)
DealClobWidthJdbc
.project 1KB
WebContent
WEB-INF
lib
ojdbc14.jar 1.47MB
web.xml 647B
index.jsp 357B
META-INF
MANIFEST.MF 39B
src
com
service
db
CrudClob.java 4KB
ConnectionUntils.java 2KB
entity
Article.java 576B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 578B
.jsdtscope 567B
build
classes
com
service
TestCrudClob.class 2KB
db
CrudClob.class 5KB
ConnectionUntils.class 2KB
entity
Article.class 1KB
test
com
service
TestCrudClob.java 1KB
.classpath 962B
共 20 条
- 1
资源评论
lishihong108
- 粉丝: 44
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功