1

//error1: java.io.FileNotFoundException: jrxml\Blank_A4.jrxml (The system //cannot find the path specified)

//error2:connectin failure!!! //com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications //link failure

// my coding`enter code here

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperPrint;
import com.seerock.utils.DBConnection;



public class JasperReport {


static Connection conn;

public static void main(String[] args) throws FileNotFoundException {

    try{

        DBConnection db = new DBConnection();
        conn=db.getConnection();

        System.out.println("Loading Report Designs");
    //  InputStream input=new FileInputStream(new File("/jrxml/Blank_A4.jrxml"));
        InputStream input=new FileInputStream(new File("jrxml/Blank_A4.jrxml"));
        JasperDesign jasperDesign= JRXmlLoader.load(input);

        System.out.println("Compiling Report Designs");

        net.sf.jasperreports.engine.JasperReport jasperReport=JasperCompileManager.compileReport(jasperDesign);

        System.out.println("Creating JasperPrint Object");
        Map<String, String> parameters=new HashMap<String, String>();
        parameters.put("ReportTitle", "PDF JasperReport");


        JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport, null, conn); 

        //Exporting the report

        OutputStream output=new FileOutputStream(new File("report/Blank_A4.pdf"));
        JasperExportManager.exportReportToPdfStream(jasperPrint, output);
        System.out.println("Report Generation Complete");
        conn.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41

1 Answers1

1

error1

The file needs to exists and you need to point at it, where is the file, what am i pointing to???

try this...

File f = new File("report/Blank_A4.pdf");
System.out.println(f.getAbsolutePath());

error2.

Connection faliure, you did not manage to connect to your database, url, password, username, port can be wrong, you have not included the code of the DBConnection so it's impossibile to say what you are doing wrong.

Please check this on how to connect to database JDBC Sample example

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109