大对象存取

<script language="javascript" src="http://tag.csdn.net/urltag.aspx" type="text/javascript"></script>
<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
大对象存取

 
大对象存取
//BLOB和CLOB方法一样
package text;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.sql.ResultSet;
import oracle.sql.BLOB;
public class BLOBtest {
 public static void createTab() {               //建表
    String sql = "create table myimg (id number,img blob)";
    Connection con = DBConn.getConnection();
    Statement stmt = null;
    try {
      stmt = con.createStatement();
      stmt.execute(sql);
      System.out.println("table created------");
    }
    catch (SQLException ex) {ex.printStackTrace();}
    finally {
      try {
        stmt.close();
        con.close();
      }
      catch (SQLException ex1) {
        ex1.printStackTrace();
      }    } }
 public static void insertData() {                                //插入大对象
    Connection con =DBConn.getConnection();
    PreparedStatement pstmt = null;
    String sql = "insert into myimg values (?,?)";
    try {
      pstmt = con.prepareStatement(sql);
      pstmt.setInt(1, 1000);
      File file = new File("f:/jdbcdemo/2.jpg");
      long len = file.length();
      FileInputStream fis = new FileInputStream(file);
      pstmt.setBinaryStream(2, fis, (int) len);
      pstmt.execute();
      System.out.println("insert pic success--------");
      fis.close();
    } catch (SQLException ex) {
      ex.printStackTrace();
    }catch (FileNotFoundException ex1) {
      ex1.printStackTrace();
    }catch (IOException ex2) {ex2.printStackTrace();}
    finally {
      try {
        pstmt.close();
        con.close();
      }
      catch (SQLException ex3) {
        ex3.printStackTrace();
      }    } }
 public static void getPic() {                           //下载大对象
    Connection con = DBConn.getConnection();
    Statement stmt = null;
    ResultSet rst = null;
    String sql = "select img from myimg where id = 1000";
   try {
      stmt = con.createStatement();
      rst = stmt.executeQuery(sql);
      while (rst.next()) {
        File f = new File("f:/jdbcdemo/123.jpg");
        FileOutputStream fos = new FileOutputStream(f);
        BLOB blob = (BLOB) rst.getBlob(1);
        InputStream is = blob.getBinaryStream();
        DataInputStream dis = new DataInputStream(is);
        int len;
        len = dis.read();
        while (len != -1) {
          fos.write(len);
          len = dis.read();
        }
       fos.close();
       dis.close();
       System.out.println("read finished-------------");
      }    }
    catch (SQLException ex) { ex.printStackTrace();}
    catch (FileNotFoundException ex1) {ex1.printStackTrace(); }
catch (IOException ex2) {ex2.printStackTrace();
}finally {
      try {
        stmt.close();
        con.close();
      }catch (SQLException ex3) {ex3.printStackTrace();}
    } }
 public static void main(String[] args) {
           createTab();
           insertData();
           getPic();
 }
}
 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1205624


<script src="http://localhost:82/PromoteIcon.aspx?id=1205624" type="text/javascript"></script>[ 收藏到我的网摘]   weasel发表于 2006年09月11日 01:30:00
href="http://blog.csdn.net/aptweasel/Services/Pingback.aspx" rel="pingback" /> <script type="text/javascript">function hide(){showComment();}</script>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RedisTemplate 是 Spring Data Redis 提供的一个用于操作 Redis 数据库的模板类。它封装了 Redis 的常用操作,使得在 Java 代码中可以方便地进行 Redis 数据的存取。 使用 RedisTemplate 存取对象的步骤如下: 1. 配置 RedisTemplate:在 Spring 配置文件中配置 Redis 连接信息,包括 Redis 服务器地址、端口号、密码等。 2. 注入 RedisTemplate:在需要使用 RedisTemplate 的类中,使用 `@Autowired` 注解注入 RedisTemplate 对象。 3. 存储对象:通过 RedisTemplate 的 `opsForValue()` 方法可以获取 ValueOperations 对象,然后使用 `set(key, value)` 方法将对象存储到 Redis 中。其中,key 是存储对象的键,value 是要存储的对象。 ```java @Autowired private RedisTemplate<String, Object> redisTemplate; public void saveObject(String key, Object value) { redisTemplate.opsForValue().set(key, value); } ``` 4. 获取对象:通过 RedisTemplate 的 `opsForValue()` 方法可以获取 ValueOperations 对象,然后使用 `get(key)` 方法从 Redis 中获取存储的对象。 ```java public Object getObject(String key) { return redisTemplate.opsForValue().get(key); } ``` 需要注意的是,存储到 Redis 中的对象需要实现 Serializable 接口,以便进行序列化和反序列化操作。 以上就是使用 RedisTemplate 存取对象的基本步骤。通过 RedisTemplate,我们可以方便地将 Java 对象存储到 Redis 中,并且可以按照需要进行读取和更新操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值