DBUtil


Apache が提供してくれてる便利なライブラリ dbutils のサンプル
それぞれのメソッドの意味は面倒だから省略。。
動かせば分るかな。



/**
* execute select
*
* @param con
* DBConnection
* @param sql
* SQL statement
* @param params
* where condition data
* @param clazz
* class
* @return result set list(not found return empty list)
* @throws SQLException
* SQL Exception
*/
public List select(final Connection con, final String sql,
final Object[] params, final Class clazz) throws SQLException {

QueryRunner qr = new QueryRunner();
ResultSetHandler rsh = new BeanListHandler(clazz);
return (List) qr.query(con, sql, params, rsh);
}

/**
* return 1st record result
*
* @param con
* DBConnection
* @param sql
* SQL
* @param params
* where condition data
* @return 1st record result
* @throws SQLException
* SQL Exception
*/
public long selectcnt(final Connection con, final String sql,
final Object[] params) throws SQLException {

QueryRunner qr = new QueryRunner();
ResultSetHandler rsh = new ScalarHandler(1);
return ((Long) qr.query(con, sql, params, rsh)).longValue();
}

/**
* get sequence nextval
*
* @param con
* DBConnection
* @param sql
* SQL
* @param params
* where condition data
* @return 1st record result
* @throws SQLException
* SQL Exception
*/
public long sequence(final Connection con, final String sql)
throws SQLException {

debuglog(sql, null);

QueryRunner qr = new QueryRunner();
ResultSetHandler rsh = new ScalarHandler(1);
return ((Long) qr.query(con, sql, new Object[] {}, rsh)).longValue();
}

/**
* delete insert update
*
* @param con
* DBConnection
* @param sql
* SQL
* @param params
* update data
* @return update count
* @throws SQLException
* SQL Exception
*/
private int executeDIU(final Connection con, final String sql,
final Object[] params) throws SQLException {

QueryRunner qr = new QueryRunner();
return qr.update(con, sql, params);
}

0 コメント: