简体   繁体   English

从Java调用PHP

[英]Invoking PHP from Java

I am trying to invoke some PHP scripts from inside my Java program so I can query / modify my database. 我正在尝试从Java程序内部调用一些PHP脚本,以便可以查询/修改数据库。 I am totally new to databases and don't have a clue whats going on. 我对数据库是完全陌生的,没有任何线索。

I am trying to call the following script from within java. 我试图从Java中调用以下脚本。

Script 脚本

<?php
include('test.php');

define("HOST", "localhost");

define("USERNAME", "xxxx");

define("PASSWORD", "xxxx");

define("DATABASE", "project_database");

mysql_connect(HOST, USERNAME, PASSWORD);

mysql_select_db(DATABASE);

?>

Java code Java代码

 public static String excutePost(String targetURL, String urlParameters)
    {
      URL url;
      HttpURLConnection connection = null;  
      try {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
             "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Length", "" + 
                 Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");  

        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                    connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        //Get Response  
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer(); 
        while((line = rd.readLine()) != null) {
          response.append(line);
          response.append('\r');
        }
        rd.close();
        return response.toString();

      } catch (Exception e) {

        e.printStackTrace();
        return null;

      } finally {

        if(connection != null) {
          connection.disconnect(); 
        }
      }
    }

What should the target URL be?? 目标URL应该是什么?

If you want to invoke a new process, see java.lang.Process . 如果要调用新进程,请参见java.lang.Process

However Java is capable of talking to your database directly via JDBC and that's the approach I would strongly recommend. 但是Java能够直接通过JDBC与您的数据库通信,这是我强烈建议的方法。 You don't have to spawn a new process or implement your solution in more than one language, and you can make use of numerous JDBC-related frameworks to make your life re. 您不必使用一种以上的语言就可以产生新的流程或实现解决方案,并且可以利用众多与JDBC相关的框架来维持生活。 database access considerably easier. 数据库访问相当容易。

You need to specify the link of your page, could be something like '/localhost/yourPageInPhp.php 您需要指定页面的链接,可能类似于'/localhost/yourPageInPhp.php

I assume you need to execute the script and just display its output within Java 我假设您需要执行脚本并仅在Java中显示其输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM