All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.voovan.db.ResultInfo Maven / Gradle / Ivy

package org.voovan.db;

import org.voovan.tools.TSQL;
import org.voovan.tools.log.Logger;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 结果集和数据库连接封装
 *
 * @author helyho
 *
 * Java Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class ResultInfo {
    private ResultSet resultSet;
    private JdbcOperate jdbcOperate;

    public ResultInfo(ResultSet resultSet,JdbcOperate jdbcOperate) {
        this.resultSet = resultSet;
        this.jdbcOperate =jdbcOperate;
    }

    public JdbcOperate getJdbcOperate() {
        return jdbcOperate;
    }

    @SuppressWarnings("unchecked")
    public  List getObjectList(Class t) {
        try{
            return (List) TSQL.getAllRowWithObjectList(t, this.resultSet);
        }catch(SQLException | ReflectiveOperationException | ParseException e){
            Logger.error("JdbcOperate.getObjectList error",e);
        }finally{
            // 非事务模式执行
            if (jdbcOperate.getTranscationType() == TranscationType.NONE) {
                jdbcOperate.closeConnection(resultSet);
            }else{
                jdbcOperate.closeResult(resultSet);
            }
        }
        return new ArrayList();

    }

    public List> getMapList() {
        try{
            return TSQL.getAllRowWithMapList(this.resultSet);
        }catch(SQLException | ReflectiveOperationException e){
            Logger.error("JdbcOperate.getMapList error",e);
        }finally{
            // 非事务模式执行
            if (jdbcOperate.getTranscationType() == TranscationType.NONE) {
                jdbcOperate.closeConnection(resultSet);
            }else{
                jdbcOperate.closeResult(resultSet);
            }
        }
        return new ArrayList>();
    }

    @SuppressWarnings("unchecked")
    public  Object getObject(Class t){
        try{
            if(resultSet.next()){
                return (T) TSQL.getOneRowWithObject(t, this.resultSet);
            }else{
                return null;
            }
        }catch(SQLException | ReflectiveOperationException | ParseException e){
            Logger.error("JdbcOperate.getObject error",e);
        }finally{
            // 非事务模式执行
            if (jdbcOperate.getTranscationType() == TranscationType.NONE) {
                jdbcOperate.closeConnection(resultSet);
            }else{
                jdbcOperate.closeResult(resultSet);
            }
        }
        return null;
    }

    public Map getMap(){
        try{
            if(resultSet.next()){
                return TSQL.getOneRowWithMap(this.resultSet);
            }else{
                return null;
            }
        }catch(SQLException | ReflectiveOperationException e){
            Logger.error("JdbcOperate.getMap error",e);
        }finally{
            // 非事务模式执行
            if (jdbcOperate.getTranscationType() == TranscationType.NONE) {
                jdbcOperate.closeConnection(resultSet);
            }else{
                jdbcOperate.closeResult(resultSet);
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy