从PHP执行java MySQL存储过程

时间:2022-09-20 22:34:25

I have a PHP page that processes incoming information, it takes the parameters and constructs a command to execute a Java class on the server using shell_exec().

我有一个处理传入信息的PHP页面,它接受参数并构造一个命令,使用shell_exec()在服务器上执行Java类。

The Java class takes the parameter and calls a stored procedure in MySQL and returns the required results. i have tested my Java class independently from the terminal and it is working perfectly.

Java类获取参数并在MySQL中调用存储过程并返回所需的结果。我已经从终端独立测试了我的Java类,它运行得很好。

My problem is that when I execute the Java class from PHP it runs until the line where I am defining the stored procedure calls and it stops there and returns NULL!

我的问题是,当我从PHP执行Java类时,它一直运行到我定义存储过程调用的行,它停在那里并返回NULL!

cSt = conn.prepareCall("{CALL prcName(?,?,?,?)}");

The are no exceptions while executing since the above line is in a try catch and no message from catch is printed.

执行时没有例外,因为上面的行在try catch中并且没有打印来自catch的消息。

The program just runs until this line and returns NULL!

程序只运行到这一行并返回NULL!

Here is the PHP page:

这是PHP页面:

<?php     
    $name= $_POST['name'];
    $contact= $_POST['contact'];

    $response =  shell_exec("java ClassName".$name." ".$contact);

    echo $response;
?>

Java class (part):

Java类(部分):

public static void main(String[] args) {
        //connection to database
        int returnValue = methodName(args[0], args[1]);
        System.out.print(String.valueOf(returnValue));
        //close connection to db
}
public static int methodName(String name,String contact){
        int result = 5; 
        try {
            cSt = conn.prepareCall("{CALL prcName(?,?,?)}");
            cst.CallableStament().setString("name",name);
            cst.CallableStament().setString("contact",contact);
            cst.CallableStament().registerOutParameter("rtn", java.sql.Types.INTEGER);
            cst.CallableStament().execute();

            result = cst.CallableStament().getInt("rtn");           
        } catch (SQLException e) {e.printStackTrace();}
        return result;
    }

UPDATE: i have narrowed down from where the problem was and finally found that while specifiying the driver by Class.forName("com.mysql.jdbc.Driver"); i get the error:

更新:我已经从问题所在的地方缩小,最后发现在通过Class.forName(“com.mysql.jdbc.Driver”)指定驱动程序时;我收到错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:195)
    at Classname.Connect(classname.java:29)
    at classname.

UPDATE 2: to be able to use the driver i added the CLASSPATH in /etc/profile/ however i noticed that the change is only for login users. But apache is using the user "www-data" while running the shell_exec() and i cant figure a way to assign the classpath to that user.

更新2:能够使用驱动程序我在/ etc / profile /中添加了CLASSPATH但是我注意到更改仅适用于登录用户。但是apache在运行shell_exec()时正在使用用户“www-data”,我无法想出一种方法来为该用户分配类路径。

1 个解决方案

#1


1  

You have to add the classpath to the executed command, or else set CLASSPATH before you run the PHP script.

您必须将类路径添加到已执行的命令,或者在运行PHP脚本之前设置CLASSPATH。

$response =  shell_exec("java -classpath mysql.jar ClassName".$name." ".$contact);

java -h

java -h

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
              A ; separated list of directories, JAR archives, # : separated list on Linux
              and ZIP archives to search for class files.

Oh, by the way, executing a shell command based on the contents of a web POST is super insecure. Someone could just POST ; rm -f / to wipe out your system, or other things to hack you.

哦,顺便说一句,基于Web POST的内容执行shell命令是超级不安全的。有人可以发帖; rm -f /消灭你的系统或其他东西来破解你。

#1


1  

You have to add the classpath to the executed command, or else set CLASSPATH before you run the PHP script.

您必须将类路径添加到已执行的命令,或者在运行PHP脚本之前设置CLASSPATH。

$response =  shell_exec("java -classpath mysql.jar ClassName".$name." ".$contact);

java -h

java -h

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
              A ; separated list of directories, JAR archives, # : separated list on Linux
              and ZIP archives to search for class files.

Oh, by the way, executing a shell command based on the contents of a web POST is super insecure. Someone could just POST ; rm -f / to wipe out your system, or other things to hack you.

哦,顺便说一句,基于Web POST的内容执行shell命令是超级不安全的。有人可以发帖; rm -f /消灭你的系统或其他东西来破解你。