- 论坛徽章:
- 0
|
首先申明不是原创,参考了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分钟多才读取结束,看来效率不是很好~
也许有别的办法解决的,我还没找到。 |
|