emrefan
2010-05-14 07:45:47 UTC
We have Informix 7.3 here and for various reasons we will be stuck
with it for a while. I need to write some graphical objects into a
database table and I was just assessing the feasibility but failed at
the first hurdle <sigh>. I created the simplest of tables, like this
one below:
create table tt_pics (
id char(10),
pic byte in blobspace
)
I then tried two ways of putting things into this table:
Method 1: using the LOAD command
The manual was very terse about loading stuff into a table with a byte
column. So I just took a shoot in the dark and hoped things would go
right.
I created a text file with the following content,
somebody|aabbccddee|
That 2nd column's value was just some bytes' values in 2-char hex
number form.
I then tried loading this text file into the table with an SQL command
like this:
load from 'the-text-file.txt' insert into tt_pics
Needless to say, I got an error message:
-------------------------------------------
603: Cannot close blob.
847: Error in load file line 1.
Error in line 18
Near character position 22
--------------------------------------------
So, no go there. Let's talk about method 2.
Method 2:
I wrote a java program like this below:
------------------------------------------- snip
--------------------------------------------------------------------
import java.io.*;
import java.sql.*;
public class WriteBlob {
static public void main( String args[] ) {
String url = "jdbc:informix-sqli://localhost:8899/somedb:" +
"INFORMIXSERVER=ifxsvr;user=whoever;password=dontcare";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strsql;
File picFile = new File("somepic.jpg");
java.io.BufferedInputStream bin = null;
int i;
try {
Class.forName( "com.informix.jdbc.IfxDriver" );
con = DriverManager.getConnection( url );
strsql = "insert into tt_pics values(?,?)";
pstmt = con.prepareStatement( strsql );
FileInputStream fis = new FileInputStream( picFile );
pstmt.setString( 1, "somebody" );
pstmt.setBinaryStream( 2, (InputStream) fis, 10 );
i = pstmt.executeUpdate();
System.out.println("i=" + i);
} catch (ClassNotFoundException ce) {
System.out.println( "Class Not Found!!" );
} catch (SQLException se) {
System.out.println( "SQL exception: " + se.getMessage() );
se.printStackTrace();
} catch (Exception ie) {
System.out.println( "I/O Exception " + ie.getMessage() );
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception ep) {
System.out.println( ep.getMessage() );
}
}
}
}
------------------------------------------------ snip
-----------------------------------------------------------
When I ran it, I got the following error message:
SQL exception: Cannot close blob.
java.sql.SQLException: Cannot close blob.
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:
373)
at com.informix.jdbc.IfxSqli.a(IfxSqli.java:3207)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3517)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2352)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2268)
at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:775)
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:291)
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1233)
at
com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedState
ment.java:408)
at WriteBlob.main(WriteBlob.java:21)
There, I am now stuck. Can anybody give me a hand?
with it for a while. I need to write some graphical objects into a
database table and I was just assessing the feasibility but failed at
the first hurdle <sigh>. I created the simplest of tables, like this
one below:
create table tt_pics (
id char(10),
pic byte in blobspace
)
I then tried two ways of putting things into this table:
Method 1: using the LOAD command
The manual was very terse about loading stuff into a table with a byte
column. So I just took a shoot in the dark and hoped things would go
right.
I created a text file with the following content,
somebody|aabbccddee|
That 2nd column's value was just some bytes' values in 2-char hex
number form.
I then tried loading this text file into the table with an SQL command
like this:
load from 'the-text-file.txt' insert into tt_pics
Needless to say, I got an error message:
-------------------------------------------
603: Cannot close blob.
847: Error in load file line 1.
Error in line 18
Near character position 22
--------------------------------------------
So, no go there. Let's talk about method 2.
Method 2:
I wrote a java program like this below:
------------------------------------------- snip
--------------------------------------------------------------------
import java.io.*;
import java.sql.*;
public class WriteBlob {
static public void main( String args[] ) {
String url = "jdbc:informix-sqli://localhost:8899/somedb:" +
"INFORMIXSERVER=ifxsvr;user=whoever;password=dontcare";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String strsql;
File picFile = new File("somepic.jpg");
java.io.BufferedInputStream bin = null;
int i;
try {
Class.forName( "com.informix.jdbc.IfxDriver" );
con = DriverManager.getConnection( url );
strsql = "insert into tt_pics values(?,?)";
pstmt = con.prepareStatement( strsql );
FileInputStream fis = new FileInputStream( picFile );
pstmt.setString( 1, "somebody" );
pstmt.setBinaryStream( 2, (InputStream) fis, 10 );
i = pstmt.executeUpdate();
System.out.println("i=" + i);
} catch (ClassNotFoundException ce) {
System.out.println( "Class Not Found!!" );
} catch (SQLException se) {
System.out.println( "SQL exception: " + se.getMessage() );
se.printStackTrace();
} catch (Exception ie) {
System.out.println( "I/O Exception " + ie.getMessage() );
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception ep) {
System.out.println( ep.getMessage() );
}
}
}
}
------------------------------------------------ snip
-----------------------------------------------------------
When I ran it, I got the following error message:
SQL exception: Cannot close blob.
java.sql.SQLException: Cannot close blob.
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:
373)
at com.informix.jdbc.IfxSqli.a(IfxSqli.java:3207)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3517)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2352)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2268)
at com.informix.jdbc.IfxSqli.executeCommand(IfxSqli.java:775)
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:291)
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1233)
at
com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedState
ment.java:408)
at WriteBlob.main(WriteBlob.java:21)
There, I am now stuck. Can anybody give me a hand?