java_JDBC编程实现文件、图片的存储方法,本文实例讲述了JDBC编程实现文
/IT之家 实现的功能: 用数据库存储文本数据,并且读取出来放在当前项目里 分析: 难度不是很大,关键是掌握文件流,数据库的操作不是很多,但是文件流的读写比较多 日期:20131003 作者:烟大阳仔 IT之家/ public class Ckb_test { public static void main(String[] args) throws SQLException, IOException { // TODO Auto-generated method stub read(); } static void create() throws SQLException, IOException { Connection conn=null; PreparedStatement prest=null; ResultSet resultset=null; try { //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 String sql="insert into ckb_test(text) values(?)"; prest=conn.prepareStatement(sql); File file=new File("src/cn/com/JDBC/JdbcUtils.java"); Reader reader=new BufferedReader(new FileReader(file)); prest.setCharacterStream(1, reader, (int)file.length()); //4.执行语句 int i=prest.executeUpdate(); reader.close(); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, prest, conn); } } static void read() throws SQLException, IOException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 resultset=st.executeQuery("select text from ckb_test"); //5.处理结果 while(resultset.next()) { Clob clob=resultset.getClob(1); Reader reader=clob.getCharacterStream(); //reader=resultset.getCharacterStream(1); File file=new File("JdbcUtils.java"); Writer writer=new BufferedWriter(new FileWriter(file)); char[] buff=new char[1024]; for(int i=0;(i=reader.read(buff))>0;) { writer.write(buff,0,i); } writer.close(); reader.close(); } } finally { JdbcUtils.free(resultset, st, conn); } } } /IT之家 实现的功能: 用数据库存储图片数据,并且读取出来放在当前项目里 分析: 难度不是很大,关键是掌握字节流,数据库的操作不是很多,但是文件流的读写比较多,注意更改图片的目录 日期:20131003 作者:烟大阳仔 IT之家/ public class PictureBlob { public static void main(String[] args) throws SQLException, IOException { read(); } static void create() throws SQLException, IOException { Connection conn=null; PreparedStatement prest=null; ResultSet resultset=null; try { //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 String sql="insert into blob_test(big_bit) values(?)"; prest=conn.prepareStatement(sql); File file=new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg"); InputStream in=new BufferedInputStream(new FileInputStream(file)); prest.setBinaryStream(1, in, (int)file.length()); //4.执行语句 int i=prest.executeUpdate(); in.close(); System.out.println("i="+i); } finally { JdbcUtils.free(resultset, prest, conn); } } static void read() throws SQLException, IOException { Connection conn=null; Statement st=null; ResultSet resultset=null; try { //2.建立连接 conn=JdbcUtils.getConnection(); //单例设计模式 conn=JdbcUtilsSingle.getInstance().getConnection(); //3.创建语句 st=conn.createStatement(); //4.执行语句 resultset=st.executeQuery("select big_bit from blob_test"); //5.处理结果 while(resultset.next()) { Blob blob=resultset.getBlob(1); InputStream in=blob.getBinaryStream(); //reader=resultset.getCharacterStream(1); File file=new File("1.jpeg"); OutputStream out=new BufferedOutputStream(new FileOutputStream(file)); byte[] buff=new byte[1024]; for(int i=0;(i=in.read(buff))>0;) { out.write(buff,0,i); } out.close(); in.close(); } } finally { JdbcUtils.free(resultset, st, conn); } } }
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jiaob/java/8559.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
Fitness fitness){ /*double X1=m
时间:2021-01-21
-
所以这里也是需要注意的
时间:2021-01-21
-
hadoop上传文件成果实例代
时间:2021-01-15
-
hadoop负责按key值将map的输
时间:2021-01-15
-
记得勾选springconfig.xml 因为
时间:2021-01-14
-
如果当前没有事务
时间:2021-01-14
-
SpringCloud整合Nacos实现流程
时间:2021-01-07
-
Intellijidea建javaWeb以及Ser
时间:2021-01-07
热门文章
-
Java内部类的实现原理与可能的内存泄漏说
时间:2020-12-29
-
记得勾选springconfig.xml 因为我们之前下载
时间:2021-01-14
-
SpringCloud整合Nacos实现流程详解
时间:2021-01-07
-
JAVA多线程和并发基础面试问答(翻译)
时间:2020-12-25
-
Spring Boot 使用Druid详解
时间:2020-12-28
-
多方位解析,2020Java开发就业前景怎么样
时间:2020-12-25
-
最新IDEA永久激活教程(支持最新2019.2版本
时间:2020-12-25
-
Fitness fitness){ /*double X1=min+0.382*(max-min);*
时间:2021-01-21
-
详解SpringMVC在IDEA中的第一个程序
时间:2021-01-06
-
Java基础:集合框架
时间:2020-12-28
