Insert a BLOB column from a file.
Suppose table is defined as: CREATE TABLE TESTTAB(A INT, B BLOB);
Oracle JDBC insert BLOB data
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.rowset.serial.SerialBlob;
public class ORATest {
private Connection conn = null;
private int blobid = 1;
private void insertBlob(String filename) {
try {
PreparedStatement pstmt = conn.prepareStatement("insert into testblob values(?, ?)");
File file = new File(filename);
FileInputStream fstream = new FileInputStream(file);
System.out.println("length=" + file.length());
pstmt.setInt(1, 1);
pstmt.setBinaryStream(2, fstream, (int)file.length());
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(String file) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@<HOST>:<PORT>:<SID>", "<UID>", "<PWD>");
insertBlob(file);
} catch (ClassNotFoundException e) {
System.out.println("Please include classpath where your Oracle Driver is located");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
ORATest oratest = new ORATest();
oratest.run(argv[0]);
}
}
DB2 insert BLOB column
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.rowset.serial.SerialBlob;
public class DB2Test {
private Connection conn = null;
private int blobid = 1;
private void insertBlob(String filename) {
try {
PreparedStatement pstmt = conn.prepareStatement("insert into testblob values(?, ?)");
File file = new File(filename);
FileInputStream fstream = new FileInputStream(file);
byte b[] = new byte[(int)file.length()];
fstream.read(b);
java.sql.Blob blob = new SerialBlob(b);
System.out.println("length=" + file.length());
pstmt.setInt(1, 1);
pstmt.setBlob(2, blob);
pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(String file) {
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn = DriverManager.getConnection("jdbc:db2:<DSN>","UID","<PWD>");
insertBlob(file);
} catch (ClassNotFoundException e) {
System.out.println("Please include classpath where your DB2 Driver is located");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] argv) {
DB2Test db2test = new DB2Test();
db2test.run(argv[0]);
}
}