初めての質問。とにかく、私にはちょっとしたプロジェクトがあります。基本的には、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));
お手伝いありがとう!