Home >Backend Development >PHP Tutorial >Practical PHP calls the java class and the java class reads the database to complete related operations_PHP tutorial
1 Establish the system ODBC data source photo (this time I am using Oracle 9i, please contact me if you don’t understand the detailed configuration)
2 Compile the DataBaseConnection.java file and put the compiled file into the class file directory supported by PHP
package com.inberkong.util;
/***************************************************** ***
* Programe File: Copy buffer pool 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
//A tool class for finding data sources.
*************************************************** *****/
import java.sql.*;
//Tool class for connecting to the database.
public class DataBaseConnection
{
/**
*A static method that returns a database connection.
*/
public static Connection getConnection()
{
Connection con=null;
Try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:photo","photo","inberkong");//Data source connection information
}
catch(Exception e)
{
e.printStackTrace();
}
Return con;
}
}
3 Compile the processing class for reading the data table and put the processing class into the Java class directory called by PHP
This is a processing class that reads the user role table and returns a JS array of the relationship between users and roles GetUserRolseArray.java
Note: When reading the program, you do not need to care about the related table structure, you can only care about the entire call processing process.
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";
//External users do not participate in post-added role management
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();