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+"/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
<?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');
新聞熱點
疑難解答