dtk31564 2015-11-07 18:14
浏览 81
已采纳

从PHP执行java MySQL存储过程

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().

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.

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!

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.

The program just runs until this line and returns NULL!

Here is the PHP page:

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

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

    echo $response;
?>

Java class (part):

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:

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.

  • 写回答

1条回答 默认 最新

  • dongliang2005 2015-11-07 22:23
    关注

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

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改