1

Im using javabridge to connect php to jasper reports and Im trying to pass two parameters but I get warnings and errors

Warning: Unchecked exception detected: [[o:Response$UndeclaredThrowableErrorMarker]:"FATAL: Undeclared java.lang.RuntimeException detected. java.lang.Exception: CreateInstance failed: new java.util.Date((o:String)[o:String]). Cause: java.lang.IllegalArgumentException VM: 1.7.0_79@http://java.oracle.com/" at: #-10 java.util.Date.parse(Unknown Source) #-9 java.util.Date.<init>(Unknown Source) #-8 sun.reflect.GeneratedConstructorAccessor57.newInstance(Unknown Source) #-7 sun.reflect.DelegatingConstructorAccessor[...]/java/Java.inc(361): java_Arg->getResult(false) #2 http://localhost:8080/JavaBridgeTemplate/java/Java.inc(364): java_Client->getWrappedResult(false) #3 http://localhost:8080/JavaBridgeTemplate/java/Java.inc(536): java_Client->getInternalResult() #4 http://localhost:8080/JavaBridgeTemplate/java/Java.inc(1930): java_Client->createObject('java.util.Date', Array) #5 C:\wamp\www\advanced\backend\javabridge\generate.php(49): Java->Java('java.util.Date', '12/Feb/16') #6 {main}] in http://localhost:8080/JavaBridgeTemplate/java/Java.inc on line 202

 Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke failed: [[c:JasperFillManager]]->fillReport((o:JasperReport)[o:JasperReport], (i:Map)[o:HashMap], (i:Connection)[o:Connection]). Cause: net.sf.jasperreports.engine.JRException: Incompatible php.java.bridge.Response$UndeclaredThrowableErrorMarker value assigned to parameter FInicio in the Reubicados dataset. VM: 1.7.0_79@http://java.oracle.com/" at: #-16 net.sf.jasperreports.engine.fill.JRFillDataset.setParameter(JRFillDataset.java:903) #-15 net.sf.jasperreports.engine.fill.JRFillDataset.setFillParameterValues(JRFillDataset.java:642) #-14 net.sf.jasperreports.engine.fill.JRFillDataset.setParameterValues(JRFillDataset.java:585) #-13 net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1280) #-12 net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:901) #-11 net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:845) #-10 net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:58) #-9 net.sf.jas in http://localhost:8080/JavaBridgeTemplate/java/Java.inc on line 195

The problem is when it tries to create java.util.Date instance. Here´s php file:

   <?php


    require_once("http://localhost:8080/JavaBridgeTemplate/java/Java.inc");

    try {   
        $Param1 = date('d/M/y', strtotime($_POST['FInicio']));
        $Param2 = date('d/M/y', strtotime($_POST['FFin']));



        $jasperxml = new java("net.sf.jasperreports.engine.xml.JRXmlLoader");
        $jasperDesign = $jasperxml->load(realpath("Reubicados.jrxml"));
        $query = new java("net.sf.jasperreports.engine.design.JRDesignQuery");

        $jasperDesign->setQuery($query);
        $compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
        $report = $compileManager->compileReport($jasperDesign); } catch (JavaException $ex) {
        echo $ex; }


    $fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager"); //aqui se pasan los parametros (Fecha Inicio y Fecha Fin) 
$params = new Java("java.util.HashMap"); 
$date=new Java('java.util.Date',$Param1);
$date1=new Java('java.util.Date',$Param2); 
$params->put("FInicio",$date); 
$params->put("FFin",$date1);



    $class = new JavaClass("java.lang.Class"); $class->forName("com.mysql.jdbc.Driver"); $driverManager = new JavaClass("java.sql.DriverManager");

    //db username and password 
$conn = $driverManager->getConnection("jdbc:mysql://localhost/viajestrafico?zeroDateTimeBehavior=convertToNull", "root", "root"); $jasperPrint = $fillManager->fillReport($report, $params, $conn);

    $exporter = new java("net.sf.jasperreports.engine.JRExporter");

And sql query in ireports:

SELECT
     ayudante_situacion_laboral.`FechaInicio` AS ayudante_situacion_laboral_FechaInicio,
     ayudante_situacion_laboral.`FechaFin` AS ayudante_situacion_laboral_FechaFin,
     ayudante_situacion_laboral.`Cant_Horas` AS ayudante_situacion_laboral_Cant_Horas,
     ayudante_situacion_laboral.`Descripcion` AS ayudante_situacion_laboral_Descripcion,
     ayudante.`Registro` AS ayudante_Registro,
     ayudante.`Nombre` AS ayudante_Nombre,
     situacion_laboral.`Estado` AS situacion_laboral_Estado
FROM
     `ayudante` ayudante INNER JOIN `ayudante_situacion_laboral` ayudante_situacion_laboral ON ayudante.`Ayudante_ID` = ayudante_situacion_laboral.`AyudanteAyudante_ID`
     INNER JOIN `situacion_laboral` situacion_laboral ON ayudante_situacion_laboral.`Situacion_LaboralSitL_ID` = situacion_laboral.`SitL_ID`
WHERE
     situacion_laboral.Estado = 'Reubicado'  and $P{FInicio}<= ayudante_situacion_laboral.`FechaInicio` and $P{FFin}>=ayudante_situacion_laboral.`FechaFin`
UNION

     SELECT
     chofer_situacion_laboral.`FechaInicio` AS chofer_situacion_laboral_FechaInicio,
     chofer_situacion_laboral.`FechaFin` AS chofer_situacion_laboral_FechaFin,
     chofer_situacion_laboral.`Cant_Horas` AS chofer_situacion_laboral_Cant_Horas,
     chofer_situacion_laboral.`Descripcion` AS chofer_situacion_laboral_Descripcion,
     chofer.`Registro` AS chofer_Registro,
     chofer.`Nombre` AS chofer_Nombre,
     situacion_laboral.`Estado` AS situacion_laboral_Estado
FROM
     `chofer` chofer INNER JOIN `chofer_situacion_laboral` chofer_situacion_laboral ON chofer.`Chofer_ID` = chofer_situacion_laboral.`ChoferChofer_ID`
     INNER JOIN `situacion_laboral` situacion_laboral ON chofer_situacion_laboral.`Situacion_LaboralSitL_ID` = situacion_laboral.`SitL_ID`
WHERE
     (situacion_laboral.Estado = 'Reubicado' and $P{FInicio}<= chofer_situacion_laboral.`FechaInicio` and $P{FFin}>=chofer_situacion_laboral.`FechaFin`)

2 Answers 2

3

Looking to your error message, the java.util.Date cannot be created with '12/Feb/16':

java_Client->createObject('java.util.Date', Array) 
#5 C:\wamp\www\advanced\backend\javabridge\generate.php(49): 
Java->Java('java.util.Date', '12/Feb/16') #6 {main}] in 

If you want to instanciate a java date object, use the java.util.Date(long date) constructor which accepts a timestamp expressed in milliseconds :

<?php
$startDate = $_POST['FInicio'];  // better to filter this :)
$Param1 = strtotime($startDate) * 1000; // to get milliseconds
if ($Param1 == 0) {
   throw new Exception("FInicio date parameter could not be parsed");
}

// ...
$javaDate = new Java('java.util.Date',$Param1); // This is a java date

Your $date parameter should now be a valid java.util.Date object.

You can test it :

$simpleDateFormat = new Java("java.text.SimpleDateFormat", 'yyyy-MM-dd');
echo $simpleDateFormat->format($javaDate); 
// should print your date in Y-m-d format

Alternatively, you can parse the date in Java through the java.text.SimpleDateFormatter object:

$date = '2016-12-21';
$simpleDateFormat = new Java("java.text.SimpleDateFormat", 'yyyy-MM-dd');
$javaDate = $simpleDateFormat->parse($date); // This is a Java date

Both approaches works...


JasperReport issue with date

Not exactly linked to your question, but if you want to use your Date as a query parameter you should use the java.sql.Date(long date) object instead of java.util.Date... Here's a little dirty snippet to summarize changes:

// php 

$sqlDate = new Java('java.sql.Date', strtotime($_POST['FInicio']) * 1000));
$params->put('FInicio', $sqlDate);

// in your report header (.jrxml):
<parameter name="FInicio" class="java.sql.Date">

// in your report query (.jrxml):
$P{FInicio} <= chofer_situacion_laboral.`FechaInicio`

You can also have a look to the soluble-japha javabridge client (refactored Java.inc client), the syntax differs a bit but there's a documentation about dates that might reveal useful.

Sign up to request clarification or add additional context in comments.

16 Comments

Thanks. Im trying the first approach but I need an object formatted as d/M/y to build the query. So I get empty results putting just like that. Any advice?
Looking at your previous post, you define your dates parameters as 'Date'
Looking at your previous post, you defined your dates as 'java.util.Date'... If you want to build your query with them, it must be done on the jasper side. If yo want to avoid jasper side, you can always specify your jasper parameters as 'java.lang.String()' and instead of preparaing a date in php, send dates as text.
Just in case, I've update the solution to include a test for parsing the date. Please have a look to the exception test. It might be the cause and easily debugable. From your previous comment, I don't think you need to format your date object in the query with a jdbc mysql datasource, a simple 'startDate > $P{FInicio}' should work as Jasper knows it's a date. Not sure 100%, but try and see.
I wouldn't think about format problems (jasper handles it, a date is a date). Just updated the solution to include a test for displaying the date you've entered. Test and if the date is the same, we are absolutly sure it's not on the PHP side... then check your jasper query params (i.e. : tableColumnDate > $P{startDate}), ensure there's no ! into param.
|
1

It was simple, just a problem I had from the beginning in php code.

    $query = new java("net.sf.jasperreports.engine.design.JRDesignQuery");

    $jasperDesign->setQuery($query);

This code was preventing xrml query from executing, because it was creating a query object empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.