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