Natty’s blog

Stay hungry. Stay foolish. — Steve Jobs

Archive for the ‘java’ Category

[Jasper] run Jasper Report from Java class

Posted by natty on October 3, 2008

จริงๆ ก่อนหน้าจะเขียนเืรื่องนี้ ควรเขียนการ run Jasper Report from JSP ก่อนนะเนี่ย แต่ไม่เป็นไร เอาอันนี้ก่อน

ได้วิธีการมาจาก ที่นี่ แต่ต่างกันที่ code การติดต่อ database นิดหน่อย และเราจะทำมันใน eclipse ไม่ได้รันแบบที่เค้าทำ

ก่อนอื่นสร้าง eclipse project เป็น java project แล้วเอา lib ต่อไปนี้ไปใส่ไว้ใน build path ของ eclipse

  • common-collection
  • common-logging
  • iReport
  • commons-javaflow
  • jasperreports
  • common-digester
  • common-beanutils
  • mysql-java-connector

และสร้าง ReportDriver.java ขึ้นมา ดังนี้

import java.sql.*;

import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;

import java.io.OutputStream;

public class ReportDriver {
    /**
     * Constructor for ReportDriver
     */
    public ReportDriver() {       
    }
    public static Connection connectDB(String databaseName, String userName, String password) {
        Connection jdbcConnection = null;
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            String connectionURL = "jdbc:mysql://127.0.0.1:3306/test?user=root;password=root";
            jdbcConnection = DriverManager.getConnection(connectionURL,userName,password);
        }catch(Exception ex) {
            String connectMsg = "Could not connect to the database: " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
        return jdbcConnection;
    }

    /**
     * Takes 4 parameters: databaseName, userName, password, reportFileLocation
     * and connects to the database and prepares and views the report.
     * @param databaseName holds database name,
     * @param userName holds user name
     * @param password holds password to connect the database,
     * @param reportFile holds the location of the Jasper Report file (.jrxml)
     */
    public static void runReport(String databaseName, String userName, String password,String reportFile) {
        try{
            JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            Connection jdbcConnection = connectDB(databaseName, userName, password);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jdbcConnection);
            JasperViewer.viewReport(jasperPrint);
        }catch(Exception ex) {
            String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
            System.out.println(connectMsg);
        }
    }
    public static void main(String[] args) {
        //if (args.length == 4) {
            String databaseName = "test" ;
            String userName = "root";
            String password = "root";
            String reportFile = "Reports/test2.jrxml";
            runReport(databaseName, userName, password, reportFile);
        /*}/lse{
            System.out.println("Usage:");
            System.out.println("java ReportDriver databaseName userName password reportFileLocation");
        }
        return;*/

    }
}

ใน code จะไปเรียกไฟล์ test2.jrxml ที่สร้างโดย iReport ดังนั้นเราต้องไปสร้างไฟล์นี้ก่อน ให้ต่อไปยัง database ตัวเดียวกับที่เราใช้ใน code นี้ เมื่อรัน main method ขึ้นมา ก็จะได้ report ที่ถูก view โดย JRViewer

Posted in jasper, java | 2 Comments »

[Resin] PHP in Java: Natty & Juacompe discussion

Posted by natty on September 24, 2008

เนื่องมาจาก natty ที่พอจะมีความรู้เรื่อง resin เท่าหางอึ่ง + PHP อยู่บ้าง บวกกับ juacompe ที่อัดแน่นไปด้วยความรู้เกี่ยวกับ server ที่ใช้ run java ทำให้วันนี้ก็เกิดความเข้าใจมากขึ้นกับ PHP in Java ใน resin

หากเราต้องการทำให้ java ของเราถูกเรียกโดย PHP ได้ ต้องทำให้ class นั้น extends AbstractQuercusModule และมี method ที่รับ parameter env ซึ่งเป็น quercus environment resources หรือไม่ก็ต้องสร้าง class ไว้แล้วให้มาสร้าง instant อยู่ใน class นี้

code ด้านล่างเป็นเป็น code ที่สร้าง method ขึ้นมาเพื่อให้ถูกเรียกจาก PHP ได้ ชื่อ method ว่า hello_test โดยจะเห็นว่ามันจะรับตัวแปร env ด้วย โดย class นี้อยู่ใน package example

 package example;

import com.caucho.quercus.env.Env;
import com.caucho.quercus.module.AbstractQuercusModule;

public class HelloModule extends AbstractQuercusModule {
   /*
   ** Notice the careful use of the naming
   ** convention hello_test.  This is done
   ** in order to prevent name collisions
   ** among different libraries.
   **
   ** @param env provides access to Quercus environment resources
   ** @param name
   */
   public String hello_test(Env env, String name)
   {
     env.println("inside HelloModule  hello_test()");
     return "Hello, " + name;
   }
} 

สิ่งที่สำคัญคือ ต้องสร้างไฟล์ชื่อ com.caucho.quercus.QuercusModule ไว้ที่ classes/META-INF/services โดยใส่ชื่อ package.class เอาไว้

 example.HelloModule 

ตอนหน้าค่อยต่ออีก….นี่เป็นเพียง study ยังไม่ได้ทดสอบเลยว่าทำต่างจากนี้แล้วจะพังป่าว

Posted in PHP, configuration, java | Leave a Comment »