1. 程式人生 > >ORACLE讀寫CLOB(oracle儲存XML檔案)

ORACLE讀寫CLOB(oracle儲存XML檔案)

把一個XML檔案寫入到ORACLE中 :

SessionFactory factory=this.getHibernateTemplate().getSessionFactory();
Session session = factory.openSession();
Transaction tran=session.beginTransaction();
Connection conn=session.connection();
Hibernate.createClob(" "); 
long tt=System.currentTimeMillis();
//SEQ_SAVA_LOG.nextval這是一個自增序列
String sql="insert into biz_save_log (resource_id,time_t,massage_clob) values (SEQ_SAVA_LOG.nextval,to_date('"+time+"','yyyy-mm-dd hh24:mi:ss'),?)";
PreparedStatement pstmt= conn.prepareStatement(sql);
//傳入的sb就是該XML檔案讀成的一個String字串
Reader clobReader = new StringReader(sb);
pstmt.setCharacterStream(1, clobReader, sb.length());
int num = pstmt.executeUpdate();
//根據返回值判斷是否執行成功
if (num > 0) {
    System.out.println("ok");
} else {
    System.out.println("NO");
}

ORACLE讀取CLOB欄位:

List<String> list=new ArrayList<String>();
try {
	SessionFactory factory=this.getHibernateTemplate().getSessionFactory();
	Session	session = factory.openSession();
	Transaction tran=session.beginTransaction();
	Connection conn=session.connection();
	String sql="select massage_clob from biz_save_log where time_t between" +
		" to_date('"+stime+"','yyyy-mm-dd hh24:mi:ss') and " +
		"to_date('"+etime+"','yyyy-mm-dd hh24:mi:ss')";
	PreparedStatement stmt = conn.prepareStatement(sql);
	ResultSet	rs= stmt.executeQuery();
	while (rs.next()) {
		String res=rs.getString(1);
		list.add(res);
	}

rs.getString(1),這個方法裡面的引數,取決於你要執行的sql返回欄位的位置,不能隨便寫。