CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2EE / EJB / JMS

java+oracle问题

楼主samkuang(执着前行)2005-04-19 21:03:59 在 Java / J2EE / EJB / JMS 提问

在java里如何访问放在oracle里的图片文件或者是大对象,,请高手帮帮忙 问题点数:40、回复次数:8Top

1 楼davidmay(我最喜欢你们这些灌水的了,真有技术含量)回复于 2005-04-20 13:09:25 得分 10

以二进制访问。可用byte数组来接收数据。Top

2 楼itjourney(IT之旅)回复于 2005-04-20 13:20:28 得分 10

java.sql.ResultSet  
              getBlob()  
              getBytes()  
              getClob()  
  Top

3 楼samkuang(执着前行)回复于 2005-04-20 19:12:55 得分 0

itjourney(IT之旅)   可以详细点吗,有例子最好,无限感激!!Top

4 楼lasthope(学生)回复于 2005-04-20 21:12:11 得分 0

关注Top

5 楼samkuang(执着前行)回复于 2005-04-21 21:33:58 得分 0

upupTop

6 楼ike_Adriano(亚热带空气)回复于 2005-04-21 22:03:13 得分 0

帮你UP一下Top

7 楼samkuang(执着前行)回复于 2005-04-23 23:46:26 得分 0

upupupTop

8 楼Squall1009(钰枫)(找工作ing)回复于 2005-04-24 00:31:43 得分 20

import   java.io.*;  
  import   java.sql.*;  
   
   
  public   class   BlobOperation  
  {  
  public   static   void   addLob(long   id,   String   binFile)   throws   SQLException  
  {  
  Connection   con   =   null;  
  PreparedStatement   ps   =   null;  
  ResultSet   rs   =   null;  
   
  try  
  {  
  con   =   ConnectionFactory.getConnection();   //换成你自己取连接的方法  
  con.setAutoCommit(false);  
   
  String   sql   =   "INSERT   INTO   Blob_Tbl(id,   binfile,   bincontent)";  
  sql   +=   "   VALUES(?,   ?,   ?)";  
  ps   =   con.prepareStatement(sql);  
   
  ps.setLong(1,   id);  
  ps.setString(2,   binFile);  
  ps.setBlob(3,   oracle.sql.BLOB.empty_lob());  
   
  ps.executeUpdate();  
  //DatabaseUtils.closeObject(ps);  
   
  ps   =   con.prepareStatement("SELECT   bincontent   FROM   Blob_Tbl   WHERE   id   =   "   +   id   +   "   for   update   ");  
  rs   =   ps.executeQuery();  
   
  if   (rs.next())  
  {  
  oracle.sql.BLOB   binContent   =   (oracle.sql.BLOB)   rs.getBlob(1);  
   
  /*   write   blob   content   */  
  OutputStream   binOut   =   binContent.getBinaryOutputStream();  
  BufferedOutputStream   out   =   new   BufferedOutputStream(binOut);  
  BufferedInputStream   in   =   new   BufferedInputStream(new   FileInputStream(binFile));  
  int   c;  
  while   ((c   =   in.read())   !=   -1)  
  {  
  out.write(c);  
  }  
  in.close();  
  out.close();  
  }  
  con.commit();  
  }   catch   (Exception   e)  
  {  
  e.printStackTrace();  
  try  
  {  
  con.rollback();  
  }   catch   (SQLException   se)  
  {  
  }  
  throw   new   SQLException(e.getMessage());  
  }   finally  
  {  
  DatabaseUtils.closeObject(rs,   ps,   con);  
  }  
  }  
   
  public   static   void   fetchLob(long   id,   String   filename)   throws   SQLException  
  {  
  Connection   con   =   null;  
  Statement   st   =   null;  
  ResultSet   rs   =   null;  
   
  try  
  {  
  con   =   ConnectionFactory.getConnection();  
   
  String   sql   =   "SELECT   *     From   Blob_Tbl   Where   id   =   "   +   id;  
  st   =   con.createStatement();  
   
  rs   =   st.executeQuery(sql);  
  while   (rs.next())  
  {  
   
  String   binFile   =   rs.getString("binfile");  
  oracle.sql.BLOB   binContent   =   (oracle.sql.BLOB)   rs.getBlob("bincontent");  
   
  /*   read   blob   content   */  
  BufferedOutputStream   out   =   new   BufferedOutputStream(new   FileOutputStream(filename));  
  BufferedInputStream   in   =   new   BufferedInputStream(binContent.getBinaryStream());  
   
  int   c;  
  while   ((c   =   in.read())   !=   -1)  
  {  
  out.write(c);  
  }  
  in.close();  
  out.close();  
  }  
   
  }   catch   (Exception   e)  
  {  
  throw   new   SQLException(e.getMessage());  
  }   finally  
  {  
  DatabaseUtils.closeObject(rs,   st,   con);  
  }  
  }  
   
  public   static   void   main(String[]   args)   throws   Exception  
  {  
  if   (args.length   ==   0)  
  {  
  addLob(1,   "a.jpg");  
  }   else  
  {  
  fetchLob(1,   args[0]);  
  }  
  }  
  }  
   
  自己把有的东西改下吧Top

相关问题

  • java与oracle
  • java操作ORACLE
  • 请教 java和oracle
  • oracle和Java的连接,急!!
  • java和oracle连接问题
  • ****************ORACLE与JAVA组合难题****************************
  • 高手求救:java weblogic oracle
  • java连接oracle的问题?
  • java连接oracle问题?
  • oracle

关键词

  • ps
  • oracle
  • sql
  • null
  • bincontent
  • binfile
  • blob
  • con
  • rs

得分解答快速导航

  • 帖主:samkuang
  • davidmay
  • itjourney
  • Squall1009

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo