-1

第一次提问。无论如何,我有一个项目,基本上它让我使用 Netbean 的 GUI 构建器编写一个 GUI,以从客户那里获取信息,然后将该信息发送到 PHP 脚本中。我知道我可以从 jTextField1.gettext(); 这一切都很好,但是问题在于将特定信息发送到 PHP 脚本,该脚本会将其发送到组网站中的 mySQL 数据库。

在提交操作方法中调用理论上应该可以工作的方法的 Java 编码:

public void Dsend() throws Exception {
URL hp = new URL("http://www.luxuryparking.comeze.com/DBinput.php"); 
HttpURLConnection hpCon = (HttpURLConnection) hp.openConnection();
hpCon.setRequestMethod("POST");
hpCon.setDoOutput(true);
hpCon.setDoInput(true);

        // important: get output stream before input stream
PrintStream ps = new PrintStream(hpCon.getOutputStream());
ps.print(jTextField7.getText());
     ps.print("secondKey=secondValue;");

// we have to get the input stream in order to actually send the request
hpCon.getInputStream();

// close the print stream
ps.close();   
}

PHP 脚本:

$conn = new PDO("mysql:host=$host;dbname=a4335408_data1", $username, $password);

foreach ($_POST as $key => $value) {
switch ($key) {
    case 'license':
        $license = $value;
        break;
    default:
        break;
}
}

$license='1234567';

$sql = "INSERT INTO Reservation (LicensePlate) VALUES (:license)";

$q = $conn->prepare($sql);

$q->execute(array(':license'=>$license));

感谢您的帮助!

4

2 回答 2

0
$q->execute(array(':license'=>$license));

应该

$q->execute(array('license'=>$license));

doInput(false)在没有 getInputStream的 Java 方面似乎更合适。Apache 的 HttpClient 可能是一个更有帮助的 API。

于 2013-06-14T13:11:25.993 回答
0

在java程序中,发送到服务器的数据应该是“application/x-www-form-urlencoded”的格式。

另请参阅:如何使用 HttpURLConnection POST 数据到 Web 服务器?

于 2013-06-14T13:14:38.327 回答