Appche Commons DbUtils


dbutils jarダウンロードアドレスhttp://labs.renren.com/apache-mirror//commons/dbutils/binaries/commons-dbutils-1.4-bin.zip
データベース接続の設定(データベースドライバの追加)
		String url = "xxx";
		String driver = "oracle.jdbc.driver.OracleDriver";
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url, "user","password");
検索結果を対応するbeanに変換します。
		BeanHandler<Score> bh = new BeanHandler<Score>(Score.class);
		QueryRunner run = new QueryRunner();
		try {
			Score score =run.query(con, "select ss.* from score_score ss where ss.id=?",bh,299);
			//     query             , sql         
			System.out.println(score.getScore());
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				DbUtils.close(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
検索結果を対応するbean listに変換します。
		BeanListHandler<Score> blh = new BeanListHandler<Score>(Score.class);
		QueryRunner run = new QueryRunner();
		try {
			List<Score> list =run.query(con, "select * from score_score",blh);
			for(Score s:list){
				System.out.println(s.getScore());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				DbUtils.close(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
上のbeanとbean list変換は表示される必要がないPreparedSttementとResultSetを呼び出します。QueryRunnerツールクラスを直接通過します。
--------------------
ResultSetをbeanに変換します。
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select ss.* from score_score ss where ss.id=299";
		try {
			pstat =con.prepareStatement(sql);
			rs = pstat.executeQuery();
			while(rs.next()){
				RowProcessor rp = new BasicRowProcessor();
				Score score = rp.toBean(rs, Score.class);
				System.out.println(score.getScore());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(con, pstat, rs);
		}
Result Setをbean listに変換します。
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from score_score";
		try {
			pstat =con.prepareStatement(sql);
			rs = pstat.executeQuery();
			while(rs.next()){
				RowProcessor rp = new BasicRowProcessor();
				List<Score> list= rp.toBeanList(rs,Score.class);
				for(Score s:list){
					System.out.println(s.getScore());
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(con, pstat, rs);
		}
*クラス呼び出し変換プロセス
*QueryRunner類のrun方法
*PreparedSttementでResultSetを取得し、取得したResult Setを変換します。
*BenHandler、BenListHandlerのデフォルトRowProcess orは
*ARrayHandler.ROW_PROCESSOR=BaicRowProcessor
*BaicRowProcessorのデフォルトのcovertはBenProcessorです。
*だから最終的に変換するのは呼び出しのBeanProcessorの中のtoBean方法とtoBenList方法です。

*だから制御変換はBeanProcessorをカスタマイズできます。
*BeanProcessor変換プロセス:
*A.まず伝達されたbeanのクラスファイルから全ての属性を抽出します。
*つまりPropertyDescriptor[]propsです。
*PropertyDescriptor対応記憶属性名と属性タイプ
*B.ResultSetのrow columsとPropertyDescriptor[]の対応関係を確認する。どの属性がどのフィールドに対応しますか?
*   デフォルトのマッチング規則は、列名と属性名が等しいです。大文字と小文字は無視されます。
*   対応方法:mapColumnstoProperties(ResultSet Meta Data rsmd、ProptyDescriptor[]props)
*   この方法を書くことで特定の属性と特定の列に対応するように指定できます。
*C.class、属性、対応するフィールドの値があれば、対象の割当値を作成できます。