开发者

insert a picture into database(sqlite) with java code. what should i do?

开发者 https://www.devze.com 2022-12-18 14:49 出处:网络
in the sqlite database, i set a field typed blob. then, i wrote java code: PreparedStatement preStmt = con.prepar开发者_如何学运维eStatement(\"INSERT INTO zz(pic) VALUES(?)\");

in the sqlite database, i set a field typed blob. then, i wrote java code:

PreparedStatement preStmt = con.prepar开发者_如何学运维eStatement("INSERT INTO zz(pic) VALUES(?)");
preStmt.setBinaryStream(1, new FileInputStream(file), file.length());

i got a exception:

java.lang.AbstractMethodError: org.sqlite.PrepStmt.setBinaryStream(ILjava/io/InputStream;J)V

any suggestion ?

Or, what code i should write if i want to use save binary number in sqlite ?


I've had good success with the sqlite jdbc dirver from xerial.org: http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC

Xerial's SQLite JDBC implementation has PreparedStatement.setBytes(index, byte[]). You should be able to get a byte array from your FileInputStream using the code on the following link: http://www.java-tips.org/java-se-tips/java.io/reading-a-file-into-a-byte-array.html

When storing things as Blobs be careful of the following issues: http://effbot.org/zone/sqlite-blob.htm

I hope this helps.

-Alex


The AbstractMethodError API documentation tells the following:

Thrown when an application tries to call an abstract method. Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

Apparently you have an older version of the JDBC driver wandering in the classpath, or your JDBC driver needs to be upgraded to match the JDBC API in the JRE version used. You can get the latest version at the Sqlite homepage.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号