- 浏览: 603109 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
limeng650419:
想要将一个项目类生成在同一个类图怎么办》?
PowerDesigner导入java类生成类图 -
q77102902:
一语道破天机啊!
Weblogic 数据源及连接池配置问题Warning! Connectivity to backend database not verified -
chaliszhou:
收藏下,!!!
JAVA读写文件,如何避免中文乱码 -
lal324:
同上 顶起来
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/di -
luchuan10000:
非常正确!!!
JAVA读写文件,如何避免中文乱码
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author semovy 测试向oracle 读,写文件Blob 读,写大文本Clob
*/
public class OracleBlobTest {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:teckotooling";
private String user = "scott";
private String pwd = "tiger";
public OracleBlobTest() {
}
public static void main(String[] args) {
OracleBlobTest obt = new OracleBlobTest();
obt.writeBlob();
obt.readBlob();
obt.writeClob();
obt.readClob();
}
/**
* 读二进制文件
*
*/
private void readBlob() {
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test where id=1");
byte[] buffer = new byte[1024];
OutputStream out = new FileOutputStream("d:/360安全卫士定1.exe");
int tempLen = 0;
int amount = 0;
if (rs.next()) {
Blob blob = rs.getBlob("BINARYCONTENT");
InputStream in = blob.getBinaryStream();
while ((tempLen = in.read(buffer)) != -1) {
out.write(buffer, 0, tempLen);
amount += tempLen;
System.out.println("已经读出并写:" + amount + " 字节");
}
System.out.println("已经读出并写:完成");
out.flush();
out.close();
in.close();
rs.close();
stmt.close();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 写二进制文件
*
*/
private void writeBlob() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
String sql = null;
Statement stmt = conn.createStatement();
sql = "delete from test where id=1";
stmt.executeUpdate(sql);
sql = "insert into test(1,BINARYCONTENT,CLOBCONTENT) values(1,empty_blob(),empty_clob())";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery("select * from test where id=1");
if (rs.next()) {
Blob blob = rs.getBlob("BINARYCONTENT");
OutputStream out = ((oracle.sql.BLOB) blob).setBinaryStream(0);// 从0开始,否则写出的文件有差错
int bufferSize = ((oracle.sql.BLOB) blob).getBufferSize();
System.out.println("bufferSize :" + bufferSize);
BufferedInputStream in = new BufferedInputStream(
new FileInputStream("d:/360安全卫士定.exe"), bufferSize);
byte[] b = new byte[bufferSize];
int count = in.read(b, 0, bufferSize);
int amount = 0;
while (count != -1) {
out.write(b, 0, count);
amount += count;
System.out.println("处理了 " + amount + " 字节");
count = in.read(b, 0, bufferSize);
System.out.println("处理了 " + amount + " 字节,成功");
}
out.close();
out = null;
in.close();
conn.commit();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
System.out.println(e1.getLocalizedMessage());
}
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 读大文本
*
*/
private void readClob() {
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test where id=2");
String tempStr = null;
if (rs.next()) {
Clob clob = rs.getClob("CLOBCONTENT");
if (clob != null) {
Reader in = clob.getCharacterStream();
BufferedReader br = new BufferedReader(in);
System.out.println("开始读....");
while ((tempStr = br.readLine()) != null) {
System.out.println(tempStr);
}
System.out.println("读完成....");
in.close();
}
rs.close();
stmt.close();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 写大文本
*
*/
private void writeClob() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
String sql = null;
Statement stmt = conn.createStatement();
sql = "delete from test where id=2";
stmt.executeUpdate(sql);
sql = "insert into test values(2,empty_blob(),empty_clob())";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery("select * from test where id=2");
if (rs.next()) {
Clob clob = rs.getClob("CLOBCONTENT");
PrintWriter out = new PrintWriter(new BufferedWriter(
((oracle.sql.CLOB) clob).setCharacterStream(0)));
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(
"d:/在北大校园BBS引起轰动的一篇文章请热爱祖国的人转发!!!!.mht")));
String str = null;
System.out.println("开始写...");
while ((str = in.readLine()) != null) {
out.println(str);
System.out.println(str);
}
in.close();
out.close();
rs.close();
conn.commit();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
System.out.println(e1.getLocalizedMessage());
}
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
private Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName(driver);
return DriverManager.getConnection(url, user, pwd);
}
/**
*
* @param rs
* @throws SQLException
*/
private void displayResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 0; i < colnum; i++) {
if (i == colnum - 1)
System.out.print(rsmd.getColumnLabel(i + 1) + ": "
+ rs.getObject(i + 1));
else
System.out.print(rsmd.getColumnLabel(i + 1) + ": "
+ rs.getObject(i + 1) + " , ");
}
System.out.println();
}
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* @author semovy 测试向oracle 读,写文件Blob 读,写大文本Clob
*/
public class OracleBlobTest {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:teckotooling";
private String user = "scott";
private String pwd = "tiger";
public OracleBlobTest() {
}
public static void main(String[] args) {
OracleBlobTest obt = new OracleBlobTest();
obt.writeBlob();
obt.readBlob();
obt.writeClob();
obt.readClob();
}
/**
* 读二进制文件
*
*/
private void readBlob() {
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test where id=1");
byte[] buffer = new byte[1024];
OutputStream out = new FileOutputStream("d:/360安全卫士定1.exe");
int tempLen = 0;
int amount = 0;
if (rs.next()) {
Blob blob = rs.getBlob("BINARYCONTENT");
InputStream in = blob.getBinaryStream();
while ((tempLen = in.read(buffer)) != -1) {
out.write(buffer, 0, tempLen);
amount += tempLen;
System.out.println("已经读出并写:" + amount + " 字节");
}
System.out.println("已经读出并写:完成");
out.flush();
out.close();
in.close();
rs.close();
stmt.close();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 写二进制文件
*
*/
private void writeBlob() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
String sql = null;
Statement stmt = conn.createStatement();
sql = "delete from test where id=1";
stmt.executeUpdate(sql);
sql = "insert into test(1,BINARYCONTENT,CLOBCONTENT) values(1,empty_blob(),empty_clob())";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery("select * from test where id=1");
if (rs.next()) {
Blob blob = rs.getBlob("BINARYCONTENT");
OutputStream out = ((oracle.sql.BLOB) blob).setBinaryStream(0);// 从0开始,否则写出的文件有差错
int bufferSize = ((oracle.sql.BLOB) blob).getBufferSize();
System.out.println("bufferSize :" + bufferSize);
BufferedInputStream in = new BufferedInputStream(
new FileInputStream("d:/360安全卫士定.exe"), bufferSize);
byte[] b = new byte[bufferSize];
int count = in.read(b, 0, bufferSize);
int amount = 0;
while (count != -1) {
out.write(b, 0, count);
amount += count;
System.out.println("处理了 " + amount + " 字节");
count = in.read(b, 0, bufferSize);
System.out.println("处理了 " + amount + " 字节,成功");
}
out.close();
out = null;
in.close();
conn.commit();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
System.out.println(e1.getLocalizedMessage());
}
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 读大文本
*
*/
private void readClob() {
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from test where id=2");
String tempStr = null;
if (rs.next()) {
Clob clob = rs.getClob("CLOBCONTENT");
if (clob != null) {
Reader in = clob.getCharacterStream();
BufferedReader br = new BufferedReader(in);
System.out.println("开始读....");
while ((tempStr = br.readLine()) != null) {
System.out.println(tempStr);
}
System.out.println("读完成....");
in.close();
}
rs.close();
stmt.close();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
/**
* 写大文本
*
*/
private void writeClob() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
String sql = null;
Statement stmt = conn.createStatement();
sql = "delete from test where id=2";
stmt.executeUpdate(sql);
sql = "insert into test values(2,empty_blob(),empty_clob())";
stmt.executeUpdate(sql);
ResultSet rs = stmt.executeQuery("select * from test where id=2");
if (rs.next()) {
Clob clob = rs.getClob("CLOBCONTENT");
PrintWriter out = new PrintWriter(new BufferedWriter(
((oracle.sql.CLOB) clob).setCharacterStream(0)));
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(
"d:/在北大校园BBS引起轰动的一篇文章请热爱祖国的人转发!!!!.mht")));
String str = null;
System.out.println("开始写...");
while ((str = in.readLine()) != null) {
out.println(str);
System.out.println(str);
}
in.close();
out.close();
rs.close();
conn.commit();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getLocalizedMessage());
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
System.out.println(e1.getLocalizedMessage());
}
System.out.println(e.getLocalizedMessage());
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
System.out.println(e.getLocalizedMessage());
}
}
}
private Connection getConnection() throws ClassNotFoundException,
SQLException {
Class.forName(driver);
return DriverManager.getConnection(url, user, pwd);
}
/**
*
* @param rs
* @throws SQLException
*/
private void displayResultSet(ResultSet rs) throws SQLException {
ResultSetMetaData rsmd = rs.getMetaData();
int colnum = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 0; i < colnum; i++) {
if (i == colnum - 1)
System.out.print(rsmd.getColumnLabel(i + 1) + ": "
+ rs.getObject(i + 1));
else
System.out.print(rsmd.getColumnLabel(i + 1) + ": "
+ rs.getObject(i + 1) + " , ");
}
System.out.println();
}
}
}
发表评论
-
oracle 列内容部分替换
2011-03-04 10:31 1384需求: mytable某列S中含有‘192.168.2 ... -
Oracle系统管理员密码忘记解决办法
2010-08-19 16:41 22861.在命令行下输入 sqlplus /nolog 进入SQL* ... -
查看表空间的名称及大小
2010-07-28 00:03 1409SQL> select tablespace_name, ... -
导入导出一个或多个数据库表命令
2010-06-28 12:56 1167导出表:exp system/manager@TEST fil ... -
oracle根据视图获得表命令
2010-05-24 12:23 961方法: create table tableName as ... -
Oracle 异常:ORA-01658:unable to create INITIAL extent for segmnet in tablespace
2010-05-20 11:41 9420问题描述: ORA-01658:unable to crea ... -
ORA-20001: Invalid or inconsistent input values解决办法
2010-05-20 11:33 3111查了些资料,加了statistics=none imp us ... -
ORA-01658: 无法为表空间space中的段创建 INITIAL 区解决办法
2010-05-19 22:07 45832天在oracle里创建表,报出错:ORA-01658: 无法为 ... -
Io 异常: The Network Adapter could not establish the connection
2010-04-29 18:27 2448异常如下: org.apache.commons.dbcp. ... -
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
2010-04-29 18:03 1076111111 -
ORA-12505
2010-04-19 11:47 2535Listener refused the connection ... -
Oracle数据库中sequence的用法
2010-01-26 18:56 1259在Oracle数据库中,sequence等同于序列号,每次取的 ... -
SQL 注释
2010-01-19 17:11 1589T-SQL 单行注释符号:--注释内容 T-SQL 多行注释 ... -
java备份oracle数据库(此方法错误)
2010-01-19 14:31 1357注意:此方法不能成功导出oracle public class ... -
修改Oracle最大连接数
2010-01-16 06:01 1074修改Oracle最大连接数 1、修改Oracle最大连接数的 ... -
oracle中实现主键的自动增加
2010-01-15 10:53 1545摘要:oracle中实现主键的自动增加 实现方法1: ... -
oracle 日期常用函數 (SYSDATE、日期格式)
2010-01-15 10:42 1496SYSDATE --◎ 可得到目前系統的時間 ... -
[oracle] to_date() 与 to_char() 日期和字符串转换
2010-01-15 10:28 1327to_date("要转换的字符串",&qu ... -
ORACLE 提示“ERROR - ORA-12541: TNS: 无监听程序”解决方案
2010-01-08 15:12 2352查看\oracle\product\10.2.0\db_1\n ... -
对《JSP JDBC(Thin模式)连接Oracle》帖子的质疑
2010-01-06 23:21 1065闲来无事,读到一个jsp jdbc连接oracle的文章《JS ...
相关推荐
在描述中提到的“Oracle导出Clob,Blob等大字段工具”就是为了解决这个问题而设计的,它是一个自编写的实用程序,方便用户导出和管理Oracle数据库中的大对象数据。 Oracle数据库中的Clob类型用于存储大量的文本数据...
在Oracle数据库环境中,数据类型BLOB(Binary Large Object)用于存储大量的二进制数据,如图像、音频或视频文件,而CLOB(Character Large Object)则用于存储大量文本数据。在某些情况下,可能需要将BLOB类型的...
CLOB数据类型用于存储大量的文本数据,如XML文档或长篇文章,而BLOB则用于存储二进制大对象,如图片、音频或视频文件。在Oracle中,对这类数据的操作通常需要特定的策略和工具,因为它们的大小和性质不同于常规的...
### Oracle Blob转换Clob #### 知识点一:Oracle Blob与Clob的基本概念 - **Blob (Binary Large Object)**:在Oracle数据库中,Blob类型用于存储大量的二进制数据,如图像、视频或文档等非结构化数据。 - **Clob ...
- **数据长度**:两者都支持最大4GB的数据存储,但`CLOB`通常用于存储文本,而`BLOB`用于存储图像或其他二进制文件。 - **编码**:`CLOB`可以使用不同的字符集编码(如UTF-8),而`BLOB`则是纯粹的二进制数据,没有...
`CLOB`用于存储非结构化的字符数据,如长篇文章、XML文档等,而`BLOB`则用于存储二进制大对象,如图片、音频或视频文件。在处理这些大型数据时,数据库系统需要考虑存储效率、性能和查询能力。 1. MySQL对`CLOB`和`...
在数据库管理中,存储非结构化数据如图片、音频或视频文件时,通常会使用`CLOB`(Character Large Object)和`BLOB`(Binary Large Object)这两种数据类型。Oracle数据库系统支持这两种数据类型,用于存储大量文本...
Oracle数据库同样支持BLOB和CLOB数据类型,但需要特殊配置来处理来自其他数据库的大对象数据。 3. **特殊配置策略**: - **联邦能力**:利用WII的联邦功能,先将DB2中的非BLOB字段复制到Oracle目标表中。 - **...
在Oracle8i中,BLOB类型提供了处理大文件的能力,可以存储高达4GB的数据。BLOB数据类型有以下特性: 1. **存储容量**:最大可存储4GB。 2. **存储方式**:以字节流的形式存储,支持二进制数据。 3. **访问方式**:...
这两种类型用于存储大量的文本数据(CLOB)和二进制数据(BLOB),例如图片、文档或音频文件。在JDBC(Java Database Connectivity)中,我们可以使用特定的方法来操作这些类型的数据。 首先,我们需要建立与数据库...
Oracle数据库中的BLOB(Binary Large Object)字段是用来存储大量二进制数据的,例如图片、文档或音频文件。在Delphi编程环境中,处理这类数据时需要掌握特定的API调用和方法。本篇文章将深入探讨如何在Delphi中对...
对于CLOB和BLOB字段,Oracle JDBC驱动提供了`oracle.jdbc.driver.OracleClob`和`oracle.jdbc.driver.OracleBlob`类,它们实现了Java的标准接口`java.sql.Clob`和`java.sql.Blob`。这两个接口提供了读取和写入大数据...
它们都是Oracle数据库中重要的字段类型,用于处理大数据量的文本或二进制文件。了解BLOB和CLOB的区别以及如何在Oracle中进行插入和查询操作对于数据库开发者和管理员来说至关重要。 首先,我们来看BLOB和CLOB的主要...
CLOB(Character Large OBject)和 BLOB(Binary Large OBject)都是大字段类型,用于存储大量的数据。 CLOB 类型用于存储文本数据,可以存储长文本、文章、备注等信息。在 Oracle 数据库中,CLOB 类型对应的数据库...
在Oracle数据库中,CLOB(Character Large Object)和BLOB(Binary Large Object)是用来存储大量文本数据和二进制数据的特殊数据类型。CLOB用于存储非结构化的文本信息,如XML文档或长篇文章,而BLOB则适用于图像、...
CLOB主要用来存储大文本数据,如长篇文章、XML文档或JSON字符串,而BLOB则用于存储大量的二进制数据,如图像、音频、视频文件或者任何非文本的大型数据。在DELPHI中,通常会使用ADO(ActiveX Data Objects)或DBX...
同时,为了提高效率,可以考虑将数据分块读写,特别是处理大文件时。此外,Clob类型通常用于存储文本数据,例如文章内容,与图片存储无关,但其处理方式与Blob类似,只是涉及到字符流而不是二进制流。在本例中,我们...
这两种类型都是用于存储大量数据,BLOB用于二进制数据,如图片、音频或视频文件,而CLOB则用于存储大量的文本数据,如XML文档或长篇文本。 在Oracle中,Blob和Clob的处理涉及到许多关键知识点,包括创建、读取、...
Oracle数据库系统支持对大型数据对象(LOBs,Large Objects)的操作,这包括BLOB、CLOB、NCLOB和BFILE四种类型。每种类型都有其特定的用途和特性,适用于存储不同类型的大数据。 1. BLOB(Binary Large Object): ...