建表文件
create
table
A_F2DB
(
TID VARCHAR2 ( 23 ) not null ,
A_FILEC CLOB,
A_FILEB BLOB
)
(
TID VARCHAR2 ( 23 ) not null ,
A_FILEC CLOB,
A_FILEB BLOB
)
WriteBlob.java
package
com.fsm.db;
import java.sql. * ;
import java.util.ArrayList;
import java.io. * ;
import oracle.sql. * ;
public class WriteBlob ... {
-----------icepoint------------
/** *//**
* @param strTID
* @param tablename
* @param filenames
* @param blobcolumnname
* @param KeyColumn
* @return
*/
-----------icepoint------------
public boolean writeblobtodb(String strTID, String tablename,
String filenames, String blobcolumnname, String KeyColumn) ...{
boolean isok = false;
try ...{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "gj", "gj");
conn.setAutoCommit(false);
ReadPath readPath = new ReadPath();
ArrayList list = readPath.okpath();
File f = new File(filenames);
FileInputStream fin = new FileInputStream(f);
BLOB blob = null;
PreparedStatement pstmt = conn.prepareStatement("insert into "
+ tablename + " (" + KeyColumn + "," + blobcolumnname
+ ") values(?,empty_blob())");
pstmt.setString(1, strTID);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select " + blobcolumnname + " from "
+ tablename + " where " + KeyColumn + "=? for update");
pstmt.setString(1, strTID);
ResultSet rset = pstmt.executeQuery();
if (rset.next())
blob = (BLOB) rset.getBlob(1);
pstmt = conn.prepareStatement("update " + tablename + " set "
+ blobcolumnname + "=? where " + KeyColumn + "='" + strTID
+ "'");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[(int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1, blob);
pstmt.executeUpdate();
pstmt.close();
System.out.println("插入成功!!!");
isok = true;
conn.commit();
conn.close();
} catch (SQLException e) ...{
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) ...{
System.err.println(e.getMessage());
}
return isok;
}
public static void main(String[] args) ...{
WriteBlob wb=new WriteBlob();
String strTID = "2007";
String tablename = "A_F2DB";
String filenames = "E:/chunye.jpg";
String blobcolumnname = "A_FILEB";
String KeyColumn = "TID";
if(wb.writeblobtodb(strTID,tablename,filenames,blobcolumnname,KeyColumn))...{
System.out.println("OK");
}else...{
System.out.println("FAILD");
}
}
}
import java.sql. * ;
import java.util.ArrayList;
import java.io. * ;
import oracle.sql. * ;
public class WriteBlob ... {
-----------icepoint------------
/** *//**
* @param strTID
* @param tablename
* @param filenames
* @param blobcolumnname
* @param KeyColumn
* @return
*/
-----------icepoint------------
public boolean writeblobtodb(String strTID, String tablename,
String filenames, String blobcolumnname, String KeyColumn) ...{
boolean isok = false;
try ...{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl", "gj", "gj");
conn.setAutoCommit(false);
ReadPath readPath = new ReadPath();
ArrayList list = readPath.okpath();
File f = new File(filenames);
FileInputStream fin = new FileInputStream(f);
BLOB blob = null;
PreparedStatement pstmt = conn.prepareStatement("insert into "
+ tablename + " (" + KeyColumn + "," + blobcolumnname
+ ") values(?,empty_blob())");
pstmt.setString(1, strTID);
pstmt.executeUpdate();
pstmt.close();
pstmt = conn.prepareStatement("select " + blobcolumnname + " from "
+ tablename + " where " + KeyColumn + "=? for update");
pstmt.setString(1, strTID);
ResultSet rset = pstmt.executeQuery();
if (rset.next())
blob = (BLOB) rset.getBlob(1);
pstmt = conn.prepareStatement("update " + tablename + " set "
+ blobcolumnname + "=? where " + KeyColumn + "='" + strTID
+ "'");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[(int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1, blob);
pstmt.executeUpdate();
pstmt.close();
System.out.println("插入成功!!!");
isok = true;
conn.commit();
conn.close();
} catch (SQLException e) ...{
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) ...{
System.err.println(e.getMessage());
}
return isok;
}
public static void main(String[] args) ...{
WriteBlob wb=new WriteBlob();
String strTID = "2007";
String tablename = "A_F2DB";
String filenames = "E:/chunye.jpg";
String blobcolumnname = "A_FILEB";
String KeyColumn = "TID";
if(wb.writeblobtodb(strTID,tablename,filenames,blobcolumnname,KeyColumn))...{
System.out.println("OK");
}else...{
System.out.println("FAILD");
}
}
}