Insert an Image : Blob Clob « Database « Java Tutorial






import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@//server.local:1521/prod", "scott", "tiger");
    conn.setAutoCommit(false);
    PreparedStatement ps = conn
        .prepareStatement("insert into images values (?,?)");
    ps.setString(1, file.getName());
    ps.setBinaryStream(2, fis, (int) file.length());
    ps.executeUpdate();
    ps.close();
    fis.close();

  }
}








20.22.Blob Clob
20.22.1.Getting and Inserting Binary Data into an Database Table
20.22.2.Store BLOBs data into database
20.22.3.Read BLOBs data from database
20.22.4.Blob and Clob data type
20.22.5.Getting BLOB Data from a Database Table: how to retrieves bytes from a BLOB.
20.22.6.Store CLOBs data into database?
20.22.7.Read CLOBs data from database
20.22.8.Insert an Image
20.22.9.Retrieve an Image