JDBC编程实现文件、图片的存储方法
本文实例讲述了JDBC编程实现文件、图片的存储方法。分享给大家供大家参考,具体如下:
/*
实现的功能:
用数据库存储文本数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握文件流,数据库的操作不是很多,但是文件流的读写比较多
日期:20131003
作者:烟大阳仔
*/
publicclassCkb_test{
publicstaticvoidmain(String[]args)throwsSQLException,IOException{
//TODOAuto-generatedmethodstub
read();
}
staticvoidcreate()throwsSQLException,IOException
{
Connectionconn=null;
PreparedStatementprest=null;
ResultSetresultset=null;
try{
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
Stringsql="insertintockb_test(text)values(?)";
prest=conn.prepareStatement(sql);
Filefile=newFile("src/cn/com/JDBC/JdbcUtils.java");
Readerreader=newBufferedReader(newFileReader(file));
prest.setCharacterStream(1,reader,(int)file.length());
//4.执行语句
inti=prest.executeUpdate();
reader.close();
System.out.println("i="+i);
}finally
{
JdbcUtils.free(resultset,prest,conn);
}
}
staticvoidread()throwsSQLException,IOException
{
Connectionconn=null;
Statementst=null;
ResultSetresultset=null;
try{
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("selecttextfromckb_test");
//5.处理结果
while(resultset.next())
{
Clobclob=resultset.getClob(1);
Readerreader=clob.getCharacterStream();
//reader=resultset.getCharacterStream(1);
Filefile=newFile("JdbcUtils.java");
Writerwriter=newBufferedWriter(newFileWriter(file));
char[]buff=newchar[1024];
for(inti=0;(i=reader.read(buff))>0;)
{
writer.write(buff,0,i);
}
writer.close();
reader.close();
}
}finally
{
JdbcUtils.free(resultset,st,conn);
}
}
}
/*
实现的功能:
用数据库存储图片数据,并且读取出来放在当前项目里
分析:
难度不是很大,关键是掌握字节流,数据库的操作不是很多,但是文件流的读写比较多,注意更改图片的目录
日期:20131003
作者:烟大阳仔
*/
publicclassPictureBlob{
publicstaticvoidmain(String[]args)throwsSQLException,IOException{
read();
}
staticvoidcreate()throwsSQLException,IOException
{
Connectionconn=null;
PreparedStatementprest=null;
ResultSetresultset=null;
try{
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
Stringsql="insertintoblob_test(big_bit)values(?)";
prest=conn.prepareStatement(sql);
Filefile=newFile("C:\\DocumentsandSettings\\Administrator\\MyDocuments\\MyPictures\\cxg.jpg");
InputStreamin=newBufferedInputStream(newFileInputStream(file));
prest.setBinaryStream(1,in,(int)file.length());
//4.执行语句
inti=prest.executeUpdate();
in.close();
System.out.println("i="+i);
}finally
{
JdbcUtils.free(resultset,prest,conn);
}
}
staticvoidread()throwsSQLException,IOException
{
Connectionconn=null;
Statementst=null;
ResultSetresultset=null;
try{
//2.建立连接
conn=JdbcUtils.getConnection();
//单例设计模式
conn=JdbcUtilsSingle.getInstance().getConnection();
//3.创建语句
st=conn.createStatement();
//4.执行语句
resultset=st.executeQuery("selectbig_bitfromblob_test");
//5.处理结果
while(resultset.next())
{
Blobblob=resultset.getBlob(1);
InputStreamin=blob.getBinaryStream();
//reader=resultset.getCharacterStream(1);
Filefile=newFile("1.jpeg");
OutputStreamout=newBufferedOutputStream(newFileOutputStream(file));
byte[]buff=newbyte[1024];
for(inti=0;(i=in.read(buff))>0;)
{
out.write(buff,0,i);
}
out.close();
in.close();
}
}finally
{
JdbcUtils.free(resultset,st,conn);
}
}
}
希望本文所述对大家Java程序设计有所帮助。
热门推荐
10 五一公司放假祝福语简短
11 追星女孩的祝福语简短
12 给长辈端午祝福语 简短
13 生日祝福语有诗意简短
14 团圆日祝福语简短
15 中秋商务祝福语简短最新
16 温暖文艺简短祝福语短句
17 男孩上学祝福语简短的
18 入住酒店文案祝福语简短