栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC编程实现文件、图片的存储方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC编程实现文件、图片的存储方法

本文实例讲述了JDBC编程实现文件、图片的存储方法。分享给大家供大家参考,具体如下:


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);
  }
 }
}

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);
  }
 }
}

希望本文所述对大家Java程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151125.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号