zoukankan      html  css  js  c++  java
  • SQL Server 对 Image字段进行操作

    将图片写入数据库

     public void testUploadPicture() {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";   
       String url = "jdbc:sqlserver://192.168.0.108:1433;databasename=music";   
       String user = "sa";   
       String password = "mmiku";   
       try {   
         Class.forName(driver);   
       } catch (ClassNotFoundException e) {   
         System.out.println("ClassNotFoundException   ->" + e);   
       }   
       try {   
         Connection conn = DriverManager.getConnection(url, user, password);   


      PreparedStatement ps = conn   
               .prepareStatement("update webdb_prod_song set song_picture=? where song_id=12313");   
         //  ps.setString(1, "123.jpg");   
           InputStream input = new FileInputStream("D:\\123.jpg");  
           
           ps.setBinaryStream(1, input, input.available());   
           ps.executeUpdate();
           ps.close();
           
        // 取出图片
           ps = conn   
               .prepareStatement("select   *   from     webdb_prod_song   where   song_id   =   ?");   
           ps.setString(1, "12313");   
           ResultSet rs = ps.executeQuery();   
           rs.next();   
           InputStream in = rs.getBinaryStream("song_picture"); 
           System.out.println(in.available());   
           FileOutputStream out = new FileOutputStream("D:\\12.jpg");   
           byte[] b = new byte[1024];   
           int len = 0;   
           while ((len = in.read(b)) != -1) {   
             out.write(b, 0, len);   
             out.flush();   
           }   
           out.close();   
           in.close();   
           rs.close();   
           ps.close();   
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
        }

    ---------------------------------------------------------------------------------------------------------------------------------
    copyright:http://www.cnblogs.com/anee/
  • 相关阅读:
    jsp引擎是什么
    asp.net MVC遇到的问题
    java中设置http响应头控制浏览器禁止缓存当前文档内容
    java中文件下载的思路(参考:孤傲苍狼)
    301与302页面重定向
    怎么在后台修改前台html页面的key、title、description
    进程外session(session保存在sqlserver)
    Cookie的读写
    HTTP 方法:GET 对比 POST 转自w3school
    位bit,字节byte,K,M,G(转)
  • 原文地址:https://www.cnblogs.com/anee/p/2675888.html
Copyright © 2011-2022 走看看