Store BLOBs data into database : Blob Binary Data JDBC « Database SQL JDBC « Java






Store BLOBs data into database

  

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

   
    
  








Related examples in the same category

1.Read BLOBs data from database
2.Insert picture to MySQL
3.Demo Display Binary Data From Database
4.Materialize binary data onto client
5.Blob: JDBC deals with Binary Data
6.Inserting Image in Database Table
7.Blob and JDBC: Image
8.Blob: Image 2
9.Blob: image 3
10.Insert an Image
11.Retrieve an Image
12.Store and retrieve an object from a table
13.Read CLOBs data from database
14.Getting BLOB Data from a Database Table: how to retrieves bytes from a BLOB.
15.Store CLOBs data into database?
16.Getting and Inserting Binary Data into an Database Table