資源描述:
《java對blob的存取》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、java對blob的存取作者:未知時(shí)間:2005-09-1323:33出處:Blog.ChinaUnix.net責(zé)編:chinaitpower??????????????摘要:java對blob的存取此實(shí)現(xiàn)為用java訪問mysql的blob,對圖片進(jìn)行存取/**?*Title:????????BlobPros.java?*Project:??????test?*Description:?把圖片存入mysql中的blob字段,并取出?*CallModule:?mtools數(shù)據(jù)庫中的tmp表??*File:????????C:
2、downloadsluozsh.jpg?*Copyright:???Copyright(c)2003-2003?*Company:?????uniware?*CreateDate:?2002.12.5?*@Author:?????ChenQH?*@version1.0版本*??????*?*?Revisionhistory?*?Name????????Date????Description?*?----????----???????-----------?*?Chenqh??2003.12.5???????對圖片進(jìn)行存取?*
3、?*note:????????要把數(shù)據(jù)庫中的Blob字段設(shè)為longblob????????*?*///packagecom.uniware;importjava.io.*;importjava.util.*;importjava.sql.*;publicclassBlobPros{???privatestaticfinalStringURL="jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";???privateCo
4、nnectionconn=null;???privatePreparedStatementpstmt=null;???privateResultSetrs=null;???privateFilefile=null;??????publicBlobPros()???{???}???/**???*向數(shù)據(jù)庫中插入一個(gè)新的BLOB對象(圖片)???*@paraminfile要輸入的數(shù)據(jù)文件???*@throwsjava.lang.Exception???*/??publicvoidblobInsert(Stringinfile)th
5、rowsException??{??????FileInputStreamfis=null;????????????try??????????{??????????????Class.forName("org.gjt.mm.mysql.Driver").newInstance();??????????????conn=DriverManager.getConnection(URL);?????????????????????????file=newFile(infile);??????????????fis=newFileI
6、nputStream(file);??????????????//InputStreamfis=newFileInputStream(infile);???????pstmt=conn.prepareStatement("insertintotmp(descs,pic)values(?,?)");???????pstmt.setString(1,file.getName());???//把傳過來的第一個(gè)參數(shù)設(shè)為文件名??????????????//pstmt.setBinaryStream(2,fis,(int)file.l
7、ength());??//這種方法原理上會(huì)丟數(shù)據(jù),因?yàn)閒ile.length()返回的是long型???????pstmt.setBinaryStream(2,fis,fis.available());?//第二個(gè)參數(shù)為文件的內(nèi)容???????pstmt.executeUpdate();?????}??????????catch(Exceptionex)??????????{?????????System.out.println("[blobInserterror:]"+ex.toString());??????????}?
8、?????finally???????{??????????????//關(guān)閉所打開的對像//??????????????pstmt.close();??????????????fis.close();??????????????conn.close();??????????}???}???