免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2811 | 回复: 3
打印 上一主题 下一主题

在400中存储BLOB图像数据以及相关操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-09-11 14:49 |只看该作者 |倒序浏览
首先申明不是原创,参考了ibm的一个教程,具体教程的地址在这里:
http://www-128.ibm.com/developer ... gal/0310bhogal.html
以前一直以为400不能存储图像数据,比如blob的数据,因为数据量可能超过了单个field的容量,现在看来完全是可以的。
针对这篇文章,我做了测试,用的是jt400.jar。有以下的一些发现。
1。strsql => create table bookcovers (bookisbn varchar(10) not null, bookcover blob (100K) not null, primary key(bookisbn))
我是直接用sql建立的表,系统提示没有jornal,当时没在意,后来发现很关键。
2。
如果用这个例子里面的方法,建立的table必须是由jornal的,如果没有会出错“the table is invaild for operation......”,
因为这是一个事务的操作,事务操作里面的table必须是有日志的。建立日志可以用这个
STRJRNPF FILE(lib name/pf name) JRN(lib name/journal name)
3. 这个例子里面的代码不全,需要注意。
比如:在insert BOLB数据的例子里面没有提交事务,这样是不会有数据插入的,需要connection.commit();
AS400JDBCConnectionPoolDataSource datasource = new AS400JDBCConnectionPoolDataSource(
    "your400");
  datasource.setUser("yourid");
  datasource.setPassword("yourpwd");
  // datasource.

  // Create an AS400JDBCConnectionPool object.
  AS400JDBCConnectionPool pool = new AS400JDBCConnectionPool(datasource);

  // Adds 10 connections to the pool that can be used by the application
  // (creates the physical database connections based on the data source).
  try {
   pool.fill(1);
  } catch (ConnectionPoolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  Connection connection = null;
  // Get a handle to a database connection from the pool.
  try {
   connection = pool.getConnection();
  } catch (ConnectionPoolException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }


  // ... Perform miscellenous queries/updates on the database.
  connection.setAutoCommit(false);

  System.out.println("connection OK!");

PreparedStatement preparedStatement = connection  .prepareStatement("INSERT INTO yourlib.BOOKCOVERS VALUES(?,?)");  
File imageFile = new File("c:\\\\redbookcover.jpg");

System.out.println(filelength);
InputStream inputStream = null;
try {
inputStream = new FileInputStream(imageFile);  } catch (FileNotFoundException e) {  // TODO Auto-generated catch block  e.printStackTrace();  }
preparedStatement.setString(1, "0738425826");
preparedStatement.setBinaryStream(2, inputStream, (int)(imageFile.length()));
preparedStatement.executeUpdate();
connection.commit();
               
System.out.println("insert OK!");

下面是读取BLOB数据的代码:
   PreparedStatement preparedStatement =
                        connection.prepareStatement("SELECT BOOKCOVER FROM yourlib.BOOKCOVERS WHERE BOOKISBN=?");
                        preparedStatement.setString(1, "0738425826");
                        ResultSet resultSet = preparedStatement.executeQuery();
                        while (resultSet.next()) {
                        // materialization of the Blob
                        Blob blob = resultSet.getBlob(1);
                        InputStream inputStream1 = blob.getBinaryStream();
                        File fileOutput = new File("C:\\\\clonedredbookcover.jpg");
                        FileOutputStream fo = null;
                        try {
                                fo = new FileOutputStream(fileOutput);
                        } catch (FileNotFoundException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }

                        int c;

                        try {
                                while ((c = inputStream1.read()) != -1)
                                        fo.write(c);
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        try {
                                fo.close();
                        } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                        System.out.println("Blob retrieved");
                }
时间比较长,我这里居然花了1分钟多才读取结束,看来效率不是很好~
也许有别的办法解决的,我还没找到。

论坛徽章:
0
2 [报告]
发表于 2006-09-11 15:24 |只看该作者
沙发。我顶~~~~~~~~~~~~~~~~~~~~

论坛徽章:
0
3 [报告]
发表于 2007-08-09 14:07 |只看该作者

很好!

有没有RPG的?

论坛徽章:
0
4 [报告]
发表于 2007-08-10 08:36 |只看该作者
存储图片不是问题, 怎么使用这些图片呢

在dspf, prtf使用,还是只能给WEB使用?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP