欢迎来到天天文库
浏览记录
ID:44325089
大小:39.00 KB
页数:4页
时间:2019-10-20
《java对blob的存取》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、java对blob的存取作者:未知时间:2005-09-1323:33出处:Blog.ChinaUnix.net责编:chinaitpower 摘要:java对blob的存取此实现为用java访问mysql的blob,对图片进行存取/** *Title: BlobPros.java *Project: test *Description: 把图片存入mysql中的blob字段,并取出 *CallModule: mtools数据库中的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 对图片进行存取 *
3、 *note: 要把数据库中的Blob字段设为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() { } /** *向数据库中插入一个新的BLOB对象(图片) *@paraminfile要输入的数据文件 *@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()); //把传过来的第一个参数设为文件名 //pstmt.setBinaryStream(2,fis,(int)file.l
7、ength()); //这种方法原理上会丢数据,因为file.length()返回的是long型 pstmt.setBinaryStream(2,fis,fis.available()); //第二个参数为文件的内容 pstmt.executeUpdate(); } catch(Exceptionex) { System.out.println("[blobInserterror:]"+ex.toString()); }
8、 finally { //关闭所打开的对像// pstmt.close(); fis.close(); conn.close(); } }
此文档下载收益归作者所有