In the process of writing a Java program, how to read a field of type BLOB for an Oracle database table?
Here's a workaround when I write a program. The core statement. (Transfer up to make a change, format do not learn, develop good habits)
For details, please refer to:
Read Sequence id:http://blog.csdn.net/yzsind/article/details/6918506
Blob Related: http://jslfl.iteye.com/blog/1771949
Http://www.linuxidc.com/Linux/2011-08/40218.htm
http://xwwccsucn.blog.163.com/blog/static/979145792011925234392/
1. Read the Blob field
Import Oracle.sql.BLOB;
ResultSet rs = ps.executequery (SQL);
Blob blob = (BLOB) Rs.getblob ("content");
String s = blobtostring (BLOB);
2. Blobtostring () method
Private String blobtostring (Blob blob) throws SQLException, Unsupportedencodingexception {
Long bloblength; Blob field length
int i = 1; Loop variable
byte[] bytes; Blob temporary storage byte array
String newstr = ""; return string
byte[] msgcontent = Blob.getbytes (); Blob conversion to byte array
Bloblength = Blob.length (); Get blob length
if (msgcontent = = NULL | | Bloblength = = 0)//If empty, return null value
{
Return "";
} else {
while (i <= bloblength)//loop processing string conversion, each 1024;oracle string limit 4k max
{
bytes = Blob.getbytes (i, 1024);
i = i + 1024;
NEWSTR = newstr + new String (bytes);
}
}
return newstr;
}
3. Update BLOB field
public void InsertData (string task_name, String tast_target) {
int num = 0;
String hql = "INSERT into table values (Sequence_id.nextval,?,empty_blob ()) returning IDs into?";
try {
Connection conn = Globaldata.getconnection ();
Oraclepreparedstatement Ops = (oraclepreparedstatement) conn.preparestatement (HQL);
Ops.setstring (1, task_name);
Ops.registerreturnparameter (2, oracletypes.number);
num = Ops.executeupdate ();
ResultSet RSet = Ops.getreturnresultset ();//Rest is not null
Long id = 0;
while (Rset.next ()) {
id = rset.getlong (1);
System.out.println ("Insert returnning:" + ID);
}
Update BLOB fields
Statement BST = Conn.createstatement ();
ResultSet bset = Bst.executequery ("Select tast_target from table where id=" + ID + "for UPDATE");
Blob blob = null;
while (Bset.next ()) {
blob = ((Oracleresultset) bset). GetBLOB (1);
}
Final Java.io.BufferedOutputStream out = new Java.io.BufferedOutputStream (Blob.getbinaryoutputstream ());//output stream
Bytearrayinputstream streamtemp = null;//input stream
try {
Streamtemp = new Bytearrayinputstream (tast_target.getbytes ("iso-8859-1"));
} catch (Unsupportedencodingexception ex) {
Logger.getlogger (Oraclegeneldaoimpl.class
. GetName ()). log (Level.severe, NULL, ex);
}
byte[] buffer = new byte[1024];//buffers
int length =-1;
try {
while (length = streamtemp.read (buffer))! =-1) {
Out.write (buffer, 0, length);
}
Streamtemp.close ();
Out.close ();
} catch (IOException ex) {
Logger.getlogger (Oraclegeneldaoimpl.class
. GetName ()). log (Level.severe, NULL, ex);
}
Conn.commit ();
Ops.close ();
Bst.close ();
} catch (SQLException ex) {
Logger.getlogger (Oraclegeneldaoimpl.class
. GetName ()). log (Level.severe, NULL, ex);
}
}
This article is from the "I Will work" blog, please be sure to keep this source http://meijia.blog.51cto.com/8684191/1563867
Java read/Update Oracle database BLOB fields