爱问知识人 爱问教育 医院库

JDBC中处理二进制类型数据是什么样的?

首页

JDBC中处理二进制类型数据是什么样的?

JDBC中处理二进制类型数据是什么样的?

提交回答

全部答案

    2018-04-03 08:53:33
  •   代码如下:package jdbc;
    import java。io。BufferedInputStream;
    import java。io。BufferedOutputStream;
    import java。io。File;
    import java。
      io。FileInputStream; import java。io。FileOutputStream; import java。io。InputStream; import java。io。OutputStream; import java。
      sql。Connection; import java。sql。PreparedStatement; import java。sql。ResultSet; /** * * @author HaoWang */ public class BlobTest { public static void main(String[] args) { // create(1,"C:\\a。
      jpg"); read(1,"C:\\a_bak。jpg"); } public static void create(int bid, String path) { Connection conn = null; String sql = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils。
      getConnection(); sql = "insert into blob_test(bid,content) value(?,?)"; ps = conn。prepareStatement(sql); ps。setInt(1, bid); File file = new File(path); InputStream in = new BufferedInputStream(new FileInputStream(file)); ps。
      setBinaryStream(2, in, file。length()); int i = ps。executeUpdate(); System。out。println("i="+i); in。close(); } catch (Exception ex) { System。
      out。println(ex。toString()); } finally { JdbcUtils。free(conn, ps, rs); } } public static void read(int bid, String path) { Connection conn = null; String sql = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JdbcUtils。
      getConnection(); sql = "select content from blob_test where bid=?"; ps = conn。prepareStatement(sql); ps。setInt(1, bid); rs = ps。
      executeQuery(); while(rs。next()) { InputStream in = rs。getBinaryStream(bid); File file = new File(path); 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(); } } catch (Exception ex) { System。out。println(ex。
      toString()); } finally { JdbcUtils。free(conn, ps, rs); } } } 编辑特别推荐: java实现缩略图和抓屏幕 Java中的Enum的使用与分析 Eclipse远程调试Java。
      

    赵***

    2018-04-03 08:53:33

类似问题

换一换

相关推荐

正在加载...
最新问答 推荐信息 热门专题 热点推荐
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200

热点检索

  • 1-20
  • 21-40
  • 41-60
  • 61-80
  • 81-100
  • 101-120
  • 121-140
  • 141-160
  • 161-180
  • 181-200
返回
顶部
帮助 意见
反馈

确定举报此问题

举报原因(必选):