Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql jdbc driver installation issue on Tomcat

Status
Not open for further replies.

magostoni

Programmer
Mar 22, 2016
1
IT
I'm trying to have a php code interacting with jasper reports library.

I have a development environment in windows, using xampp with
apache 2.4 listening on port 8090
php 5.5.24
mysql listening on port 3307
tomcat 7.0.56 listening on port 8080

I successfully installed and configured PHP/JavaBridge in Tomcat in C:\xampp_5.5.24\tomcat\webapps\JavaBridgePHP, so that if I browse to I can run correctly the samples.

Inside C:\xampp_5.5.24\tomcat\webapps\JavaBridgePHP\WEB-INF\lib I deployed all the jasper .jar files plus dependencies needed.

I deployed sakila test db to Mysql server.

Then as last step I deployed in xampp test application for jasper integration C:\xampp_5.5.24\htdocs\jasper-report-php-integration-master.
Here the file I post to from a php form.


Code:
    <?php 
    require_once("[URL unfurl="true"]http://localhost:8080/JavaBridgePHP/java/Java.inc");[/URL]
    try {

    $jasperxml = new Java("net.sf.jasperreports.engine.xml.JRXmlLoader");
    $jasperDesign = $jasperxml->load(realpath("customer.jrxml"));
    $query = new Java("net.sf.jasperreports.engine.design.JRDesignQuery");
    $query->setText("SELECT customer.first_name AS customer_first_name,
                                customer.last_name AS customer_last_name,
                                customer.email AS customer_email
                         FROM  customer customer");
    $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");

    $params = new Java("java.util.HashMap");
    $params->put("title", "Customer");
    $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:3307/sakila?zeroDateTimeBehavior=convertToNull", "root", "");
    $jasperPrint = $fillManager->fillReport($report, $params, $conn);

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

    switch ($_POST['format']) {
    case 'xls':
        $outputPath = realpath(".") . "\\" . "output.xls";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_ONE_PAGE_PER_SHEET, java("java.lang.Boolean")->TRUE);
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_WHITE_PAGE_BACKGROUND, java("java.lang.Boolean")->FALSE);
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java("java.lang.Boolean")->TRUE);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=output.xls");
        break;
    case 'csv':
        $outputPath = realpath(".") . "\\" . "output.csv";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.JRCsvExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->FIELD_DELIMITER, ",");
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->RECORD_DELIMITER, "\n");
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->CHARACTER_ENCODING, "UTF-8");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
         } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/csv");
        header("Content-Disposition: attachment; filename=output.csv");
        break;
    case 'docx':
        $outputPath = realpath(".") . "\\" . "output.docx";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.ooxml.JRDocxExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        
            
        } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition: attachment; filename=output.docx");
        break;
    case 'html':
        $outputPath = realpath(".") . "\\" . "output.html";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.JRHtmlExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        
            
        } catch (JavaException $ex) {
            echo $ex;
        }
        break;
    case 'pdf':
        $outputPath = realpath(".") . "\\" . "output.pdf";

        $exporter = new java("net.sf.jasperreports.engine.export.JRPdfExporter");
        $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
        $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        
        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=output.pdf");
        break;
    case 'ods':
        $outputPath = realpath(".") . "\\" . "output.ods";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.oasis.JROdsExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
         } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/vnd.oasis.opendocument.spreadsheet");
        header("Content-Disposition: attachment; filename=output.ods");
        break;
    case 'odt':
        $outputPath = realpath(".") . "\\" . "output.odt";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.oasis.JROdtExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
         } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/vnd.oasis.opendocument.text");
        header("Content-Disposition: attachment; filename=output.odt");
        break;
    case 'txt':
        $outputPath = realpath(".") . "\\" . "output.txt";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.JRTextExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_WIDTH, 120);
            $exporter->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_HEIGHT, 60);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: text/plain");
        break;
    case 'rtf':
        $outputPath = realpath(".") . "\\" . "output.rtf";

        try {
            $exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: application/rtf");
        header("Content-Disposition: attachment; filename=output.rtf");
        break;
    case 'pptx':
         $outputPath = realpath(".") . "\\" . "output.pptx";
        try {
            $exporter = new java("net.sf.jasperreports.engine.export.ooxml.JRPptxExporter");
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
            $exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
        } catch (JavaException $ex) {
            echo $ex;
        }

        header("Content-type: aapplication/vnd.ms-powerpoint");
        header("Content-Disposition: attachment; filename=output.pptx");
      break;
    }
    $exporter->exportReport();
        
    readfile($outputPath);
    unlink($outputPath);
    ?>


Unfortunatly when it reaches the line `$class->forName("com.mysql.jdbc.Driver");` it raises this error

Code:
    Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke failed: [[c:Class]]->forName((o:String)[o:String]). Cause: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver VM:
    1.8.0_74@[URL unfurl="true"]http://java.oracle.com/"[/URL] at: #-14 java.net.URLClassLoader.findClass(URLClassLoader.java:381) #-13 java.lang.ClassLoader.loadClass(ClassLoader.java:424) #-12 java.lang.ClassLoader.loadClass(ClassLoader.java:357) #-11 java.lang.Class.forName0(Native Method) #-10 java.lang.Class.forName(Class.java:264) #-9 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) #-8 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    #-7 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    #-6 java.lang.reflect.Method.invoke(Method.java:498) #-5 php.java.bridge.JavaBridge.Invoke(JavaBridge.java:1044) #-4 php.java.bridge.Request.handleRequest(Request.java:417) #-3 php.java.bridge.Request.handleRequests(Request.java:500) #-2 php.java.bridge.http.ContextRunner.run(ContextRunner.java:145) #-1 p in [URL unfurl="true"]http://localhost:8080/JavaBridgePHP/java/Java.inc[/URL] on line 195

I checked for mysql jdbc driver; but it seems I did all I need.
I downloaded mysql-connector-java-3.1.14.zip from Mysql website.
I've taken the mysql-connector-java-3.1.14-bin.jar inside and copied it to C:\xampp_5.5.24\tomcat\lib

I also created a context.xml file in C:\xampp_5.5.24\tomcat\webapps\JavaBridgePHP\META-INF

Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context>
        <Resource type="javax.sql.DataSource" name="jdbc/sakila"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3307/sakila"
            username="root" password=""
            maxActive="100" maxIdle="20" minIdle="15" initialSize="15" maxWait="10000" />
    </Context>

Then I modified also the web.xml file in C:\xampp_5.5.24\tomcat\webapps\JavaBridgePHP\WEB-INF adding these lines of code

Code:
	<resource-env-ref>
		<resource-env-ref-name>jdbc/sakila</resource-env-ref-name>
		<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
	</resource-env-ref>
I also tried copying mysql-connector-java-3.1.14-bin.jar also to C:\xampp_5.5.24\tomcat\webapps\JavaBridgePHP\WEB-INF\lib and restarting Tomcat, but I'm always getting the same error.

I searched a lot on google but it seems I have done the right steps.... surely I'm missing something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top