Database: Oracle 10G XE
Data source driver: Jdbc12.jar
FileName: Writeblob
Database to create a clobtest table with two fields, name (VARCHAR2), content (BLOB).
1package Dbdriver;
2
3/** *//**
4 * 2008-09-28
5 * @author Duduli
6 * email:lxyzj2000@gmail.com
7 * *
8import java.sql.*;
9import java.io.*;
10import oracle.sql.*;
11
12public class Writeblob {
13
public static void Main (string[] args) {
try {
String fName2 = "";
String fileName = "E:\\jianxin.bmp";
18//e disk downstream a jianxin.bmp image file
file F = new file (fileName);
String fName = F.getname ();
int i = Fname.lastindexof ('. ');
if (i > 0 && i < fname.length ()-1) {
fName2 = fname.substring (0,i);
24}
System.out.println (fName2);
26//get filename, go out suffix file name.
Drivermanager.registerdriver (New Oracle.jdbc.driver.OracleDriver ());
Connection conn = drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:xe", "System", "Duduli");
Conn.setautocommit (FALSE);
blob blob = null;
PreparedStatement pstmt = conn.preparestatement ("INSERT INTO BLOBTest (name,content) VALUES (?, Empty_blob ())") ;
pstmt.setstring (1, fName2);
pstmt.executeupdate ();
Pstmt.close ();
pstmt = conn.preparestatement ("Select content from BLOBTest where name=?") For update ");
pstmt.setstring (1, fName2);
Panax Notoginseng ResultSet rset = Pstmt.executequery ();
(Rset.next ()) {
(BLOB) Rset.getblob (1);
40}
41
fileinputstream fin = new FileInputStream (f);
System.out.println ("File size =" + fin.available ());
pstmt = conn.preparestatement ("Update blobtest set content=? where name=? ");
OutputStream out = Blob.getbinaryoutputstream ();
byte[] data = new byte[(int) fin.available ()];
fin.read (data);
out.write (data);
Fin.close ();
Out.close ();
Wuyi Pstmt.setblob (1, BLOB);
pstmt.setstring (2, fName2);
pstmt.executeupdate ();
Pstmt.close ();
Conn.commit ();
Conn.close ();
catch (SQLException e) {
System.err.println (E.getmessage ());
E.printstacktrace ();
catch (IOException e) {
System.err.println (E.getmessage ());
62}
63}
64}