php调用java,并生成 jasperReports报表
关键字 php java bridge iReport jasperReports pdf
1.先安装配置环境,请参阅前辈们的文章 我也提供一个参考地址 http://zhengdl126.iteye.com/blog/418574 按文中第一种配置就ok了。
2.编辑数据库连接的java通用类JdbcConnection.java
/*
* @(#)JdbcConnection.java
*
* Copyright (c) 2010 AsiaSoft Company Limited. All Rights Reserved.
*/
package org.altic.jasperReports;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* Class description goes here.
*
* @version 1.00 2010-3-12
* @author Frank.wu
*/
public class JdbcConnection {
private String driver; // "oracle.jdbc.driver.OracleDriver";
private String connectString; // "jdbc:oracle:thin:@YOUR_ORACLE_HOST:1521:YOUR_SID";
private String user; // "YOUR_ORACLE_USER_NAME";
private String password; // "YOUR_ORACLE_PASSWORD";
public JdbcConnection() {
// loadPropertiesConnection();
}
public JdbcConnection(String driver, String connectString, String user,
String password) {
this.driver = driver;
this.connectString = connectString;
this.user = user;
this.password = password;
}
public void loadPropertiesConnection() {
this.driver = "";
this.connectString = "";
this.user = "";
this.password = "";
try {
Properties props = new Properties();
InputStream resourceAsStream = getClass().getResourceAsStream(
"/connection.properties");
props.load(resourceAsStream);
this.setDriver(props.getProperty("driver"));
this.setConnectString(props.getProperty("connectString"));
this.setUser(props.getProperty("user"));
this.setPassword(props.getProperty("password"));
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection getConnection() {
try {
// Change these settings according to your local configuration
Class.forName(this.getDriver());
Connection conn = DriverManager.getConnection(this
.getConnectString(), this.getUser(), this.getPassword());
return conn;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public String toString() {
return " Driver : " + this.getDriver() + " | " + " ConnectString : "
+ this.getConnectString() + " | " + " User : " + this.getUser()
+ " | " + " Password : " + this.getPassword();
}
public String getConnectString() {
return connectString;
}
public void setConnectString(String connectString) {
this.connectString = connectString;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
3.在Eclipse下面,将项目已导出jar包的形式导出,(方法是选择项目,右键-->export,然后next就可以了),在这里我的导出为iReport1.jar
4.将项目中相应的jar包都复制到 D:\lib文件夹下。在文章的末尾可以下载到。
5.iReport.php代码
setDriver("com.mysql.jdbc.Driver");
// url for mysql
$Conn->setConnectString("jdbc:mysql://localhost/aerp");
// mysql user name
$Conn->setUser("root");
// mysql password
$Conn->setPassword("root");
$jrm = new JavaClass("net.sf.jasperreports.engine.JasperRunManager");
$jrm->runReportToPdfFile(
"D:/report1.jasper",
"D:/report5.pdf",
new Java("java.util.HashMap"),
$Conn->getConnection()
);
} catch (JavaException $ex) {
$trace = new Java("java.io.ByteArrayOutputStream");
$ex->printStackTrace(new Java("java.io.PrintStream", $trace));
print "java stack trace: $trace\n";
}
?>
6.启动apache....
7.http://localhost/demo/iReport.php 即可,在D:/下就可以看到生成了一个pdf报表。
我也是这几天反复尝试再弄出来,各位如果不理解之处,可以联系本人。wushexin2006@126.com