org.apache.commons.dbutils.handlers.MapHandler Java Examples

The following examples show how to use org.apache.commons.dbutils.handlers.MapHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EsPublisher.java    From tunnel with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> executeQuery(String sql, InvokeContext context) {
    Connection connection = null;
    try {
        connection = getConnection(context);
        QueryRunner qr = new QueryRunner();
        Map<String, Object> query = qr.query(connection, sql, new MapHandler());
        if (query == null || query.isEmpty()) {
            query = new LinkedHashMap<>();
            LOG.warn("Select Nothing By SQL:{}", sql);
        }
        return query;
    } catch (Exception e) {
        //
    } finally {
        closeClosable(connection);
    }
    return new LinkedHashMap<>();
}
 
Example #2
Source File: EsPublisher.java    From tunnel with Apache License 2.0 6 votes vote down vote up
private Map<String, Object> executeQuery(String sql, InvokeContext context) {
    Connection connection = null;
    try {
        connection = getConnection(context);
        QueryRunner qr = new QueryRunner();
        Map<String, Object> query = qr.query(connection, sql, new MapHandler());
        if (query == null || query.isEmpty()) {
            query = new LinkedHashMap<>();
            LOG.warn("Select Nothing By SQL:{}", sql);
        }
        return query;
    } catch (Exception e) {
        //
    } finally {
        closeClosable(connection);
    }
    return new LinkedHashMap<>();
}
 
Example #3
Source File: M.java    From ThinkJD with Apache License 2.0 6 votes vote down vote up
public long add() throws Exception{
	try {
		if (buildSql_Insert()) {
			Map<String, Object> result_insert = new QueryRunner().insert(conn, sql, new MapHandler(), param_data);
			long id=0;
			if(null!=result_insert && result_insert.containsKey("GENERATED_KEY")) {
				id = (long) result_insert.get("GENERATED_KEY");
			}
			D.autoCloseConn(conn);
			return id;
		}
	} catch (Exception e) {
		D.autoCloseConn(conn);
		throw e;
	}
	return 0;
}
 
Example #4
Source File: DataFactory.java    From xiaoyaoji with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Map<String, Object> getById(final String tableName, final String id) {
    return process(new Handler<Map<String, Object>>() {
        @Override
        public Map<String, Object> handle(Connection connection, QueryRunner qr) throws SQLException {
            String sql = "select * from " + tableName + " where id = ?";
            return qr.query(connection, sql, new MapHandler(), id);
        }
    });
}
 
Example #5
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> queryMap(String sql, Object... params) {
    Map<String, Object> map;
    try {
        map = queryRunner.query(sql, new MapHandler(), params);
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return map;
}
 
Example #6
Source File: DefaultDataAccessor.java    From smart-framework with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> queryMap(String sql, Object... params) {
    Map<String, Object> map;
    try {
        map = queryRunner.query(sql, new MapHandler(), params);
    } catch (SQLException e) {
        logger.error("查询出错!", e);
        throw new RuntimeException(e);
    }
    printSQL(sql);
    return map;
}
 
Example #7
Source File: DbUtilsDemo.java    From JavaTutorial with Apache License 2.0 5 votes vote down vote up
/**
 * 将查询结果集转换成键值对列表返回。
 * 
 * @param ds JDBC连接池
 * @param userId 用户编号
 */
public void queryMap(DataSource ds, int userId) {
    String sql = "select userId, userName, gender, age from student where userId=?";
    
    QueryRunner run = new QueryRunner(ds);
    ResultSetHandler<Map<String, Object>> handler = new MapHandler();
    Map<String, Object> result = null;
    try {
        result = run.query(sql, handler, userId);
    } catch (SQLException e) {
        _logger.error("获取JDBC连接出错或执行SQL出错", e);
    }
    
    System.out.println(result);
}
 
Example #8
Source File: JdbcHelper.java    From obevo with Apache License 2.0 4 votes vote down vote up
public MutableMap<String, Object> queryForMap(Connection conn, String sql) {
    return MapAdapter.adapt(this.query(conn, sql, new MapHandler()));
}