-2

I have made a basic swing application to input data into MySQL server. It is for some reason not accessing the driver to connect to the database. Here is the code. Thanks in advance for all the answers

import javax.swing.*;
import java.awt.event.*;
import java.sql.*;

public class Action extends JApplet {

  public void init() {
  }

  public Action() {
    JButton button = new JButton("Click here");
    button.addActionListener(new EventHandler());
    add(button);
  }
}

public class EventHandler implements ActionListener{
  public void actionPerformed(ActionEvent e) {
    try{
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      String url = "jdbc:mysql://localhost:3306/testgui";
      Connection con= DriverManager.getConnection(url,"root", null);
      String str = JOptionPane.showInputDialog(null,"Enter type");
      String abc = JOptionPane.showInputDialog(null,"Enter number");
      Statement st= con.createStatement();
      st.executeUpdate("insert into tb1 values (null,'"+str+"',"+abc+")");      
    }
    catch(Exception e1){
      e1.printStackTrace();
    }   
  }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    do you get error messages? exceptions? – John3136 Jun 26 '12 at 06:43
  • also, you might want to add a finally clause to your try block, to close the statement and connection. – Morfic Jun 26 '12 at 06:48
  • @John 3136 I dont get any exceptions or messages. When i run this in eclipse it works fine but when i run it on a browser it doesn't add data in the MySQL server database – user1481851 Jun 27 '12 at 05:55

1 Answers1

3

please read What Applets Can and Cannot Do

Unsigned applets cannot perform the following operations:

  • They cannot access client resources such as the local filesystem, executable files, system clipboard, and printers.
  • They cannot connect to or retrieve resources from any third party server (any server other than the server it originated from).
  • They cannot load native libraries.
  • They cannot change the SecurityManager.
  • They cannot create a ClassLoader.
  • They cannot read certain system properties. See System Properties for a list of forbidden system properties.

maybe simple and possible way is look at Java Web Start, completed by @Andrew Thompson

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319