IReport com 2 querys

6 respostas
D

Olá comunidade,
Estou precisando da ajuda de vocês. Bem, estou montando um relatório que apresenta as informações de Receita e Despesa da empresa, o problema é que estes dados não tem relação nenhuma, ou seja, são do mesmo modelo de dados porém não possuem relação. Preciso apresenta-las como se fosse em duas colunas uma ao lado da outra para ter uma visualização macro do negócio. Ja tentei usar subrelatorio, mas os mesmos não são apresentados. Quando coloco a query de despesa no relatorio principal por exemplo, funciona somente o subrelatorio de despesa. Tem como eu só passar os parametros do principal para os subrelatorios sem ter que criar a query no principal?

Desde já agradeço à todos…

6 Respostas

P

Bom uma solução seria vc fazer a consulta de receita no principal e criar um subrelatório de despesas passando parametros para ele…Ou criar um relatório principal, sem query, e vc adiciona 2 subrelatórios, um para despesa e outro para receita, também funcionaria…
E respondendo sua pergunta, tem sim como passar parametros do pincipal para os subs sem query no principal…

Fico no aguardo

D

Você pode criar um Empty Datasource (lá em conexões), e setar ele para conter apenas 1 registro. Com isso o relatório não é gerado em branco e vc não é obrigado a colocar uma querie…

daí vc faz ele receber como parametro apenas a conexão do banco, e criar os 2 subrelatorios, e passa a conexao pra eles usarem…

Se vc estiver utilizando o ireport 3.5.2 (ou superior) nem precisa criar os subrelatorios. Nessa versão tem um componente novo chamado List, daí vc pode criar as duas queries no relatório principal mesmo e e criar 2 lists pra usar as queries…

http://therobinsonfamily.net/?p=30

D

Bom dia galera,

Paezani e danielsb2…

Fiz os testes aqui com as soluções que vocês postaram e todas funcionaram muito bem. :smiley:
Optei por usar a opção dos lists com dataset…ficou lindo…rss

Valeu galera!!

D

Pessoal,

Achei que estava tudo certo, mas quando fui chamar meu relatório a partir da minha aplicação os dados dos lists não são apresentados, como se eu não estivesse passando a conexão do master para eles. Portanto eu coloquei o $P{REPORT_CONNECTION} em Dataset run -> Connection/Datasource exp. Quando eu dou um preview no iReport os dados são apresentados e os parametros são passados corretamente do master para os lists…mas chamando da aplicação não.
Outra coisa, tenho certeza que o problema não está na aplicação pois fiz o teste chamando o mesmo relatorio com e sem o list, somente com os fields no detail do master e tudo funciona. A não ser que eu tenha que passar a conexão como parâmetro da aplicação. :?
Alguém sabe o que pode ser?

Em anexo envio a imagem do template do relatório, note que coloquei um field (em vermelho) no detail do master para testar, sendo que somente este field é apresentado.

Valeu!


D

Resolvido…passando a conexão por parametro da aplicação.

:smiley:

F

Galera sei que o poste é antigo.
Estou tendo o mesmo problema, em preview do ireport funciona corretamente, quando chamo da aplicação java apenas o list vem null.
O que estou fazendo errado.

CLASSE IREPORT

package report;

import dao.AcessoBancoDeDados;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.view.JasperViewer;
import com.lowagie.text.pdf.codec.Base64.InputStream;
import model.VendaEntrega;
import net.sf.jasperreports.engine.JasperPrintManager;


public class Report extends AcessoBancoDeDados{

    public void geraRelatorio(VendaEntrega entrega) throws JRException, Exception{
        
    Connection conexao = conectar();

    String sql = "SELECT V.ID ID_VENDA_ENTREGA, V.MOTOBOY, P.FORMA_PAGAMENTO, P.ID ID_PEDIDO, P.TOTAL, P.TROCO, P.DATA, P.HORA, "
                            + "P.SITUACAO, C.NOME NOME_CLIENTE, C.BAIRRO, C.ENDERECO, C.NUMERO, C.TELEFONE, "
                            + "C.ID ID_CLIENTE_E "
                            + "FROM VENDA_ENTREGA V "
                            + "INNER JOIN PEDIDO P ON V.ID_PEDIDO_V_E = P.ID "
                            + "INNER JOIN CLIENTE C ON V.ID_CLIENTE_V_E = C.ID "
                            + "WHERE V.ID = ? ";
    
    PreparedStatement stm = conexao.prepareStatement(sql);
    
    stm.setInt(1, entrega.getId());
    
    ResultSet resultado = stm.executeQuery();
    
    //implementacao da interface JRDataSource para DataSource ResultSet
    
    JRResultSetDataSource jrRS = new JRResultSetDataSource( resultado );
    //HashMap de parametros utilizados no relatorio. Sempre instanciados    

    Map parameters = new HashMap();
    

    //Preenche o relatorio com os dados.    
    JasperFillManager.fillReportToFile( "relatorio/view/Entrega.jasper", parameters, jrRS );
    
    //Exporta para o formato PDF    
    JasperExportManager.exportReportToPdfFile( "relatorio/view/report name.jrprint" );
    
    // Visualiza o relatorio em formato PDF    
    JasperViewer.viewReport( "relatorio/view/report name.jrprint", false,false );

XML RELATORIO

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="351" pageHeight="300" columnWidth="311" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d5141310-8daa-487a-8cfa-b133004d4778">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<subDataset name="ITENS" uuid="07cf3489-a2a5-4290-9050-7f604b254d81">
		<parameter name="ID_PEDIDO" class="java.lang.Integer">
			<defaultValueExpression><![CDATA[]]></defaultValueExpression>
		</parameter>
		<queryString>
			<![CDATA[SELECT I.ID, I.NOME_PROD, I.QUANTIDADE, I.VALOR FROM ITENS_PEDIDO I INNER JOIN PEDIDO P ON P.ID = I.ID_PEDIDO_I WHERE P.ID = $P{ID_PEDIDO};]]>
		</queryString>
		<field name="ID" class="java.lang.Integer"/>
		<field name="NOME_PROD" class="java.lang.String"/>
		<field name="QUANTIDADE" class="java.lang.Integer"/>
		<field name="VALOR" class="java.lang.Double"/>
	</subDataset>
	<queryString>
		<![CDATA[SELECT V.ID ID_VENDA_ENTREGA, V.MOTOBOY, P.FORMA_PAGAMENTO, P.ID ID_PEDIDO, P.TOTAL, P.TROCO, P.DATA, P.HORA, 
P.SITUACAO, C.NOME NOME_CLIENTE, C.BAIRRO, C.ENDERECO, C.NUMERO, C.TELEFONE, C.ID ID_CLIENTE_E, I.ID, I.NOME_PROD, I.QUANTIDADE, I.VALOR
FROM VENDA_ENTREGA V 
INNER JOIN PEDIDO P ON V.ID_PEDIDO_V_E = P.ID
INNER JOIN ITENS_PEDIDO I ON I.ID_PEDIDO_I = P.ID
INNER JOIN CLIENTE C ON V.ID_CLIENTE_V_E = C.ID;]]>
	</queryString>
	<field name="ID_VENDA_ENTREGA" class="java.lang.Integer"/>
	<field name="MOTOBOY" class="java.lang.String"/>
	<field name="FORMA_PAGAMENTO" class="java.lang.String"/>
	<field name="ID_PEDIDO" class="java.lang.Integer"/>
	<field name="TOTAL" class="java.lang.Double"/>
	<field name="TROCO" class="java.lang.Double"/>
	<field name="DATA" class="java.lang.String"/>
	<field name="HORA" class="java.lang.String"/>
	<field name="SITUACAO" class="java.lang.String"/>
	<field name="NOME_CLIENTE" class="java.lang.String"/>
	<field name="BAIRRO" class="java.lang.String"/>
	<field name="ENDERECO" class="java.lang.String"/>
	<field name="NUMERO" class="java.lang.Integer"/>
	<field name="TELEFONE" class="java.lang.String"/>
	<field name="ID_CLIENTE_E" class="java.lang.Integer"/>
	<field name="ID" class="java.lang.Integer"/>
	<field name="NOME_PROD" class="java.lang.String"/>
	<field name="QUANTIDADE" class="java.lang.Integer"/>
	<field name="VALOR" class="java.lang.Double"/>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band height="260" splitType="Stretch">
			<staticText>
				<reportElement uuid="0f5a6829-acd8-4969-8ede-a2ebe602fa00" x="114" y="13" width="76" height="20"/>
				<textElement>
					<font size="14" isBold="true"/>
				</textElement>
				<text><![CDATA[Sandubaia]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="f9d6df57-0231-4350-bbeb-cccb327d224b" x="82" y="33" width="150" height="13"/>
				<textElement/>
				<text><![CDATA[Rua Vera Lucia, 153 -  Goânia]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="314689e2-c6de-42e2-a917-b9be93d6f958" x="70" y="46" width="179" height="13"/>
				<textElement/>
				<text><![CDATA[MG Belo Horizonte - CEP 31950-060]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="b2bebcff-7dd2-4d3b-b1e6-bc2aee31f268" x="0" y="0" width="29" height="13"/>
				<textElement/>
				<text><![CDATA[Data:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="2cc07d1b-4db9-452c-bafc-a6539a994284" x="93" y="0" width="29" height="13"/>
				<textElement/>
				<text><![CDATA[Hora:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="29" y="0" width="64" height="13" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{DATA}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="122" y="0" width="43" height="13" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{HORA}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement uuid="84f3e86f-d75f-4dae-b081-6e59b2fbd9e3" x="0" y="72" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Nome:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="53" y="72" width="258" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{NOME_CLIENTE}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement uuid="8fd1f63d-d7f9-4d94-a998-b92e5c7ca102" x="0" y="86" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Endereço:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="ea655f82-682f-402f-950f-9a2b8d7c0b50" x="0" y="114" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Total:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="83f35279-b098-42c3-9563-f602df12f1ba" x="0" y="128" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Troco:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="5b9d67cf-71ae-4677-b7f6-0d3cf4427332" x="126" y="114" width="106" height="14"/>
				<textElement/>
				<text><![CDATA[Forma de Pagamento:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="53" y="86" width="258" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{ENDERECO}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="53" y="114" width="61" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{TOTAL}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="232" y="114" width="79" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{FORMA_PAGAMENTO}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="53" y="128" width="61" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{TROCO}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="232" y="100" width="43" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{NUMERO}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement uuid="dd25e004-6b3c-43ee-a88a-46d99cad6c7d" x="0" y="100" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Bairro:]]></text>
			</staticText>
			<textField>
				<reportElement uuid="9891bf41-0b17-4a81-81f1-e06e47a70013" x="53" y="100" width="157" height="14" forecolor="#000000"/>
				<textElement>
					<font size="10" isBold="false"/>
				</textElement>
				<textFieldExpression><![CDATA[$F{BAIRRO}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement uuid="99eaa3ce-664e-45f4-a728-08f7ab0698fb" x="210" y="100" width="22" height="14"/>
				<textElement/>
				<text><![CDATA[N°:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="83f35279-b098-42c3-9563-f602df12f1ba" x="0" y="142" width="82" height="14"/>
				<textElement/>
				<text><![CDATA[ITENS PEDIDO:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="83f35279-b098-42c3-9563-f602df12f1ba" x="0" y="156" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Nome:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="83f35279-b098-42c3-9563-f602df12f1ba" x="126" y="156" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Quantidade:]]></text>
			</staticText>
			<staticText>
				<reportElement uuid="83f35279-b098-42c3-9563-f602df12f1ba" x="249" y="156" width="53" height="14"/>
				<textElement/>
				<text><![CDATA[Valor:]]></text>
			</staticText>
			<componentElement>
				<reportElement uuid="71218388-7fcb-4d80-9dc0-1cf50d245f5a" x="0" y="170" width="311" height="90"/>
				<jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
					<datasetRun subDataset="ITENS" uuid="397b373c-d655-46b1-bd31-2598d3aee362">
						<datasetParameter name="ID_PEDIDO">
							<datasetParameterExpression><![CDATA[$F{ID_PEDIDO}]]></datasetParameterExpression>
						</datasetParameter>
						<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
					</datasetRun>
					<jr:listContents height="90" width="311">
						<textField>
							<reportElement uuid="e2c7739d-7f98-4228-ae4a-95c26839485d" x="0" y="0" width="100" height="20"/>
							<textElement/>
							<textFieldExpression><![CDATA[$F{NOME_PROD}]]></textFieldExpression>
						</textField>
						<textField>
							<reportElement uuid="00fba14e-3faa-4cc0-83ba-dd8af95879ca" x="100" y="0" width="100" height="20"/>
							<textElement/>
							<textFieldExpression><![CDATA[$F{QUANTIDADE}]]></textFieldExpression>
						</textField>
						<textField>
							<reportElement uuid="21188441-b4c0-4604-b96b-2731060d12f5" x="202" y="0" width="100" height="20"/>
							<textElement/>
							<textFieldExpression><![CDATA[$F{VALOR}]]></textFieldExpression>
						</textField>
					</jr:listContents>
				</jr:list>
			</componentElement>
		</band>
	</title>
</jasperReport>
Criado 9 de julho de 2009
Ultima resposta 7 de jul. de 2015
Respostas 6
Participantes 4