Jasperreport+jsp+sql like % problem

0
 

hello, I try to write a report

I hope I can select some sql data and show in the report

this is my sql

select * from send where name like 'n%'

I hope I can show all the "n" data in report like n138 ,n140......

but my code only can show last data

this is my code

can anyone help me

thank very much

<%@page import="net.sf.jasperreports.engine.design.JasperDesign"%>
<%@page import="net.sf.jasperreports.engine.*" %>
<%@page import="net.sf.jasperreports.engine.JasperRunManager"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.File"%>
<%@ page import="java.sql.*"%> 
<%@ include file = "db.jsp"%> //I have connect sql
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% 
                    String sql = "select * from send where name like 'n%' "; 
                    ResultSet rs = stmt.executeQuery(sql); 
                    JRDataSource dataSource = new JRResultSetDataSource(rs);
            File reportFile;
            Map parameters = new HashMap();
           byte[] bytes={0};
            ServletOutputStream outStream ;
            int i=0;
             while(rs.next())
             {
          parameters.put("name",rs.getString("name"));
              parameters.put("youad",rs.getString("youad"));
          reportFile= new File(application.getRealPath("/report/report3.jasper"));
           bytes= JasperRunManager.runReportToPdf(reportFile.getPath(), parameters); 
 
            } 
                response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
           outStream = response.getOutputStream();
            outStream.write(bytes, 0, bytes.length);
          outStream.flush();
            outStream.close(); 
 
            rs.close();
%> 

db.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%> 
    <%! 
        String user = "root"; 
        String pass = ""; 
        String url = "jdbc:mysql://localhost....."; 
 
            Connection conn; 
          Statement stmt; 
         Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                conn = DriverManager.getConnection(url,user,pass); 
               stmt = conn.createStatement();
 
 
    %> 

cool3690's picture
Joined: Sep 18 2018 - 12:52am
Last seen: 5 months 3 days ago

3 Answers:

1

What are you doing with while(rs.next())?

Instead of trying to read result set in your program, your jrxml should have "name" and "yourad" fields (not parameters but fields).

Delete the following lines.

             while(rs.next())
             {
          parameters.put("name",rs.getString("name"));
              parameters.put("youad",rs.getString("youad"));

hozawa's picture
46897
Joined: Apr 24 2010 - 4:31pm
Last seen: 2 months 1 week ago
0

>bytes= JasperRunManager.runReportToPdf(reportFile.getPath(), parameters);

You haven't specified your datasource.

Try the following

bytes= JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, dataSource);

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/engine/Jas...

 

hozawa's picture
46897
Joined: Apr 24 2010 - 4:31pm
Last seen: 2 months 1 week ago
0

hello , hozawa

I have add data source

but it still can show the last data.....

I hope I can show all the n% data

<% 
                    String sql = "select * from send where name like 'n%' "; 
                    ResultSet rs = stmt.executeQuery(sql); 
 
                       JRDataSource dataSource = new JRResultSetDataSource(rs);
            File reportFile;
            Map parameters = new HashMap();
 
           byte[] bytes={0};
            ServletOutputStream outStream ;
            int i=0;
             while(rs.next())
             {
          parameters.put("name",rs.getString("name"));
              parameters.put("youad",rs.getString("youad"));
          reportFile= new File(application.getRealPath("/report/report3.jasper"));
           bytes= JasperRunManager.runReportToPdf(reportFile.getPath(), parameters,dataSource); 
           response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
 
            } 
           outStream = response.getOutputStream();
            outStream.write(bytes, 0, bytes.length);
             outStream.flush();
            outStream.close(); 
 
            rs.close();     
 
%> 

cool3690's picture
Joined: Sep 18 2018 - 12:52am
Last seen: 5 months 3 days ago
Feedback
randomness