insert a picture into database(sqlite) with java c

2019-07-12 03:46发布

问题:

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

PreparedStatement preStmt = con.prepareStatement("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 ?

回答1:

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



回答2:

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.