当我想将blob插入数据库时,我遇到了这个异常:
java.lang.NullPointerException
at com.informix.jdbc.IfxSmartBlob.IfxLoColInfo(IfxSmartBlob.java:313) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxSmBlob.i(IfxSmBlob.java:791) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxSmBlob.a(IfxSmBlob.java:520) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:295) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1283) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxPreparedStatement.executeUpdate(IfxPreparedStatement.java:421) ~[ifxjdbc.jar:?]
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.pmiExecuteUpdate(WSJdbcPreparedStatement.java:1187) ~[com.ibm.ws.runtime.jar:?]
at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeUpdate(WSJdbcPreparedStatement.java:804) ~[com.ibm.ws.runtime.jar:?]
有没有办法解决什么问题?我尝试google但没有关于此异常的信息
语句如下:
insert into my_table (col1, col2, col3, blob) VALUES(?, ?, ?, ?)
我正在使用informix db: 12.10.FC4
更新
我尝试从java或groovy这样的独立应用程序中运行它。当我想从Websphere Server 8.5.5.2上运行的Web应用程序插入blob时,就会发生此异常
DDL:
CREATE TABLE my_table (
col1 CHAR(24) not null,
col2 VARCHAR(255) not null,
col3 CHAR(1) default '0' not null,
blob_col blob not null
)
Groovy:有效的脚本
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import groovy.sql.Sql
class sql {
static main(args) {
println "START"
def sql = Sql.newInstance( 'url', 'user', 'pass', 'com.informix.jdbc.IfxDriver' );
sql.connection.autoCommit = false
def ps = sql.connection.prepareStatement("insert into my_table (col1,col2,col3,blob_col) values(?,?,?,?)");
ps.setString(1, "1");
ps.setString(2, "1");
ps.setString(3, "1");
byte[] bytes = new byte[100];
ps.setBytes(4, bytes);
ps.execute()
ps.close()
fis.close()
sql.commit()
println "END"
}
}
UPDATE_2:
另一个奇怪的例外。我真的不知道我们的数据库专家用这个数据库做什么
java.lang.ClassCastException: com.informix.jdbc.IfxUDT incompatible with com.informix.jdbc.IfxInteger
at com.informix.jdbc.IfxSmartBlob.IfxLoOpen(IfxSmartBlob.java:494) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxBblob.<init>(IfxBblob.java:77) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxSmBlob.toBytes(IfxSmBlob.java:1030) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxResultSet.getBytes(IfxResultSet.java:1216) ~[ifxjdbc.jar:?]
at com.informix.jdbc.IfxResultSet.getBytes(IfxResultSet.java:1231) ~[ifxjdbc.jar:?]
at com.ibm.ws.rsadapter.jdbc.WSJdbcResultSet.getBytes(WSJdbcResultSet.java:909) ~[com.ibm.ws.runtime.jar:?]
但我仍然想知道Google上没有关于此错误的信息
答案 0 :(得分:0)
你能创建简单的test_blobs
表并在其中插入一些图片,如我的Jython代码:
# writing picture into blob
db = DriverManager.getConnection(db_url, usr, passwd)
blob = FileInputStream('picture.jpeg')
pstm = db.prepareStatement("INSERT INTO test_blobs (content) VALUES (?)")
pstm.setBinaryStream(1, blob)
rs = pstm.execute()
blob.close()
# reading just inserted blob
pstm = db.prepareStatement("SELECT content FROM test_blobs")
rs = pstm.executeQuery()
while (rs.next()):
image_stream = rs.getBinaryStream(1)
fout = ByteArrayOutputStream()
while 1:
b = image_stream.read()
if b < 0:
break
fout.write(b)
arr = fout.toByteArray()
f = open('picture_from_blob.jpeg', 'wb')
f.write(arr)
f.close()
同时告诉我们: