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

Java对Oracle里Blob字段的处理

2006-04-24 15:30 369 查看
比较简单,直接贴代码吧,有问题留言

/**
* Usage:Oracle Blob Field
*
* FileName : Blob.java
* PackageName: org.ahpo.db
* Author : Ahpo Yang
* CreateDate : 2006-4-24 14:54:04
*/
package org.ahpo.db;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JBlob {

String tableName = null; // 表名

String primaryKey = null; // 表的主键名

String primaryValue = null; // 表的主键值

String fieldName = null; // 表的CLOB字段名

String clobValue = null; // 表的CLOB字段值

Connection conn = null; // 与oracle的连接

/**
*
* 用于测试用
*
*/
public static void main(String[] args) {
try {
JBlob jb = new JBlob(getConnection(), "aa", "a", "aaaa", "c", "ccc");
jb.writeBlob();
jb.readBlob();
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}

/**
*
* 构造方法
*
*/
public JBlob(Connection connection, String tableName, String primaryKey,
String primaryValue, String fieldName, String clobValue) {
this.conn = connection;
this.tableName = tableName;
this.primaryKey = primaryKey;
this.primaryValue = primaryValue;
this.fieldName = fieldName;
this.clobValue = clobValue;
}

/**
*
* 构造方法,但不必传clobValue值 一般构造出的实例用来读Clob字段
*
*/
public JBlob(Connection connection, String tableName, String primaryKey,
String primaryValue, String fieldName) {
this.conn = connection;
this.tableName = tableName;
this.primaryKey = primaryKey;
this.primaryValue = primaryValue;
this.fieldName = fieldName;
}

/**
* 读取Blob字段
*
* @return
* @throws SQLException
* @throws IOException
*/
public String readBlob() throws SQLException, IOException {
String strRet = "";
Statement stmt = null;
ResultSet rs = null;
try {
String strSql = "select " + fieldName + " from " + tableName
+ " where " + primaryKey + "=" + primaryValue;
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(strSql);
byte b[] = null; // 保存从BLOB读出的字节

if (rs.next()) {
// System.out.println("ahpo = == 数据存在");
java.sql.Blob blob = (java.sql.Blob) rs.getBlob(1);
int filesize = (int) blob.length();
b = blob.getBytes(1, filesize); // 从BLOB取出字节流数据
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream q = new ObjectInputStream(bi);
try {
strRet = (String) q.readObject();

} catch (ClassNotFoundException ex) {
} catch (IOException ex) {
}

bi.close();
q.close();
}

} catch (SQLException e) {
throw e;
} catch (Exception e) {
// System.out.println("ahpo===异常出现");
// 异常处理
try {
conn.rollback();
} catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();

} finally {
// 释放连接
if (rs != null) {
try {
rs.close();
} catch (Exception ex) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception ex) {
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception ex) {
}
}
}

return strRet;

}

public void writeBlob() throws SQLException, IOException {
// open a file, put the content of file into a bytes array
try {
File files = new File("d://rssxml//sample.xml");
FileInputStream fis = new FileInputStream(files);

byte[] fl = new byte[(int) files.length()];
fis.read(fl);

// use this bytes array to construct a InputStream
ByteArrayInputStream kk = new ByteArrayInputStream(fl);

// connect the oracle database
if (conn == null) {
conn = getConnection();
}

PreparedStatement ps = conn.prepareStatement("INSERT INTO "
+ tableName + "(" + fieldName + ") values (?) where "
+ primaryKey + "=" + primaryValue);

// set parameters

ps.setBinaryStream(1, kk, (int) files.length());

// execute

ps.executeUpdate();
fis.close();
ps.close();
} catch (IOException e) {
throw e;

} catch (SQLException e) {
throw e;

} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 取得数据库连接
*
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection getConnection() throws SQLException,
ClassNotFoundException {
Class.forName("oracle.jdbc.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.1.18:1521:portal", "portal",
"portal");
return conn;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: