sql 2k中的图片保存和获取----引申到文件保存和获取 (转)

来源:这里教程网 时间:2026-03-02 09:57:49 作者:
sql 2k中的图片保存和获取----引申到文件保存和获取 (转)[@more@]

  当大家想完成一个任务的时候,首先是考虑怎么设计,(会说到软工就离题了)。实现的时候肯定是想复用现成的代码,或者找个相似的来参考。看到一些关于文章留下不少代码,就是论坛也有不少人高分求代码(不会是急功近利吧)。所以偶就把自己常常写得比较典型有用的代码给大家贴出来,供大家参考,水平有限,希望多提意见,咔咔

  这次是图片的保存,在sql 2k里面图片一般存为image或者varbinary类型,操作的时候没有弄过的感觉很迷茫,已经会的了就很简单。通过自己的体会,感觉通过字节数组来保存是最方便的,也是比较不容易出错的。通过流的方式,总会出一些问题。在获取图片或者文件时,两者都是比较稳定的,无论是用微软的jdbc驱动还是使用JDBCodbc桥。

/*
 * Created on 2003-5-13
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package scut.ailab.sql2ktools;

/**
 * @author youyongming
 * 针对数据库字段的image和varbinary的存取
 */
import java.sql.*;
import java.io.*;
import java.nio.*;

public class ImageOperation {
 义数据库连接
 private Connection conn;
 
 /**
  * 使用指定的连接构造
  * @param conn 数据库连接
  */
 public ImageOperation(Connection conn)
 {
 this.conn = conn;
 if (this.conn == null)//如果没有初始化连接,默认初始化
 initConn();
 }
 
 /**
  * 默认的数据库连接
  * 当构造函数的conn为null时调用,做演示用
  */
 private void initConn()
 {
 String user = "DevTeam";
 String password = "DevTeam";
 String driverstr = "sun.jdbc.odbc.JdbcOdbcDriver";
 String url = "jdbc:odbc:chinascutface";
 try{
 Class.forName(driverstr).newInstance();
 conn = DriverManager.getConnection(url,user,password);
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 }
 
 /**
  * 保存图像文件到数据库指定的位置
  * @param sqlstr 指定位置的sql语句
  * @param file 指定图像文件
  * @return 保存成功返回true,否则返回false
  */
 public boolean storeImage(String sqlstr, File file)
 {
 try{
 开文件
 FileInputStream fin= new FileInputStream(file);
 一个缓冲保存数据
 ByteBuffer nbf = ByteBuffer.allocate((int)file.length());
 byte[] array = new byte[1024];
 int offset = 0, length=0;
 存数据
 while ((length=fin.read(array)) >0)
 {
  //nbf.put(array, offset, length);
  if (length != 1024)
 nbf.put(array, 0, length);
  else
 nbf.put(array);
  offset += length;
 }
 闭文件
 fin.close();
 建一个数组保存要写的内容
 byte[] content = nbf.array();
 存数据
 return setImage(sqlstr, content);
 }
 catch(FileNotFoundException e)
 {
 e.printStackTrace();
 }
 catch(IOException e)
 {
 e.printStackTrace();
 }
 果发生文件读写错误都会返回false
 return false; 
 }
 
 /**
  * 保存字节数组到数据库指定位置
  * @param sqlstr 描述位置的sql语句
  * @param in 要保存的字节数组
  * @return 返回是否成功保存
  */
 private boolean setImage(String sqlstr, byte[] in)
 {
 boolean flag = false;
 if (sqlstr == null)
 sqlstr = "select img from image_table";
 try{
 Statement stmt = conn.createStatement(
 ResultSet.TYPE_SCROLL_INSENSITIVE,
 ResultSet.CONCUR_UPDATABLE);
 ResultSet rs = stmt.executeQuery(sqlstr);
 if (rs.next())
 {
 rs.updateBytes(1, in);
 rs.updateRow();
 }
 rs.close();
 flag = true;
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 return flag;
 }
 
 /**
  * 从数据库指定位置获取图像文件
  * @param sqlstr 描述数据库位置的sql语句
  * @param file 图像文件保存路径
  * @return 是否正确返回文件
  */
 public boolean retrieveImage(String sqlstr, File file)
 {
 boolean flag = false;
 if (sqlstr == null)
 sqlstr = "select img from image_table";
 try {
  Statement stat = conn.createStatement();
  ResultSet rs=stat.executeQuery(sqlstr);
  while (rs.next())
  {
 byte[] content = rs.getBytes(1);
 if (!rs.wasNull())//找到数据
 {
  flag = true;
  存到指定文件
  FileOutputStream fout= new FileOutputStream(file);
  fout.write(content);;
  fout.flush();
  fout.close();
 }
 存第一条纪录,跳出
 break;
  }
  闭连接
  rs.close();
  stat.close();
 }
 catch (SQLException sqle) {
  sqle.printStackTrace() ;
 }
 catch(Exception e)
 {
 e.printStackTrace();
 flag = false;//如果有io错误则不成功
 }
 return flag;
 } 
 
 试
 public static void main(String[] args)
 {
 ImageOperation iop = new ImageOperation(null);
 try{//使用一个图片作为测试文件
 File file = new File("wodao.gif");
 if (iop.storeImage(null, file))
 System.out.print("true");
 else
 System.out.print("false");
 file = new File("new.gif");
 if (iop.retrieveImage(null, file))
 System.out.print("true");
 else
 System.out.print("false");
 }
 catch(Exception e)
 {
 e.printStackTrace();
 }
 }
}


相关推荐