本站不再更新,歡迎光臨 java開發技術網
          隨筆-230  評論-230  文章-8  trackbacks-0


          package com.scitel.gdnumcommon.utils;

          import java.math.BigDecimal;
          import java.sql.Connection;
          import java.sql.PreparedStatement;
          import java.sql.ResultSet;
          import java.sql.ResultSetMetaData;
          import java.sql.Types;
          import java.util.ArrayList;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.List;
          import java.util.Map;

          import org.apache.commons.beanutils.BeanUtils;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import com.scitel.gdnumcommon.entity.Pagination;
          import com.scitel.gdnumcommon.entity.BaseVO;


          public class BaseDAO {
          ?private static final Log log = LogFactory.getLog(BaseDAO.class);

          ?/**
          ? * 保存數據,新建和修改都用這個
          ? *
          ? * @param con
          ? * @param SQL
          ? * @param params
          ? * @throws Exception
          ? * @author
          ? */
          ?public void save(Connection con, String SQL, List params)
          ???throws Exception {
          ??PreparedStatement ps = null;
          ??try {
          ???ps = con.prepareStatement(SQL);
          ???if (SQL == null) {
          ????throw new Exception();
          ???}

          ???if (params != null && params.size() > 0) {
          ????int count = 0;
          ????for (Iterator iterator = params.iterator(); iterator.hasNext(); count++) {
          ?????Object object = iterator.next();
          ?????setObjectValue(ps, count + 1, object);
          ????}
          ???}
          ???ps.executeUpdate();
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try{
          ????if(ps != null) {
          ?????ps.close();
          ????}
          ???}catch(Exception e){

          ???}
          ??}
          ?}

          ?/**
          ? * 保存數據,新建和修改都用這個,通過字段名稱匹配類型
          ? * @param con
          ? * @param SQL
          ? * @param voclass
          ? * @param paramMap
          ? * @throws Exception
          ? * @author
          ? */
          ?public void save(Connection con, String SQL, Class voclass, Map paramMap)
          ???throws Exception {
          ??PreparedStatement ps = null;
          ??try {
          ???ps = con.prepareStatement(SQL);
          ???if (SQL == null) {
          ????throw new Exception();
          ???}

          ???if (paramMap != null && paramMap.size() > 0) {
          ????int count = 0;
          ????for (Iterator iterator = paramMap.keySet().iterator(); iterator.hasNext(); count++) {

          ?????String key = (String)iterator.next();
          ?????Object object = paramMap.get(key);
          ?????setObjectValue(ps, voclass, count+1, key, object);
          ????}
          ???}
          ???ps.executeUpdate();
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try{
          ????if(ps != null) {
          ?????ps.close();
          ????}
          ???}catch(Exception e){

          ???}
          ??}
          ?}

          ?/**
          ? * 刪除數據
          ? *
          ? * @param con
          ? * @param SQL
          ? * @param params
          ? * @throws Exception
          ? * @author
          ? */
          ?public void remove(Connection con, String SQL, List params)
          ???throws Exception {
          ??PreparedStatement ps = null;
          ??try {
          ???ps = con.prepareStatement(SQL);
          ???if (SQL == null) {
          ????throw new Exception();
          ???}
          ???if (params != null && params.size() > 0) {
          ????int count = 0;
          ????for (Iterator iterator = params.iterator(); iterator.hasNext(); count++) {
          ?????Object object = iterator.next();
          ?????setObjectValue(ps, count + 1, object);
          ????}
          ???}
          ???ps.executeUpdate();
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try{
          ????if(ps != null) {
          ?????ps.close();
          ????}
          ???}catch(Exception e){

          ???}
          ??}
          ?}

          ?/**
          ? * 根據ID選擇數據
          ? *
          ? * @param con
          ? * @param SQL
          ? * @param id
          ? * @param voclass
          ? * @return
          ? * @throws Exception
          ? * @author
          ? */
          ?public BaseVO selectById(Connection con, String SQL, String id,
          ???Class voclass) throws Exception {
          ??Object po = null; // 承載值對象
          ??PreparedStatement ps = null;
          ??ResultSet rs = null;
          ??ResultSetMetaData rsm = null;
          ??try {
          ???ps = con.prepareStatement(SQL);
          ???if (SQL == null) {
          ????throw new Exception();
          ???}

          ???ps.setString(1, id);

          ???rs = ps.executeQuery();
          ???rsm = rs.getMetaData();
          ???if (rs.next()) {
          ????Map entity = new HashMap();
          ????for (int i = 1; i <= rsm.getColumnCount(); i++) {
          ?????String columnName = rsm.getColumnName(i).toLowerCase();
          ?????Object columnValue = getObjectValue(rs, voclass, i, columnName);
          ?????entity.put(columnName, columnValue);
          ????}
          ????if (voclass != null) {
          ?????po = voclass.newInstance();
          ?????BeanUtils.populate(po, entity);
          ????}
          ????
          ???}
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try {
          ????if (rs != null) {
          ?????rs.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ???try {
          ????if (ps != null) {
          ?????ps.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ??}
          ??return (BaseVO) po;
          ?}

          ?/**
          ? * 選擇記錄,不分頁
          ? * @param con
          ? * @param SQL
          ? * @param params
          ? * @param voclass
          ? * @return
          ? * @throws Exception
          ? * @author
          ? */
          ?public List select(Connection con, String SQL, List params, Class voclass)
          ???throws Exception {
          ??Object vo = null; // 承載值對象
          ??PreparedStatement ps = null;
          ??ResultSet rs = null;
          ??ResultSetMetaData rsm = null;
          ??List relist = null;
          ??try {
          ???ps = con.prepareStatement(SQL);
          ???if (SQL == null) {
          ????throw new Exception();
          ???}
          ???if (params != null && params.size() > 0) {
          ????int count = 0;
          ????for (Iterator iterator = params.iterator(); iterator.hasNext(); count++) {
          ?????Object object = iterator.next();
          ?????setObjectValue(ps, count + 1, object);
          ????}
          ???}
          ???rs = ps.executeQuery();
          ???rsm = rs.getMetaData();
          ???relist = new ArrayList();
          ???while (rs.next()) {
          ????Map entity = new HashMap();
          ????for (int i = 1; i <= rsm.getColumnCount(); i++) {
          ?????String columnName = rsm.getColumnName(i).toLowerCase();
          ?????Object columnValue = getObjectValue(rs, voclass, i, columnName);
          ?????entity.put(columnName, columnValue);
          ?????
          ????}
          ????if (voclass != null) {
          ?????vo = voclass.newInstance();
          ?????BeanUtils.populate(vo, entity);
          ?????relist.add(vo);
          ????} else {
          ?????relist.add(entity);
          ????}
          ???}
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try {
          ????if (rs != null) {
          ?????rs.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ???try {
          ????if (ps != null) {
          ?????ps.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ???
          ??}
          ??return relist;
          ?}

          ?/**
          ? * 分頁查詢
          ? *
          ? * @param con
          ? * @param SQL
          ? * @param params
          ? * @param voclass
          ? * @param pagination
          ? * @return
          ? * @throws Exception
          ? * @author
          ? */
          ?public List selectPagination(Connection con, String SQL, List params,
          ???Class voclass, Pagination pagination) throws Exception {
          ??if (SQL == null) {
          ???throw new NullPointerException("SQL不能為空!");
          ??}
          ??if (pagination == null) {
          ???throw new NullPointerException("分頁類不能為空!");
          ??}

          ??// TODO Auto-generated method stub
          ??Object vo = null; // 承載值對象
          ??PreparedStatement ps = null;
          ??ResultSet rs = null;
          ??ResultSetMetaData rsm = null;
          ??List relist = null;
          ??try {

          ???ps = con.prepareStatement("select count(1) as count_ from ( " + SQL + " )");

          ???if (params != null && params.size() > 0) {
          ????int count = 0;
          ????for (Iterator iterator = params.iterator(); iterator.hasNext(); count++) {
          ?????Object object = iterator.next();
          ?????setObjectValue(ps, count + 1, object);
          ????}
          ???}

          ???rs = ps.executeQuery();
          ???if (rs.next()) {
          ????pagination.setTotalCount(rs.getInt(1));
          ????
          ???}
          ???

          ???if (pagination.getTotalCount() > 0) {
          ????/* 組成分頁內容 */
          ????StringBuffer pagingSelect = new StringBuffer(100);
          ????pagingSelect
          ??????.append("select * from ( select row_.*, rownum rownum_ from ( ");
          ????pagingSelect.append(SQL);
          ????pagingSelect
          ??????.append(" ) row_ where rownum <= ?) where rownum_ > ?");

          ????ps = con.prepareStatement(pagingSelect.toString());
          ????int count = 0;
          ????if (params != null && params.size() > 0) {
          ?????for (Iterator iterator = params.iterator(); iterator.hasNext(); count++) {
          ??????Object object = iterator.next();
          ??????setObjectValue(ps, count + 1, object);
          ?????}
          ????}
          ????
          ????ps.setInt(count + 1, pagination.getPage()
          ??????* pagination.getCount());
          ????ps.setInt(count + 2, (pagination.getPage() - 1)
          ??????* pagination.getCount());

          ????log.info("pagination.getPage():" + pagination.getPage());
          ????log.info("pagination.getCount():" + pagination.getCount());
          ????rs = ps.executeQuery();
          ????rsm = rs.getMetaData();
          ????relist = new ArrayList();
          ????while (rs.next()) {
          ?????Map entity = new HashMap();
          ?????for (int i = 1; i <= rsm.getColumnCount(); i++) {
          ??????String columnName = rsm.getColumnName(i).toLowerCase();
          ??????Object columnValue = getObjectValue(rs, voclass, i, columnName);
          ??????entity.put(columnName, columnValue);
          ?????}
          ?????if (voclass != null) {
          ??????vo = voclass.newInstance();
          ??????BeanUtils.populate(vo, entity);
          ??????relist.add(vo);
          ?????} else {
          ??????relist.add(entity);
          ?????}
          ????}
          ???}
          ??} catch (Exception e) {
          ???log.error(e);
          ???throw e;
          ??} finally {
          ???try {
          ????if (rs != null) {
          ?????rs.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ???try {
          ????if (ps != null) {
          ?????ps.close();
          ????}
          ???} catch (Exception e) {

          ???}

          ??}
          ??return relist;
          ?}

          ?/**
          ? * 獲得SequenceValue
          ? * @param sequenceName
          ? * @return
          ? * @throws Exception
          ? * @author
          ? */
          ?public Long getSequenceValue(Connection con, String sequenceName)throws Exception {
          ??PreparedStatement ps = null;
          ??ResultSet rs = null;
          ??Long sequenceValue = null;
          ??try{
          ???ps = con.prepareStatement("select " + sequenceName + ".nextval from dual");
          ???rs = ps.executeQuery();
          ???if(rs.next()) {
          ????sequenceValue = new Long(rs.getLong(1));
          ???}
          ??}catch(Exception e){
          ???log.error(e);
          ???throw e;
          ??}finally{
          ???try {
          ????if (rs != null) {
          ?????rs.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ???try {
          ????if (ps != null) {
          ?????ps.close();
          ????}
          ???} catch (Exception e) {

          ???}
          ??}
          ??return sequenceValue;
          ?}
          ?/**
          ? * 把對象傳入數據庫
          ? * @param ps
          ? * @param count
          ? * @param object
          ? * @author
          ? */
          ?private final void setObjectValue(PreparedStatement ps, int count, Object object) throws Exception {
          ??log.debug("count is " + count + " object is " + object);
          ??if(object != null) {
          ???if(object instanceof Integer){
          ????ps.setInt(count, ((Integer)object).intValue());
          ???}else if(object instanceof Long) {
          ????ps.setLong(count, ((Long)object).longValue());
          ???}else if(object instanceof BigDecimal){
          ????ps.setBigDecimal(count, (BigDecimal)object);
          ???}else if(object instanceof String){
          ????ps.setString(count, (String)object);
          ???}else if(object instanceof java.util.Date) {
          ????if(object!=null){
          ?????long time = ((java.util.Date)object).getTime();
          ?????ps.setDate(count, new java.sql.Date(time));
          ????}else{
          ?????ps.setDate(count, null);
          ????}
          ???}else{
          ????ps.setObject(count, object);
          ???}
          ??}else{
          ???ps.setNull(count, Types.INTEGER);
          ??}
          ?}

          ?/**
          ? * 把對象傳入數據庫
          ? * @param ps
          ? * @param clazz
          ? * @param count
          ? * @param columnName
          ? * @param object
          ? * @throws Exception
          ? * @author
          ? */
          ?private final void setObjectValue(PreparedStatement ps, Class clazz, int count,
          ???String columnName, Object object)throws Exception {
          ??log.debug("count is " + count + " columnName is " + columnName + " object is " + object);
          ??String classType = clazz.getDeclaredField(columnName).getType().getName();
          ??if(classType.equals("java.lang.Integer")){
          ???if(object != null) {
          ????ps.setInt(count, ((Integer)object).intValue());
          ???}else{
          ????ps.setNull(count, Types.INTEGER);
          ???}
          ??}else if(classType.equals("java.lang.Long")) {
          ???if(object != null ) {
          ????ps.setLong(count, ((Long)object).longValue());
          ???}else{
          ????ps.setNull(count, Types.INTEGER);
          ???}
          ??}else if(classType.equals("java.math.BigDecimal")){
          ???if(object != null) {
          ????ps.setBigDecimal(count, (BigDecimal)object);
          ???}else{
          ????ps.setNull(count, Types.NUMERIC);
          ???}
          ??}else if(classType.equals("java.lang.String")){
          ???if(object != null) {
          ????ps.setString(count, (String)object);
          ???}else{
          ????ps.setString(count, null);
          ???}
          ??}else if(classType.equals("java.util.Date")) {
          ???if(object!=null){
          ????long time = ((java.util.Date)object).getTime();
          ????ps.setDate(count, new java.sql.Date(time));
          ???}else{
          ????ps.setDate(count, null);
          ???}
          ??}else{
          ???ps.setObject(count, object);
          ??}
          ?}

          ?/**
          ? * 把數據從數據取出來
          ? * @param rs
          ? * @param clazz
          ? * @param count
          ? * @param columnName
          ? * @return
          ? * @throws Exception
          ? * @author
          ? */
          ?private final Object getObjectValue(ResultSet rs, Class clazz, int count, String columnName) throws Exception {
          ??Object fieldValue = null;
          ??log.debug("columnName is " + columnName + " count is " + count);
          ??if(columnName != null) {
          ???if("rownum".equals(columnName)) {
          ????fieldValue = new Long(rs.getLong(count));
          ???}else if("rownum_".equals(columnName)) {
          ????fieldValue = new Long(rs.getLong(count));
          ???}else if("count_".equals(columnName)) {
          ????fieldValue = new Long(rs.getLong(count));
          ???}else{
          ????String classType = clazz.getDeclaredField(columnName).getType().getName();

          ????if(classType.equals("java.lang.Integer")){
          ?????fieldValue =new Integer( rs.getInt(count));
          ????}else if(classType.equals("java.lang.Long")) {
          ?????fieldValue =new Long( rs.getLong(count));
          ????}else if(classType.equals("java.math.BigDecimal")){
          ?????fieldValue = rs.getBigDecimal(count);
          ????}else if(classType.equals("java.lang.String")){
          ?????fieldValue = rs.getString(count);
          ????}else if(classType.equals("java.util.Date")) {
          ?????java.sql.Date date = rs.getDate(count);
          ?????if(date!= null){
          ??????fieldValue = new java.util.Date(date.getTime());
          ?????}
          ????}else{
          ?????fieldValue = rs.getString(count);
          ????}
          ???}
          ??}
          ??return fieldValue;
          ?}


          }

          posted on 2006-07-11 17:27 有貓相伴的日子 閱讀(2857) 評論(4)  編輯  收藏 所屬分類: j2ee

          評論:
          # re: 一個寫得不錯的DAO操作類 2006-07-11 21:38 | 劍事
          感覺方法上的參數過多  回復  更多評論
            
          # re: 一個寫得不錯的DAO操作類 2006-07-11 23:07 | hibernate
          寫的不好,事務呢?
          建議用spring的jdbcTemplate  回復  更多評論
            
          # re: 一個寫得不錯的DAO操作類 2006-07-13 22:55 | 有貓相伴的日子
          事務的處理在BO層做。因為一次業務的操作可能需要涉到N個DAO的操作,
          所以事務處理在BO層做!!  回復  更多評論
            
          # re: 一個寫得不錯的DAO操作類 2006-11-22 22:51 | ecsoftcn
          如果單從JDBC的使用來說,樓主的DAO寫的還是不錯的.但是現在的開源框架如此之多,而且性能優越,是否還有必要使用原始的JDBC技術呢?

          如果用IBATIS,那上面的DAO會更加幽雅.至于事物完全可以用Spring來處理全局事物,和你操作幾個DAO沒有關系.  回復  更多評論
            
          本站不再更新,歡迎光臨 java開發技術網
          主站蜘蛛池模板: 梧州市| 青浦区| 上蔡县| 安泽县| 北安市| 凭祥市| 灌南县| 醴陵市| 泽库县| 邯郸市| 澜沧| 古交市| 海伦市| 通榆县| 德化县| 调兵山市| 凤山县| 昌乐县| 友谊县| 托克逊县| 河西区| 梁平县| 济宁市| 酉阳| 榕江县| 马山县| 四平市| 来宾市| 彰武县| 罗定市| 托里县| 通辽市| 西平县| 镇雄县| 白城市| 乐清市| 勃利县| 衢州市| 夏津县| 利津县| 永春县|