#5
public void addClob() {
try {
//取得舊表得ID列表
// ResultSet rsTmp=DoSQL.Select("zd2_T_NEWS2","COL007",false);
//String [] idList = SQLAI.getPrimaryKey("fj_xt_zd2_T_news");
String[] idList = this.getContent();
String titleList[] = this.getTitle();
String newsID [] = this.getNewsID();
//--開始讀取大文本到數據庫--
int countReplain = 0; //統計導入的大文件個數
for (int i = 0; i
//--初始化數據庫連接--
java.sql.Connection con = Dbconn.pubGetCon();
con.setAutoCommit(false);
java.sql.Statement st = con.createStatement();
PreparedStatement psSel = null;
PreparedStatement ps = null;
ResultSet rs = null;
//構造文件名
String path = "c:\\abc\\" + idList[i].trim() + ".txt";
//--取得數據庫重中的大文本字段--
int id = i;
rs = null;
/* 查詢此CLOB對象並鎖定 */
String sql =
"insert into t_news_message2 (id,title,content) values (?,?,empty_clob())";
String selectSql =
"SELECT content FROM t_news_message2 WHERE ID="
+ id
+ " FOR UPDATE";
String sqlUpdate =
"update "
+ "t_news_message"
+ " SET content=? where id = "
+ id;
psSel = con.prepareStatement(sql);
psSel.setInt(1, id);
psSel.setString(2, titleList[i]);
psSel.executeUpdate();
rs = st.executeQuery(selectSql);
rs.next();
CLOB clob = (CLOB) rs.getClob("content");
//----
BufferedReader in = null;
//BufferedWriter out=null;
try {
//-- 向CLOB對象中寫入數據 --
//out = new BufferedWriter(clob.getCharacterOutputStream());
in = new BufferedReader(new FileReader(path));
if (in != null) {
String s;
String sAll = new String("");
if(this.getCountNewsID(newsID[i])>0)
{
sAll = sAll + this.getPicUrl(newsID[i]) ;
}
while ((s = in.readLine()) != null) {
sAll = sAll + " "+ s + "
";
}
System.out.println(sAll);
clob.putString(1L, sAll);
ps = con.prepareStatement(sqlUpdate);
ps.setClob(1, clob);
ps.executeUpdate();
// int writeClob;
// while ( (writeClob = in.read()) != -1) {
// out.write(writeClob);
// }
countReplain++; //統計目前已經導入的大文本個數
con.commit();
con.setAutoCommit(true);
}
} catch (Exception e1) {
System.out.println("Lose file:" + path);
if (in != null)
in.close();
//if(out!=null)out.close();
//----
}
if (rs != null)
rs.close();
if (st != null)
st.close();
if (ps != null)
ps.close();
if (psSel != null)
psSel.close();
if (con != null)
con.close();
//----
}
System.out.println("共有文件" + countReplain);
} catch (Exception e) {
}
}