title: Java Connect to MySQL via PHP-Script
body: <p>As im only able to use the DB from my Webhoster through a PHP-Script im wondering how im making the connection to the script workable</p>
  
  <p>PHP:</p>
  
  <pre><code>&lt;?php
  $query=$_POST["query"];
  echo" Query = ".$query."\n";
  if($query){
      $link=mysql_connect("localhost:3306","user","password");
      if($link){
          mysql_select_db("learnEng");
          $result=mysql_query($query);
          if($result){
              while($row=mysql_fetch_row($result)){
                  foreach($row as $value){ 
                      echo "\t".$value;
                  }
                  echo"\n";
              }
          }
      }else{
          echo " MySQL connect failed ! \n";
      }
  }else{
      echo " No Query ! \n";
  }
  exit();
  ?&gt;
  </code></pre>
  
  <p>Currently im making DB-Connections with Java like this (using H2):</p>
  
  <pre><code>Connection conn = null;
  
      String db = "~/testDB";
      String dbUser = "user";
      String dbPass = "password";
  
      Class.forName("org.h2.Driver");
      conn = DriverManager.getConnection("jdbc:h2:tcp://"+h2IP+"/" + db, dbUser, dbPass);
  </code></pre>
  
  <p>After some more research i found that code:</p>
  
  <pre><code>HttpURLConnection conn=null;
      try{
          URL url=new URL("http://www.example.net/database.php");
          String agent="Applet";
          String query="query=" + "create table test(name varchar(255), id int);";
          String type="application/x-www-form-urlencoded";
          conn=(HttpURLConnection)url.openConnection();
          conn.setDoInput(true);
          conn.setDoOutput(true);
          conn.setRequestMethod("POST");
          conn.setRequestProperty( "User-Agent", agent );
          conn.setRequestProperty( "Content-Type", type );
          conn.setRequestProperty( "Content-Length", ""+query.length());
  
          OutputStream out=conn.getOutputStream();
          out.write(query.getBytes());
          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String inputLine;
          while((inputLine=in.readLine())!=null){
              System.out.print(inputLine+"\n");
          };
          in.close();
          int rc = conn.getResponseCode();
          System.out.print("Response Code = "+rc+"\n");
          String rm=conn.getResponseMessage();
          System.out.print("Response Message = "+rm+"\n");
      }catch(Exception e){
          e.printStackTrace();
      }finally{
          conn.disconnect();
      }
  </code></pre>
  
  <p>After executing i get the following response with the php-Code from above in the Eclipse Console:</p>
  
  <pre><code>    Query = 
   No Query ! 
  Response Code = 200
  Response Message = OK
  </code></pre>
  
  <p>May someone be able to tell me what is going on and how to get it to work?</p>
  
  <p>Kind regards
  AMC</p>