文档详情

JAVA中SQLlite数据库BLOB数据类型的存取.docx

发布:2017-06-08约4.01千字共3页下载文档
文本预览下载声明
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);//读取本地文件 ? ? ? ? FileInputStream fis = 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 { ? ? ? ? ? ? PreparedStatement pstmt = conn.prepareStatement(
显示全部
相似文档