数据库

位置:IT落伍者 >> 数据库 >> 浏览文章

JDBC存取ORACLE大型数据对象LOB几种情况的示范类


发布日期:2018年05月12日
 
JDBC存取ORACLE大型数据对象LOB几种情况的示范类

import javaio*;

import javautil*;

import javasql*;

public class LobPros

{

/**

* ORACLE驱动程序

*/

private static final String DRIVER = oraclejdbcdriverOracleDriver;

/**

* ORACLE连接用URL

*/

private static final String URL = jdbc:oracle:thin:@test::orac;

/**

* 用户名

*/

private static final String USER = user;

/**

* 密码

*/

private static final String PASSWORD = pswd;

/**

* 数据库连接

*/

private static Connection conn = null;

/**

* SQL语句对象

*/

private static Statement stmt = null;

/**

* @roseuid EDAEBC

*/

public LobPros()

{

}

/**

* 往数据库中插入一个新的CLOB对象

*

* @param infile 数据文件

* @throws javalangException

* @roseuid EDAABC

*/

public static void clobInsert(String infile) throws Exception

{

/* 设定不自动提交 */

boolean defaultCommit = conngetAutoCommit();

connsetAutoCommit(false);

try {

/* 插入一个空的CLOB对象 */

stmtexecuteUpdate(INSERT INTO TEST_CLOB VALUES ( EMPTY_CLOB()));

/* 查询此CLOB对象并锁定 */

ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);

while (rsnext()) {

/* 取出此CLOB对象 */

oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);

/* 向CLOB对象中写入数据 */

BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());

BufferedReader in = new BufferedReader(new FileReader(infile));

int c;

while ((c=inread())!=) {

outwrite(c);

}

inclose();

outclose();

}

/* 正式提交 */

mit();

} catch (Exception ex) {

/* 出错回滚 */

connrollback();

throw ex;

}

/* 恢复原提交状态 */

connsetAutoCommit(defaultCommit);

}

/**

* 修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)

*

* @param infile 数据文件

* @throws javalangException

* @roseuid EDAB

*/

public static void clobModify(String infile) throws Exception

{

/* 设定不自动提交 */

boolean defaultCommit = conngetAutoCommit();

connsetAutoCommit(false);

try {

/* 查询CLOB对象并锁定 */

ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);

while (rsnext()) {

/* 获取此CLOB对象 */

oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);

/* 进行覆盖式修改 */

BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());

BufferedReader in = new BufferedReader(new FileReader(infile));

int c;

while ((c=inread())!=) {

outwrite(c);

}

inclose();

outclose();

}

/* 正式提交 */

mit();

} catch (Exception ex) {

/* 出错回滚 */

connrollback();

throw ex;

}

/* 恢复原提交状态 */

connsetAutoCommit(defaultCommit);

}

/**

* 替换CLOB对象(将原CLOB对象清除换成一个全新的CLOB对象)

*

* @param infile 数据文件

* @throws javalangException

* @roseuid EDABFE

*/

public static void clobReplace(String infile) throws Exception

{

/* 设定不自动提交 */

boolean defaultCommit = conngetAutoCommit();

connsetAutoCommit(false);

try {

/* 清空原CLOB对象 */

stmtexecuteUpdate(UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID=);

/* 查询CLOB对象并锁定 */

ResultSet rs = stmtexecuteQuery(SELECT CLOBCOL FROM TEST_CLOB WHERE ID= FOR UPDATE);

while (rsnext()) {

/* 获取此CLOB对象 */

oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);

/* 更新数据 */

BufferedWriter out = new BufferedWriter(clobgetCharacterOutputStream());

BufferedReader in = new BufferedReader(new FileReader(infile));

int c;

while ((c=inread())!=) {

outwrite(c);

}

inclose();

outclose();

}

/* 正式提交 */

mit();

} catch (Exception ex) {

/* 出错回滚 */

connrollback();

throw ex;

}

/* 恢复原提交状态 */

connsetAutoCommit(defaultCommit);

}

/**

* CLOB对象读取

*

* @param outfile 输出文件名

* @throws javalangException

* @roseuid EDAD

*/

public static void clobRead(String outfile) throws Exception

{

/* 设定不自动提交 */

boolean defaultCommit = conngetAutoCommit();

connsetAutoCommit(false);

try {

/* 查询CLOB对象 */

ResultSet rs = stmtexecuteQuery(SELECT * FROM TEST_CLOB WHERE ID=);

while (rsnext()) {

/* 获取CLOB对象 */

oraclesqlCLOB clob = (oraclesqlCLOB)rsgetClob(CLOBCOL);

/* 以字符形式输出 */

BufferedReader in = new BufferedReader(clobgetCharacterStream());

BufferedWriter out = new BufferedWriter(new FileWriter(outfile));

int c;

while ((c=inread())!=) {

outwrite(c);

}

outclose();

inclose();

}

} catch (Exception ex) {

connrollback();

throw ex;

}

/* 恢复原提交状态 */

connsetAutoCommit(defaultCommit);

}

/**

* 向数据库中插入一个新的BLOB对象

*

* @param infile 数据文件

* @throws javalangException

* @roseuid EDAEF

*/

public static void blobInsert(String infile) throws Exception

{

/* 设定不自动提交 */

boolean defaultCommit = conngetAutoCommit();

connsetAutoCommit(fal

上一篇:Oracle常见错误代码的分析与解决一

下一篇:Oracle Net8 网络配置和联接