实战 php调用java类由java类读数据库完成相关操作(Inber Write)_PHP
2 编译DataBaseConnection.java文件 将其编译文件放入到PHP支持的类文件目录中
package com.inberkong.util;
/*******************************************************
* Programe File :复件缓冲池DataBaseConnection.java
* Creat Date :3/28/2005(MM-DD-YYYY)
* Creat By :InberKong(GongYingBin)
* LastModifyDate :3/28/2005(MM-DD-YYYY)
* ModifyBy :InberKong(GongYingBin)
* Function :Get the Oracle DataBaseConnectionPool
//一个用于查找数据源的工具类。
*******************************************************/
import java.sql.*;
//连接数据库的工具类。
public class DataBaseConnection
{
/**
*一个静态方法,返回一个数据库的连接。
*/
public static Connection getConnection()
{
Connection con=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:photo","photo","inberkong");//数据源连接信息
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}
3 编译读数据表的处理类同样将处理类放入PHP调用的Java类目录
这是一个读取用户角色表 返回用户与角色间关系的JS数组的处理类 GetUserRolseArray.java
注:读程序时不需要关心相关的表结构,可只关心整个调用的处理过程。
package com.inberkong.processBean;
import java.lang.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import com.inberkong.util.DataBaseConnection;
public class GetUserRolseArray
{
private Connection conss;
private Statement stmtsss=null;
private Statement stmt2=null,stmt3=null;
private ResultSet rstsss=null;
private ResultSet rst2=null,rst3=null;
private String sqlss="";
private String sql2="",sql3="";
private String rString="";
private String ID,ID2;
private String NAME;
private int i=0;
public GetUserRolseArray()
{
try
{
this.conss=DataBaseConnection.getConnection();
}
catch(Exception e)
{
}
//DataBaseConnection.getConnection();
}
protected void finalize() throws Throwable
{
//
//super.finalize();
//System.out.println("inberkong");
closeCON();
}
public String getUserRolseArray() throws Exception
{
//exePtempfunction();
sqlss="SELECT ID,NAME FROM ADMIN WHERE 1=1 AND STATE!=0 AND USERTYPE!='0'
ORDER BY ID DESC";
//外部用户不参于后加角色管理
stmtsss=conss.createStatement();
rstsss=stmtsss.executeQuery(sqlss);
while (rstsss.next())
{
ID=rstsss.getString("ID");
ID2=ID;
NAME=rstsss.getString("NAME");
rString=rString "\n" "arr[" i "]=new BDSort
('" ID "','00','" NAME "','');";
i ;
sql3="select ID,NAME from ROLE_MANAGE where ID in( select ROLSEID
from USER_ROLSE_LINK where USERID=" ID2 ") order by ID asc ";
stmt3=conss.createStatement();
rst3=stmt3.executeQuery(sql3);
while(rst3.next())
{
ID=rst3.getString("ID");
NAME=rst3.getString("NAME");
rString=rString "\n" "arr[" i "]=new BDSort
('" ID "','" ID2 "','" NAME "','y');";
i ;
}
sql2="select ID,NAME from ROLE_MANAGE where ID not in(select
ROLSEID from USER_ROLSE_LINK where USERID=" ID2 ") order by ID asc ";
stmt2=conss.createStatement();
rst2=stmt2.executeQuery(sql2);
while(rst2.next())
{
ID=rst2.getString("ID");
NAME=rst2.getString("NAME");
rString=rString "\n" "arr[" i "]=new BDSort
('" ID "','" ID2 "','" NAME "','n');";
i ;
}
}
rstsss=null;
return rString;
}
public static void main(String[] args){
//测试如下
GetUserRolseArray obj1 = new GetUserRolseArray();
try
{
System.out.println(obj1.getUserRolseArray());
}
catch(Exception e)
{
//System.out.println(e);
}
return;
}
public void closeStmtSSS()throws Exception
{
stmtsss.close();
}
public void closeCON() throws Exception
{
if(!(conss.isClosed()))
conss.close();
}
}
4 PHP调用
testjava.php
$b=new Java("com.inberkong.processBean.GetUserRolseArray");
$x=$b->getUserRolseArray();
echo $x;
?>
调用结果:
arr[0]=new BDSort('186','00','dp2','');
arr[1]=new BDSort('44','186','店铺管理员','y');
arr[2]=new BDSort('42','186','超级管理员','n');
arr[3]=new BDSort('43','186','公告管理员','n');
arr[4]=new BDSort('87','186','测试普通管理员','n');
arr[5]=new BDSort('106','186','普通超管','n');
arr[6]=new BDSort('185','00','dp1','');
arr[7]=new BDSort('44','185','店铺管理员','y');
arr[8]=new BDSort('42','185','超级管理员','n');
arr[9]=new BDSort('43','185','公告管理员','n');
arr[10]=new BDSort('87','185','测试普通管理员','n');
arr[11]=new BDSort('106','185','普通超管','n');
arr[12]=new BDSort('146','00','chayue','');
arr[13]=new BDSort('44','146','店铺管理员','y');
arr[14]=new BDSort('42','146','超级管理员','n');
arr[15]=new BDSort('43','146','公告管理员','n');
arr[16]=new BDSort('87','146','测试普通管理员','n');
arr[17]=new BDSort('106','146','普通超管','n');
arr[18]=new BDSort('145','00','temp2','');
arr[19]=new BDSort('87','145','测试普通管理员','y');
arr[20]=new BDSort('42','145','超级管理员','n');
arr[21]=new BDSort('43','145','公告管理员','n');
arr[22]=new BDSort('44','145','店铺管理员','n');
arr[23]=new BDSort('106','145','普通超管','n');
arr[24]=new BDSort('20','00','cnbadmin','');
arr[25]=new BDSort('42','20','超级管理员','y');
arr[26]=new BDSort('43','20','公告管理员','n');
arr[27]=new BDSort('44','20','店铺管理员','n');
arr[28]=new BDSort('87','20','测试普通管理员','n');
arr[29]=new BDSort('106','20','普通超管','n');
arr[30]=new BDSort('1','00','inber','');
arr[31]=new BDSort('106','1','普通超管','y');
arr[32]=new BDSort('42','1','超级管理员','n');
arr[33]=new BDSort('43','1','公告管理员','n');
arr[34]=new BDSort('44','1','店铺管理员','n');
arr[35]=new BDSort('87','1','测试普通管理员','n');