急急急急:如何读取Oracle数据表中的clob字段(该字段存有图片)

dinghaohong81 2011-08-25 07:55:23
假设表名为 book
字段为:
id VARCHAR2(16) not null,//唯一
book_name VARCHAR2(100) not null,
book_cop clob //是存的图片
我现在想查找id大于'100002'而小于‘100100’的书的图片。
显示形式为
编号 书名 封皮
100002 ##

/////////////////
我使用的是vs2005
asp.net
c#
////在线等,我qq66697186////谢谢啊
补充一下:
假如web.config配置:
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=book_manager;User ID=dinghh;Password=810826;Unicode=True providerName="System.Data.OracleClient" />
</connectionStrings>

在程序中:
public OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["sjzxtbzl"].ToString());
...全文
475 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dinghaohong81 2011-11-22
  • 打赏
  • 举报
回复
继续寻找???
GIS__ 2011-08-30
  • 打赏
  • 举报
回复
一、BLOB操作  
1、入库
(1)JDBC方式
//通过JDBC获得数据库连接
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是传入的byte数组,定义:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
(2)JNDI方式
//通过JNDI获得数据库连接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Blob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server对应的可能会不同)
weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是传入的byte数组,定义:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
2、出库
//获得数据库连接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");
if (rs.next())
{
java.sql.Blob blob = rs.getBlob("BLOBATTR");
InputStream inStream = blob.getBinaryStream();
//data是读出并需要返回的数据,类型是byte[]
data = new byte[input.available()];
inStream.read(data);
inStream.close();
<pre class="java" name="code">conn = this.getConnection();
conn.setAutoCommit(false);
java.sql.Statement st = conn.createStatement();
rs= st.executeQuery(sql);
BLOB inblob = null;
if (rs.next()) {
inblob = (BLOB) rs.getBlob("BLOBATTR");
}
data=inblob.getBytes(1,(int)inblob.length());//这个就是数据
}
inStream.close();
con.commit();
con.close();

二、CLOB操作
1、入库
(1)JDBC方式
//通过JDBC获得数据库连接
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Clob对象后强制转换为oracle.sql.CLOB
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");
Writer outStream = clob.getCharacterOutputStream();
//data是传入的字符串,定义:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
(2)JNDI方式
//通过JNDI获得数据库连接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Clob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinClob(不同的App Server对应的可能会不同)
weblogic.jdbc.vendor.oracle.OracleThinClob clob = (weblogic.jdbc.vendor.oracle.OracleThinClob) rs.getClob("CLOBATTR");
Writer outStream = clob.getCharacterOutputStream();
//data是传入的字符串,定义:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
2、出库
//获得数据库连接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
if (rs.next())
{
java.sql.Clob clob = rs.getClob("CLOBATTR");
Reader inStream = clob.getCharacterStream();
char[] c = new char[(int) clob.length()];
inStream.read(c);
//data是读出并需要返回的数据,类型是String
data = new String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();

需要注意的地方:
1、java.sql.Blob、oracle.sql.BLOB、weblogic.jdbc.vendor.oracle.OracleThinBlob几种类型的区别
2、java.sql.Clob、oracle.sql.CLOB、weblogic.jdbc.vendor.oracle.OracleThinClob几种类型的区别
公司项目中的用法(博客):
入库:先插一个oracle.sql.CLOB.empty_lob()进去,然后
String updateBaseSourceSql = "select content from mb_baseSource where id = ? for update";
conn.setAutoCommit(false);
ps = conn.prepareStatement(updateBaseSourceSql);
ps.setLong(1, result);
ResultSet rs = ps.executeQuery();
oracle.sql.CLOB clob = null;
if (rs.next()) {
clob = (oracle.sql.CLOB) rs.getClob(1);
}
Writer wr = clob.getCharacterOutputStream();
wr.write(baseSource[4]);
wr.flush();
wr.close();
rs.close();
ps.close();
conn.commit();
出库:
findBaseSourceSql = "select content from mb_baseSource where id = ?";
ps = conn.prepareStatement(findBaseSourceSql);
ps.setLong(1, sourceID);
rs = ps.executeQuery();
if (rs.next()) {
CLOB clob = (oracle.sql.CLOB) rs.getClob(1);
if (clob != null) {
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
while (s != null) {
result[6] += s;
s = br.readLine();
}
}
}
rs.close();
ps.close();
conn.close();
90后小朋友 2011-08-30
  • 打赏
  • 举报
回复
好帖顶
dinghaohong81 2011-08-25
  • 打赏
  • 举报
回复
?????都没有遇见过???
dinghaohong81 2011-08-25
  • 打赏
  • 举报
回复
???都没有上班呢
lanjian1111 2011-08-25
  • 打赏
  • 举报
回复
public InputStream getBlob(int tableid, long shoeid, long playid) throws IOException {
InputStream in = null;
try {

this.strSQL = "select jpgimage from image where id=?";
pstmt = conn.prepareStatement(this.strSQL);
pstmt.setInt(1, tableid);

rs = pstmt.executeQuery();
if (rs.next()) {
in = rs.getBinaryStream("jpgimage");
}
if (pstmt != null) {
pstmt.close();
pstmt = null;
}
} catch (SQLException e) {
return null;
}
return in;
}

然后在页面显示
InputStream in = null;
in = conn.getBlob(100002);

if (in != null) {
response.reset();
response.setContentType("image/jpeg");
OutputStream os = response.getOutputStream();
response.setHeader("Cache-Control", "public,max-age=600");
response.setDateHeader("Expires", (new Date()).getTime() + 1000 * 60 * 10);
byte[] b = new byte[1024];
int len;
if (in != null) {
while ((len = in.read(b)) != -1) {
os.write(b, 0, len);
}
in.close();
in = null;
}
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}//直接显示在一个页面中
lanjian1111 2011-08-25
  • 打赏
  • 举报
回复
以前用JAVA做过,C#没有做过
lanjian1111 2011-08-25
  • 打赏
  • 举报
回复
首先要读出图片到内存里面,然后再显示

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧