部落客廣告聯播

2008年2月18日 星期一

JDBC insert CLOB欄位

1. 若為String可轉為StringReader物件...

String s="String content字串";
StringReader sr=new StringReader(s);


3. prepare statement

Connection con = ds.getConnection();
String sql="insert testTable(colClob) values (?) ";
PreparedStatement pstmt=con.prepareStatement(sql);

2. 將該Reader寫入暫存檔

tmp=File.createTempFile("tmpInsertClob_", null);
BufferedWriter writer=new BufferedWriter(new FileWriter(tmp));
BufferedReader reader=new BufferedReader((Reader)insertVals.get(i));

//StringWriter用來計算字數用
StringWriter strW=new StringWriter();
int contentLength=0;
int data=0;
while((data=reader.read())!=-1)
{
writer.write(data);
strW.write(data);
}
strW.close();
contentLength=strW.toString().length();
strW=null;
writer.close();
reader.close();
BufferedReader reader2=new BufferedReader(new FileReader(tmp));

pstmt.setCharacterStream(i+1,reader2,contentLength);



3. 執行

pstmt.executeUpdate();


4.關閉來源串流

reader2.close();

沒有留言: