数据库 BLOB对象

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BlobPros {
 private static final String URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=qhjcpt";

 private Connection conn = null;

 private PreparedStatement pstmt = null;

 private ResultSet rs = null;

 private File file = null;

 public BlobPros() {
 }

 /**
  * 向数据库中插入一个新的BLOB对象
  *
  * @param infile
  *            要输入的数据文件
  * @throws java.lang.Exception
  */
 public void blobInsert(String infile) throws Exception {
  FileInputStream fis = null;
  try {
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   conn = DriverManager.getConnection(URL, "sa", "");

   file = new File(infile);
   fis = new FileInputStream(file);
   // InputStream fis = new FileInputStream(infile);
   pstmt = conn.prepareStatement("insert into a(bb,aa) values(?,?)");
   pstmt.setString(1, file.getName()); // 把传过来的第一个参数设为文件名
   // pstmt.setBinaryStream(2,fis,(int)file.length());
   // //这种方法原理上会丢数据,因为file.length()返回的是long型
   pstmt.setBinaryStream(2, fis, fis.available()); // 第二个参数为文件的内容
   pstmt.executeUpdate();
  } catch (Exception ex) {
   System.out.println("[blobInsert error : ]" + ex.toString());
  } finally {
   // 关闭所打开的对像//
   pstmt.close();
   fis.close();
   conn.close();
  }
 }

 /**
  * 从数据库中读出BLOB对象
  *
  * @param outfile
  *            输出的数据文件
  * @param id
  *            要取的文件在数据库中的ID
  * @throws java.lang.Exception
  */

 public void blobRead(String outfile, int id) throws Exception {
  FileOutputStream fos = null;
  InputStream is = null;
  byte[] Buffer = new byte[4096];

  try {
   Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
   conn = DriverManager.getConnection(URL, "sa", "");
   pstmt = conn.prepareStatement("select aa from a where id=?");
   pstmt.setInt(1, id); // 传入要取的图片的ID
   rs = pstmt.executeQuery();
   rs.next();

   file = new File(outfile);
   if (!file.exists()) {
    file.createNewFile(); // 如果文件不存在,则创建
   }
   fos = new FileOutputStream(file);
   is = rs.getBinaryStream("aa");
   int size = 0;
   /*
    * while(size != -1) { size = is.read(Buffer); //从数据库中一段一段的读出数据
    * //System.out.println(size); if(size != -1) //-1表示读到了文件末
    * fos.write(Buffer,0,size); }
    */
   while ((size = is.read(Buffer)) != -1) {
    // System.out.println(size);
    fos.write(Buffer, 0, size);
   }

  } catch (Exception e) {
   System.out.println("[OutPutFile error : ]" + e.getMessage());
  } finally {
   // 关闭用到的资源
   fos.close();
   if (rs != null) {
    rs.close();
   }
   pstmt.close();
   conn.close();
  }
 }

// public static void main(String[] args) {
//  try {
//           
//   BlobPros blob = new BlobPros();
//   // blob.blobInsert("e:/天气.xls");
//   blob.blobRead("d:/啊.txt", 1);
//  } catch (Exception e) {
//   System.out.println("[Main func error: ]" + e.getMessage());
//  }
// }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值