您的位置:首页 > 数据库 > Oracle

java操作oracle中的clob

2011-07-15 10:50 381 查看
package com.chinacreator.oraclelob;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.sql.CLOB;
import com.creator.util.Constants;
public class TestCLOB {
public static void main(String[] args) {
//insertCLOB();
ReadCLOB();
}

//clob入库
public static void insertCLOB() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

String sql = "insert into clobtest(id, content) values(1, empty_clob())";

try {
Class.forName(Constants.DRIVER);
conn = DriverManager.getConnection(Constants.URL, Constants.DB_USER, Constants.DB_PSW);
conn.setAutoCommit(false);
pstmt = conn.prepareStatement(sql);
pstmt.executeUpdate();
//锁定数据行进行更新,注意"for update"语句
sql = "select content from clobtest where id=1 for update" ;
rs = conn.createStatement().executeQuery(sql);

Writer out = null;
while(rs.next()) {
CLOB content = (CLOB)rs.getClob("content");
out = content.getCharacterOutputStream();
//定义一个字符串
String data = "我的中国心";
char[] chars = data.toCharArray();
out.write(chars, 0, chars.length);
out.flush();
}
out.close();
conn.commit();
System.out.println("success!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}

//clob出库
public static void ReadCLOB() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

String sql = "select content from clobtest where id = 1";

try {
Class.forName(Constants.DRIVER);
conn = DriverManager.getConnection(Constants.URL, Constants.DB_USER, Constants.DB_PSW);
conn.setAutoCommit(false);

pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
Reader reader = null;
String temp = "";
while(rs.next()) {
CLOB content = (CLOB)rs.getClob("content");
reader = content.getCharacterStream();

int length = (int)content.getLength();
char[] c = new char[length];
reader.read(c);
temp = new String(c);
}
reader.close();
conn.commit();
System.out.println("success!");
System.out.println("temp:"+temp);

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
} finally {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: