我正在使用SQLite,我无法将图像保存到数据库.这是我的代码:
File file = new File(url);
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {}
fileLenght = (int) file.length();
stat.executeUpdate("create table "+tableName+" (id int,name String ,image Blob, features String);");
prep = conn.prepareStatement("insert into "+tableName+" values (?, ?, ?, ?);");
prep.setBinaryStream(3, fis, fileLenght);
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
java.sql.SQLException: not implemented by SQLite JDBC driver
at org.sqlite.Unused.unused(Unused.java:29)
at org.sqlite.Unused.setBinaryStream(Unused.java:58)
Run Code Online (Sandbox Code Playgroud)
我使用以下jar:sqlitejdbc-v056.jar.
有任何想法吗?谢谢
小智 9
您可以非常轻松地将图像插入数据库,说SQLite请参阅下面的代码.我确实使用了DBase Connection的属性文件,你可以放在任何地方.
public class JDBCSqliteConn
{
public static void main(String args[]) throws FileNotFoundException, IOException
{
Connection connection = null;
//ResultSet resultSet = null;
PreparedStatement ps = null;
String file = "C:\\Fingerprint\\histoImg_med.png";
Properties prop = new Properties();
int s = 0;
byte[] person_image = null;
File image = new File(file);
FileInputStream fis = new FileInputStream(image);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;)
{
bos.write(buf, 0, readNum);
//no doubt here is 0
/*Writes len bytes from the specified byte array starting at offset
off to this byte array output stream.*/
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
person_image = bos.toByteArray();
try {
prop.load(new FileInputStream("C:\\dbconfig.properties"));
Class.forName(prop.getProperty("driver"));
connection = DriverManager.getConnection(prop.getProperty("url"));
ps = connection.prepareStatement("INSERT INTO epmc_tbl_test_img (hhld_photo) VALUES (?)");
ps.setBytes(1, person_image);
s = ps.executeUpdate();
if (s > 0)
{
System.out.println("Image Uploaded");
}
ps.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ps.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
背后的逻辑是将图像转换为bytearray然后存储到数据库,SQLite接受Image类型BLOB的字节数组
这是我的dbconfig.properties文件
driver = org.sqlite.JDBC
url = jdbc:sqlite:C:\\Ronald\\Personal\\epmc\\JavaJ2EE\\EPMC.db
Run Code Online (Sandbox Code Playgroud)
尝试使用此代码.
| 归档时间: |
|
| 查看次数: |
15096 次 |
| 最近记录: |