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();
}
}
}
}
}
}
}
}
}
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();
}
}
}
}
}
}
}
}
}
相关文章推荐
- 环境:JAVA操作Oracle中BLOB、CLOB
- java 操作ORACLE Clob字段
- Java 对Oracle Clob(大字符串)格式的操作 增改查
- Java 对Oracle Clob(大字符串)格式的操作 增改查
- java 关于操作oracle的clob类型
- java中操作Oracle clob数据出现的连接关闭问题
- java 关于操作oracle的clob类型(inster ,update,select)的完整例程
- java操作Oracle的CLOB类型
- JAVA操作Oracle中BLOB、CLOB
- 从 Oracle 大字段(blob,clob)的读、写认识 Java JDBC操作全攻略
- java操作 oracle 的 blob和clob类型
- java操作Oracle中的BLOB和CLOB代码示例
- Java 对Oracle Clob(大字符串)格式的操作 增改查
- java操作oracle的blob,clob数据
- java操作oracle里面的clob
- java操作oracle存取clob类型数据
- JAVA操作Oracle中CLOB字段
- JAVA对Oracle中BLOB、CLOB类型字段的操作说明
- oracle使用java操作clob类型的字段
- java操作oracle的blob,clob数据