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();
}
}
}