欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

实战 php调用java类由java类读数据库完成相关操作

程序员文章站 2022-06-05 23:38:31
...
1 建立系统ODBC 数据源photo(本次使用的是Oracle 9i,详细配置如有不懂这处再和我联系)
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+" "+"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+" "+"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+" "+"arr["+i+"]=new BDSort

("+ID+","+ID2+","+NAME+",n);";
i++;
}





}

rstsss=null;
return rString;

}


public static void main(String[] args){

//测试如下
GetUserRolseArray obj1 = new GetUserRolseArray();