Jeg lavede engang den her kode til applet-PHP-MySQL.
Du bruger så Java WebStart men jeg tror at forksellen er lille.
DatabaseApplet.java:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
public class DatabaseApplet extends Applet implements ActionListener {
TextArea select = new TextArea();
TextField f1 = new TextField();
TextField f2 = new TextField();
Button insert = new Button();
public void init() {
select.setColumns(40);
select.setRows(20);
select.setText(select());
f1.setColumns(10);
f2.setColumns(20);
insert.setLabel("Add");
insert.addActionListener(this);
add(select, null);
add(f1, null);
add(f2, null);
add(insert, null);
setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
insert(f1.getText(), f2.getText());
select.setText(select());
repaint();
}
private String select() {
StringBuffer res = new StringBuffer("");
try {
URL url = new URL("
http://localhost/select.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ( (line = br.readLine()) != null) {
res.append(line);
res.append("\r\n");
}
con.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return res.toString();
}
private void insert(String f1, String f2) {
try {
URL url = new URL("
http://localhost/insert.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
String info = "F1=" + f1 + "&F2=" + f2;
con.setDoOutput(true);
con.getOutputStream().write(info.getBytes());
con.connect();
if (con.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.err.println("POST failed");
}
con.disconnect();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
select.php:
<?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
$rs = mysql_query("SELECT * FROM T1") or die(mysql_error());
while ($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
print $row["F1"] . " " . $row["F2"] . "\n";
}
mysql_free_result($rs);
mysql_close($con);
?>
insert.php:
<?php
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("Test") or die(mysql_error());
$f1 = $_POST["F1"];
$f2 = $_POST["F2"];
mysql_query("INSERT INTO T1(F1,F2) VALUES($f1,'$f2')") or die(mysql_error());
mysql_close($con);
?>