实战 php调用java类由java类读数据库完成相关操作(Inber Write)_PHP
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
$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');

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools
