Actual combat PHP Invoke Java class to complete related operations by Java class Read database (Inber Write)

Source: Internet
Author: User
Tags odbc

1 Establish the System ODBC data source photo (this time using Oracle 9i, detailed configuration if you do not understand this place and I will contact)
2 Compile the Databaseconnection.java file and put it into the PHP-supported class file directory

Package com.inberkong.util;
/*******************************************************
* Programe File: Complex 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 locating data sources.

*******************************************************/
Import java.sql.*;
A tool class that connects to a database.
public class DatabaseConnection
{
/**
* A static method that returns a connection to a database.
*/
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 The processing class that compiles and reads the data table also puts the processing class into the Java class directory of the PHP invocation
This is a processing class that reads the User Role table returns a JS array of relationships between the user and the role Getuserrolsearray.java
Note: The reading program does not need to care about the related table structure, can only care about the entire call 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 user does not participate in post plus role management
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+ ', ', ', ' +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) {

Test as follows
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 Calls

testjava.php

<?php
$b =new Java ("Com.inberkong.processBean.GetUserRolseArray");
$x = $b->getuserrolsearray ();
echo $x;
?>
Call Result:
Arr[0]=new bdsort (' 186 ', ' + ', ' dp2 ', ');
Arr[1]=new Bdsort (' 44 ', ' 186 ', ' Shop keeper ', ' Y ');
Arr[2]=new Bdsort (' 42 ', ' 186 ', ' super admin ', ' n ');
Arr[3]=new Bdsort (' 43 ', ' 186 ', ' Bulletin manager ', ' n ');
Arr[4]=new Bdsort (' 87 ', ' 186 ', ' Test General admin ', ' n ');
Arr[5]=new bdsort (' 106 ', ' 186 ', ' normal tube ', ' n ');
Arr[6]=new bdsort (' 185 ', ' + ', ' dp1 ', ');
Arr[7]=new Bdsort (' 44 ', ' 185 ', ' Shop keeper ', ' Y ');
Arr[8]=new Bdsort (' 42 ', ' 185 ', ' super admin ', ' n ');
Arr[9]=new Bdsort (' 43 ', ' 185 ', ' Bulletin manager ', ' n ');
Arr[10]=new Bdsort (' 87 ', ' 185 ', ' Test General admin ', ' n ');
Arr[11]=new bdsort (' 106 ', ' 185 ', ' normal tube ', ' n ');
Arr[12]=new bdsort (' 146 ', ' + ', ' Chayue ', ');
Arr[13]=new Bdsort (' 44 ', ' 146 ', ' Shop keeper ', ' Y ');
Arr[14]=new Bdsort (' 42 ', ' 146 ', ' super admin ', ' n ');
Arr[15]=new Bdsort (' 43 ', ' 146 ', ' Bulletin manager ', ' n ');
Arr[16]=new Bdsort (' 87 ', ' 146 ', ' Test General admin ', ' n ');
Arr[17]=new bdsort (' 106 ', ' 146 ', ' normal tube ', ' n ');
Arr[18]=new bdsort (' 145 ', ' + ', ' temp2 ', ');
Arr[19]=new Bdsort (' 87 ', ' 145 ', ' Test general manager ', ' y ');
Arr[20]=new Bdsort (' 42 ', ' 145 ', ' super admin ', ' n ');
Arr[21]=new Bdsort (' 43 ', ' 145 ', ' Bulletin manager ', ' n ');
Arr[22]=new Bdsort (' 44 ', ' 145 ', ' Shop keeper ', ' n ');
Arr[23]=new bdsort (' 106 ', ' 145 ', ' normal tube ', ' n ');
Arr[24]=new bdsort (' A ', ', ', ' cnbadmin ', ');
Arr[25]=new Bdsort (' 42 ', ' 20 ', ' super admin ', ' y ');
Arr[26]=new Bdsort (' 43 ', ' 20 ', ' Bulletin manager ', ' n ');
Arr[27]=new Bdsort (' 44 ', ' 20 ', ' Shop keeper ', ' n ');
Arr[28]=new Bdsort (' 87 ', ' 20 ', ' Test General admin ', ' n ');
Arr[29]=new bdsort (' 106 ', ' 20 ', ' normal tube ', ' n ');
Arr[30]=new bdsort (' 1 ', ', ', ' inber ', ');
Arr[31]=new bdsort (' 106 ', ' 1 ', ' normal tube ', ' y ');
Arr[32]=new Bdsort (' 42 ', ' 1 ', ' super admin ', ' n ');
Arr[33]=new Bdsort (' 43 ', ' 1 ', ' Bulletin manager ', ' n ');
Arr[34]=new Bdsort (' 44 ', ' 1 ', ' Shop keeper ', ' n ');
Arr[35]=new Bdsort (' 87 ', ' 1 ', ' Test General admin ', ' n ');



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.