我试图从Java程序中调用一些PHP脚本,以便查询/修改我的数据库。我对数据库完全陌生,不知道发生了什么。
我试图从java中调用以下脚本。
脚本
<?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代码
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应该是什么??
发布于 2013-02-07 17:36:33
如果您想要调用一个新进程,请参阅java.lang.Process。
但是,Java能够通过JDBC直接与数据库对话,这是我强烈推荐的方法。您不必使用多种语言生成新的流程或实现您的解决方案,您还可以使用许多与JDBC相关的框架来重新使用。数据库访问要容易得多。
https://stackoverflow.com/questions/14757351
复制相似问题