`
pavel
  • 浏览: 927714 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

大字段的插入

    博客分类:
  • jdbc
阅读更多

//字符读取用read 字符读取用stream 

 

//读取文件插入mysql(Text字段)
  try {
   File file = new File("d:\test.txt");
   Reader reader = new BufferedReader(new FileReader(file));
   PreparedStatement pstmt = null;
   pstmt.setCharacterStream(1, reader,(int)file.length());
   reader.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 

/**
   * 读取数据库 放入文件Clob是储存文本,Blob储存二进制
   * 基本思路:放入缓冲区char中,然后写入文件
   * */
  try {
   ResultSet rs  = null;
   Clob clob = rs.getClob(1);
      Reader reader = clob.getCharacterStream();
      /***
       * 也可以直接得到reader
       * reader = rs.getCharacterStream(1);
       * */
      File file = new File("test.txt");
      Writer writer = new BufferedWriter(new FileWriter(file));
      char[]buff = new char[1024];
      for(int i=0;(i=reader.read(buff))>0;){
       writer.write(buff,0, i);
      }
      writer.close();
      reader.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 

//读取字节流,插入
  try {
   PreparedStatement pstmt = null;
   File file = new File("xxx.gif");
   InputStream in = new BufferedInputStream(new FileInputStream(file));
      pstmt.setBinaryStream(1, in,(int)file.length());
      pstmt.executeUpdate();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

/**
   * 读取数据库 放入文件Blob储存二进制
   * 基本思路:放入缓冲区byte中,然后写入文件
   * */
  try {
   ResultSet rs = null;
   InputStream in =rs.getBinaryStream(1);
      File file = new File("test.gif");
      OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
      byte[]buff = new byte[1024];
      for(int i=0;(i=in.read(buff))>0;){
       out.write(buff,0, i);
      }
      out.close();
      in.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics