資源描述:
《java中sqllite數(shù)據(jù)庫(kù)blob數(shù)據(jù)類型的存取》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫(kù)。
1、JAVA中SQLlite數(shù)據(jù)庫(kù)BLOB數(shù)據(jù)類型的存取找了好多資料,在sqllite在寫(xiě)入文件沒(méi)什么難的,但是在取出blob的生成本地文件的時(shí)候存在問(wèn)題,而且網(wǎng)上大部分都是針對(duì)手機(jī)開(kāi)發(fā)的,很少有java對(duì)sqllite的介紹??/**???*sqllite數(shù)據(jù)庫(kù)文件的存儲(chǔ)???*???*@return???*/??privatestaticvoidtestSqlliteFile()throwsIOException{????Connectionconn=null;????Statementsta=null;????try{??????Class.forName("org.sqlit
2、e.JDBC");????}catch(ClassNotFoundExceptione){??????e.printStackTrace();?//TochangebodyofcatchstatementuseFile
3、Settings
4、FileTemplates.????}????try{??????conn=DriverManager.getConnection("jdbc:sqlite:"+SetCustomFinal.getInstance().getFileDiskStoreFolder()+SetCustomFinal.getInstance().getFileIte
5、mStoreFolder()+"\DBU1111111.DB","","");????}catch(SQLExceptione){??????e.printStackTrace();?//TochangebodyofcatchstatementuseFile
6、Settings
7、FileTemplates.????}????try{??????sta=conn.createStatement();????}catch(SQLExceptione){??????e.printStackTrace();?//TochangebodyofcatchstatementuseFile
8、Se
9、ttings
10、FileTemplates.????}????//上面沒(méi)什么好廢話的鏈接sqllite的jdbc????//?讀取數(shù)據(jù)????FileInsertefile=newFile("D:\進(jìn)程.chm");//讀取本地文件????FileInputStreamfis=newFileInputStream(Insertefile);//將本地文件轉(zhuǎn)為文件流????byte[]bs=newbyte[Integer.parseInt(""+Insertefile.length())];//sqllite不能直接存如文件流,直接存byte【】數(shù)組。數(shù)組大小int可能會(huì)超限所以用
11、Integer????fis.read(bs,0,bs.length);//將文件流寫(xiě)入byte【】數(shù)組????fis.close();//關(guān)閉文件流????//下面是將byte【】數(shù)組插入????Stringsb=("insertintofilelist?(file_uid,folder_uid,file)values('U14012102074641','U14012102074688',?)?");????PreparedStatementprep=null;????try{??????prep=conn.prepareStatement(sb);??????prep.se
12、tBytes(1,bs);//傳入整理好的byte【】數(shù)組??????prep.executeUpdate();????}catch(SQLExceptione){??????e.printStackTrace();????}????//下面是將寫(xiě)入的文件從sqllite數(shù)據(jù)庫(kù)讀取出來(lái),和寫(xiě)入類似????try{??????PreparedStatementpstmt=conn.prepareStatement("SELECTFILEFROMfilelistWHEREFILE_UID=?");??????pstmt.setString(1,"U14012102074641");/
13、/傳入要取的圖片的ID??????FileOutputStreamfos=null;??????Strings="SELECTFILE_UID,FILEFROMfilelistWHEREFILE_UID=?";??????PreparedStatementpstmts=conn.prepareStatement(s);??????pstmts.setString(1,"U14012102074641");//傳入要讀取的FILE_UID??????ResultSetrs=pstm