2013-02-07 3 views
0

데이터베이스를 쿼리/수정할 수 있도록 Java 프로그램 내부에서 일부 PHP 스크립트를 호출하려고합니다. 나는 데이터베이스에 완전히 익숙하지 않고 계속 진행되고있다.Java에서 PHP 호출

자바에서 다음 스크립트를 호출하려고합니다.

스크립트

<?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); 

?> 

자바 코드

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(); 
     } 
     } 
    } 

대상 URL은 무엇을해야?

+1

내가 당신을 생각 나게 자바 내에서 출력을 표시해야 가정 /localhost/yourPageInPhp.php

'와 같은 뭔가가 필요 "물어보기"버튼은 질문을하기위한 것입니다. 당신이 알고 싶은 것이? 또한, 당신이 기대하지 않는 한 당신의 [tag : php], [tag : sql], [tag : database] 태그는 전혀 관련이 없습니다 ... –

+0

저를 상기시켜 주셔서 감사합니다! – TomSelleck

답변

3

새 프로세스를 호출하려면 java.lang.Process을 참조하십시오.

그러나 Java는 JDBC을 통해 데이터베이스에 직접 대화 할 수 있으며 그 방법을 강력히 권합니다. 새로운 프로세스를 생성하거나 솔루션을 두 가지 이상의 언어로 구현할 필요가 없으며 수많은 JDBC 관련 프레임 워크를 사용하여 자신의 삶을 재사용 할 수 있습니다. 데이터베이스 액세스가 상당히 쉬워졌습니다.

0

당신은 수, 페이지의 링크를 지정하는 당신이 스크립트를 실행하고 단지