java CachedRowSet Resultset

Posted in :

今天學習到一個新的 Class 叫CachedRowSet,可以用來解決 connection 斷掉時 ResultSet 就無法使用的問題。

取得資料庫某一Table的某些Row,一般是使用java.sql.ResultSet,但是一旦connection斷掉後,
ResultSet也就消失了,如果一直保持dataBase的連線,那又是一件很佔Resource且不實際的做
法。


CashedRowSet也可以使用現有的連線,而不必一定每次於execute()時檢取得連線與取消連線
重點在於execute()可傳connection進去。而建立連線的方式也可以取得Application Server/Web
Server 的Connection Pool,方式如下:

InitialContext ctx = new InitialContext();
javax.sql.DataSource ds =   (javax.sql.DataSource)ctx.lookup(“DataSource名稱”);
java.sql.Connection con = ds.getConnection();

而DataSource名稱 是取得DataSource的JNDI名稱,以WebLogic為例,當使用

http://localhost:7001/console後,在左邊mydomain/Services/JDBC/Data Sources或 TX Data Sources
中列出的Entry中的JNDI Name,便是該DataSource名稱,JSP中請使用Data Sources中的Entry
而EJB用TX Data Sources的Entry

 

String url = “jdbc:informix-sqli://192.168.0.51:1561/” + dbname +
“:INFORMIXSERVER=eis;user=eisadm;password=win98;” +
“CLIENT_LOCALE=zh_tw.Big5;DB_LOCALE=zh_TW.big5”;
String strSQL = “Select Name, chart_no,emerg_no,birthdate,sex,arrival_time,” +
“departure_time, bed_no,recent_level From Er_Pt Where ” +
“Departure_Type <> ‘Z’ And Paid_Status <> ‘C’ And ” +
“(Departure_Time is Null Or Departure_Time + ” +
” Interval(18) Hour to Hour > CURRENT ) And Exam_Area = ‘C'”;

java.sql.Connection conEIS = null;
sun.jdbc.rowset.CachedRowSet crs;
try {
Class.forName(“com.informix.jdbc.IfxDriver”);
conEIS = DriverManager.getConnection(url); //建立連線
crs = new CachedRowSet();
crs.setCommand(strSQL);
crs.execute(conEIS); //利用現有的連線做事
conEIS.close(); //需要時可以關閉它
conEIS = null;
while (crs.next()) {
System.out.println(crs.getString(“name”));
}

}catch (ClassNotFoundException ce) {
System.out.println(“Class Not Found!! for com.informix.jdbc.IfxDriver”);
}catch (SQLException se) {
System.out.println(se.getMessage());
}finally {
try {
if (conEIS != null)
conEIS.close();
}catch (Exception ee){
System.out.println(ee.getMessage() );
}
}


如果你厭倦了舊的JDBC Connection的寫法,不彷試試看新的RowSet,今日自己寫的code如下。

package test;

import java.sql.*;

import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.JoinRowSet;
import javax.sql.rowset.RowSetProvider;

import dbcon.DBConnection;

public class myRowSet {
	DBConnection db=null;
	JdbcRowSet jrs=null; //Jdbcrowset就是ResultSet子介面,只是
	CachedRowSet crs=null; //CacheRowSet是先把data讀到記憶體裡,操作資料的時候不再連線database,但記憶體的耗比較多
	CachedRowSet crs2=null;
	JoinRowSet jjrs=null; //joinrowset就是用一個API實現sql join的功能,個人覺得挺新鮮的
	
	public void jsqlJoin(){
		try {
			crs=RowSetProvider.newFactory().createCachedRowSet(); //產生CachedRowSet實例
			crs.setUsername(db.getUser()); //很像設定connection參數,把URL, User, PW設定
			crs.setPassword(db.getDbpassword());
			crs.setUrl(db.getDburl());
			crs.setCommand("select no, fdino from fpr_fdi"); //類似statement,
			crs.execute();//執行查詢
			
			crs2=RowSetProvider.newFactory().createCachedRowSet();//產生CachedRowSet實例
			crs2.setUsername(db.getUser());
			crs2.setPassword(db.getDbpassword());
			crs2.setUrl(db.getDburl());
			crs2.setCommand("select 編號, 收文日期 from seo_fprtable");
			crs2.execute();//執行查詢
			
			jjrs=RowSetProvider.newFactory().createJoinRowSet();//產生JoinRowSet實例
			jjrs.addRowSet(crs, "no");//add要join的row後設定共同的key
			jjrs.addRowSet(crs2, "編號");//add要join的row後設定共同的key
			//jjrs.setJoinType(para); //可以設定join type,INNER Join之類的,我這個例子one to one沒啥好設定的
			
			while(jjrs.next()){
				System.out.println(jjrs.getString(1)+", "+jjrs.getString(2)+", "+jjrs.getString(3));
			}//FPR-10-0297, LT1-31113-0214, 2010/05/10
			
			ResultSetMetaData meta=jjrs.getMetaData(); 
			int cols=meta.getColumnCount(); //用getmeta測試一下總col數有多少
			System.out.println(cols); //3
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	
	public void getRowSet(){
		try {
//			db=new DBConnection();
//		    con=db.getConnection();
//			jrs=new JdbcRowSetImpl(con); //JDK7前舊的產生實例的方法,功能一樣
			
			jrs=RowSetProvider.newFactory().createJdbcRowSet(); //產生JdbcRowSet實例
			jrs.setUsername(db.getUser());//很像設定connection參數,把URL, User, PW設定
			jrs.setPassword(db.getDbpassword());
			jrs.setUrl(db.getDburl());
			jrs.setCommand("select no, fdino from fpr_fdi");
			jrs.execute();//執行查詢
			
			while(jrs.next()){
				String fpr=jrs.getString("no");
				String fdi=jrs.getString("fdino");
				System.out.println(fpr+", "+fdi);//FPR-10-0297, LT1-31113-0214
			}
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *