-1

初めての質問。とにかく、私にはちょっとしたプロジェクトがあります。基本的には、Netbean の GUI ビルダーを使用して GUI を作成し、顧客から情報を取得してから、その情報を PHP スクリプトに送信します。jTextField1.gettext(); から文字列を取得できることはわかっています。それはそれでいいのですが、問題はその特定の情報を PHP スクリプトに送信することです。PHP スクリプトはそれをグループの Web サイトの 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」でフォーマットする必要があります。

参照: Web サーバーへの HttpURLConnection POST データの使用方法

于 2013-06-14T13:14:38.327 に答える