import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.sql.*; public class InsertApplet extends Applet { TextField cof_name, id, price, sales, total; TextArea status = new TextArea("", 2, 30, TextArea.SCROLLBARS_NONE); public InsertApplet() { cof_name = new TextField(10); id = new TextField(3); price = new TextField(5); sales = new TextField(10); total = new TextField(10); Panel hlavny = new Panel(); Button insert = new Button("Insert"); hlavny.setLayout(new GridLayout(6, 2)); hlavny.add(new Label("Coffee name: ")); hlavny.add(cof_name); hlavny.add(new Label("Good number: ")); hlavny.add(id); hlavny.add(new Label("Price:")); hlavny.add(price); hlavny.add(new Label("Sales: ")); hlavny.add(sales); hlavny.add(new Label("Total: ")); hlavny.add(total); insert.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { status.append("test status : ok\n"); String url = "jdbc:mysql://bbase.intrak.upjs.sk:3306/test"; String query = "INSERT INTO coffees VALUES("+"'"+cof_name.getText()+"'"+", "+id.getText()+" ,"+price.getText()+" ,"+sales.getText()+" ,"+total.getText()+");"; try { Class.forName("com.mysql.jdbc.Driver"); } catch(Exception ex) { status.append("Can't find Database driver class: " + ex.getMessage()); return; } try { Connection con = DriverManager.getConnection(url, "test", "test"); Statement stmt = con.createStatement(); stmt.executeUpdate(query); stmt.close(); con.close(); status.append("Database updated\n"); }catch(SQLException e) { status.append("SQLException: " + e.getMessage() +"\n"); } } }); hlavny.add(insert); add(hlavny); add(status, BorderLayout.SOUTH); } public static void main(String args[]) { Frame f = new Frame("Insert into coffees"); InsertApplet insertApplet = new InsertApplet(); insertApplet.init(); insertApplet.start(); f.add(insertApplet); f.setSize(300, 250); f.show(); } }