JAVA 中SQLlite数据库BLOB数据类型的存取
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JAVA 中SQLlite数据库BLOB数据类型的存取
找了好多资料,在sqllite在写入文件没什么难的,但是在取出blob的生成本地文件的时候存在问题,而且网上大部分都是针对手机开发的,很少有java对sqllite的介绍
/**
* sqllite数据库文件的存储
*
* @return
*/
private static void testSqlliteFile() throws IOException {
Connection conn = null;
Statement sta = null;
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
conn = DriverManager.getConnection("jdbc:sqlite:" + SetCustomFinal.getInstance().getFileDiskStoreFolder() + SetCustomFinal.getInstance().getFileItemStoreFolder() + "\\DBU1111111.DB", "", "");
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
try {
sta = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
//上面没什么好废话的链接sqllite的jdbc
// 读取数据
File Insertefile = new File("D:\\进程.chm");//读取本地文件
FileInputStreamfis = new FileInputStream(Insertefile);//将本地文件转为文件流
byte[] bs = new byte[Integer.parseInt("" + Insertefile.length())];//sqllite不能直接存如文件流,直接存byte【】数组。
数组大小int可能会超限所以用Integer fis.read(bs, 0, bs.length);//将文件流写入byte【】数组
fis.close();//关闭文件流
//下面是将byte【】数组插入
String sb = ("insert into filelist (file_uid,folder_uid,file)values('U14012102074641','U14012102074688',?) ");
PreparedStatement prep = null;
try {
prep = conn.prepareStatement(sb);
prep.setBytes(1, bs);//传入整理好的byte【】数组
prep.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
//下面是将写入的文件从sqllite数据库读取出来,和写入类似
try {
PreparedStatementpstmt = conn.prepareStatement("SELECT FILE FROM filelist WHERE FILE_UID=?");
pstmt.setString(1, "U14012102074641"); //传入要取的图片的ID
FileOutputStreamfos = null;
String s = "SELECT FILE_UID, FILE FROM filelist WHERE FILE_UID=?";
PreparedStatementpstmts = conn.prepareStatement(s);
pstmts.setString(1, "U14012102074641"); //传入要读取的FILE_UID
ResultSetrs = pstmt.executeQuery();
rs.next();
File dqfiles = new File("D:\\test.chm");//写入本地路径
if (!dqfiles.exists()) {
dqfiles.createNewFile(); //如果文件不存在,则创建
}
fos = new FileOutputStream(dqfiles);
byte[] Buffer_s = rs.getBytes("FILE");//从查询中取得存取的blob数组,这里需要注意一下
/*
InputStream is = rs.getBinaryStream("FILE");
如果直接用这个句话来取得查询的值,本来我也是从网上找的方法这么取但是一直有下面的异常,
是jdbc没有对应的实现,更新了jdbcjar包也没能解决所以才采用了上面的方法
java.sql.SQLException: not implemented by SQLite JDBC driver atorg.sqlite.Unused.unused(Unused.java:29)
atorg.sqlite.Unused.getBinaryStream(Unused.java:94)
at
com.ptrue.smartagent.testcase.remote.RemoteTest.testSqlliteFile(RemoteTest.java:143) at com.ptrue.smartagent.testcase.remote.RemoteTest.main(RemoteTest.java:189) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:4 3)
ng.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) */
fos.write(Buffer_s, 0, Buffer_s.length);//将byte【】写入文件
fos.close();
sta.close();
rs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
下面是测试用的数据库表
CREATE TABLE FILELIST( FILE_UID CHAR(15)PRIMARY KEY,
FOLDER_UID CHAR(15) NOT_NULL,
FILE B OLB NOT_NULL)。