데이터베이스를 쿼리/수정할 수 있도록 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은 무엇을해야?
내가 당신을 생각 나게 자바 내에서 출력을 표시해야 가정 /localhost/yourPageInPhp.php
'와 같은 뭔가가 필요 "물어보기"버튼은 질문을하기위한 것입니다. 당신이 알고 싶은 것이? 또한, 당신이 기대하지 않는 한 당신의 [tag : php], [tag : sql], [tag : database] 태그는 전혀 관련이 없습니다 ... –
저를 상기시켜 주셔서 감사합니다! – TomSelleck