Javascript required
Skip to content Skip to sidebar Skip to footer

How to Insert Blob Data in Oracle Using Sql

Technical Questions and Answers

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to insert an image in to Oracle database using Java program?


To hold an image in Oracle database generally blob type is used. Therefore, make sure that you have a table created with a blob datatype as:

Name Null? Type ----------------------------------------- -------- ---------------------------- NAME VARCHAR2(255) IMAGE BLOB

To insert an image in to Oracle database, follow the steps given below:

Step 1: Connect to the database

You can connect to a database using the getConnection() method of the DriverManager class

Connect to the Oracle database by passing the Oracle URL which is jdbc:oracle:thin:@localhost:1521/xe (for express edition), username and password as parameters to the getConnection() method.

String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe"; Connection con = DriverManager.getConnection(oracleUrl, "user_name", "password");

Step 2: Create a Prepared statement

Create a PreparedStatement object using the prepareStatement() method of the Connection interface. To this method pass the insert query (with place holders) as a parameter.

PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?, ?)");

Step 3: Set values to the place holders

Set the values to the place holders using the setter methods of the PreparedStatement interface. Chose the methods according to the datatype of the column. For Example if the column is of VARCHAR type use setString() method and if it is of INT type you can use setInt() method.

And if it is of Blob type you can set value to it using the setBinaryStream() or setBlob() methods. To these methods pass an integer variable representing the parameter index and an object of InputStream class as parameters.

pstmt.setString(1, "sample image"); //Inserting Blob type InputStream in = new FileInputStream("E:\\images\\cat.jpg"); pstmt.setBlob(2, in);

Step 4: Execute the statement

Execute the above created PreparedStatement object using the execute() method of the PreparedStatement interface.

Example

import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertImageToOracleDB {    public static void main(String args[]) throws Exception{       //Registering the Driver       DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ());       //Getting the connection       String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";       Connection con = DriverManager.getConnection(oracleUrl, "system", "password");       System.out.println("Connected to Oracle database.....");       PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyTable VALUES(?,?)");       pstmt.setString(1, "sample image");       //Inserting Blob type       InputStream in = new FileInputStream("E:\\images\\cat.jpg");       pstmt.setBlob(2, in);       //Executing the statement       pstmt.execute();       System.out.println("Record inserted");    } }

Output

Connected to Oracle database..... Record inserted.....

raja

Published on 22-Mar-2019 10:43:19

  • Related Questions & Answers
  • How to insert an image in to MySQL database using Java program?
  • How to optimize the INSERT statement using direct-path insert technique in Oracle?
  • How to Insert an Image in HTML Page?
  • How to insert Timestamp value in a database using JDBC program?
  • How to insert images in Database using JDBC?
  • How to insert and retrieve dates in Oracle ?
  • How to retrieve a record from an existing table in oracle database using JDBC API?
  • How to remove a record from an existing table in oracle database using JDBC API?
  • How to drop a table from Oracle database using JDBC API?
  • How to insert an image in a Tkinter canvas item?
  • How to create a Stored procedure in Oracle database using JDBC API?
  • How to view storage configuration of Oracle database?
  • How to limit Database Resources per Session in Oracle?
  • How to flip an image using Java OpenCV library?
  • How to rotate an image with OpenCV using Java?

How to Insert Blob Data in Oracle Using Sql

Source: https://www.tutorialspoint.com/how-to-insert-an-image-in-to-oracle-database-using-java-program