zoukankan      html  css  js  c++  java
  • blob

    import java.io.File;
    import java.io.FileInputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    
    public class Main {
      static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
      static String username = "username";
      static String password = "welcome";
    
      public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection(url, username, password);
        conn.setAutoCommit(false);
    
        String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, "java.gif");
        stmt.setString(2, "Java Official Logo");
    
        File image = new File("D:\a.gif");
        FileInputStream   fis = new FileInputStream(image);
        stmt.setBinaryStream(3, fis, (int) image.length());
        stmt.execute();
    
        conn.commit();
        fis.close();
        conn.close();
      }
    } 
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    public class Main {
      static String url = "jdbc:oracle:thin:@localhost:1521:javaDemo";
      static String username = "username";
      static String password = "welcome";
      public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection conn = DriverManager.getConnection(url, username, password);
    
        String sql = "SELECT name, description, image FROM pictures ";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet resultSet = stmt.executeQuery();
        while (resultSet.next()) {
          String name = resultSet.getString(1);
          String description = resultSet.getString(2);
          File image = new File("D:\java.gif");
          FileOutputStream fos = new FileOutputStream(image);
    
          byte[] buffer = new byte[1];
          InputStream is = resultSet.getBinaryStream(3);
          while (is.read(buffer) > 0) {
            fos.write(buffer);
          }
          fos.close();
        }
        conn.close();
      }
    }
  • 相关阅读:
    Notes相关开发Tips
    gridView滚动条相关问题
    MyBatis学习(一)简单入门程序
    springMVC入门
    zoj 3702 Gibonacci number 找规律
    邻接表表示
    poj 1269 直线相交情况
    poj 3304 Segments 线段与直线相交的判断
    poj 1654 多边形面积
    zoj 3696 Alien's Organ 概率,泊松分布
  • 原文地址:https://www.cnblogs.com/yasepix/p/6423080.html
Copyright © 2011-2022 走看看