PHP calls Java classes to do the related operations by the Java class Read database

Source: Internet
Author: User

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:复件缓冲池DataBaseConnection.java
*Creat  Date:3/28/2005(MM-DD-YYYY)
*CreatBy :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 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+ "
"+" 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+ "
"+" 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) {
  
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 ();
  
}
}

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.