title: Executing java MySQL stored procedure from PHP
body: <p>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 <code>shell_exec()</code>.</p>
  
  <p>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.</p>
  
  <p>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 <code>NULL</code>!</p>
  
  <pre><code>cSt = conn.prepareCall("{CALL prcName(?,?,?,?)}");
  </code></pre>
  
  <p>The are no exceptions while executing since the above line is in a <code>try catch</code> and no message from <code>catch</code> is printed.</p>
  
  <p>The program just runs until this line and returns <code>NULL</code>!</p>
  
  <p>Here is the PHP page:</p>
  
  <pre><code>&lt;?php     
      $name= $_POST['name'];
      $contact= $_POST['contact'];
  
      $response =  shell_exec("java ClassName".$name." ".$contact);
  
      echo $response;
  ?&gt;
  </code></pre>
  
  <p>Java class (part):</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>UPDATE: 
  i have narrowed down from where the problem was and finally found that while specifiying the driver by <code>Class.forName("com.mysql.jdbc.Driver");</code> 
  i get the error:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>UPDATE 2:
    to be able to use the driver i added the CLASSPATH in <code>/etc/profile/</code>
    however i noticed that the change is only for login users. But apache is using            the user "www-data" while running the <code>shell_exec()</code> and i cant figure a way to assign the classpath to that user. </p>