1

I have been given a college assignment to insert data into a mysql table using java applets. When I press the submit button, the command prompt throws a lot of exceptions(pic attached) and no data is inserted into the table.

Following is my code:

//Student registration Form (using applets and awt controls)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import java.util.*;

public class mysql4 extends Applet implements ActionListener{

    Label l1 = new Label("Roll No. : ");
    TextField t1 = new TextField("",10);
    Label l2 = new Label("Name : ");
    TextField t2 = new TextField("",20);
    Label l3 = new Label("Gender : ");
    CheckboxGroup radioGroup = new CheckboxGroup(); 
    Checkbox r1 = new Checkbox("Male", radioGroup, false); 
    Checkbox r2 = new Checkbox("Female", radioGroup, true);
    Label l4 = new Label("Hobbies : ");
    Checkbox c1 = new Checkbox("Sports");
    Checkbox c2 = new Checkbox("Cooking/Gardening");
    Checkbox c3 = new Checkbox("Music");
    Checkbox c4 = new Checkbox("Arts/Crafts");
    Label l5 = new Label("Course Opted : ");
    Choice l=new Choice();
    Label l6 = new Label("FeedBack : ",Label.CENTER);
    TextArea ta = new TextArea("",15,20);
    Button b1 = new Button("Submit");
    Button b2 = new Button("Reset");
    public void init() {
        l1.setAlignment(Label.CENTER);
        add(l1);
        add(t1);     
        add(l2);
        add(t2);    
        add(l3);
        add(r1);
        add(r2);
        add(l4);
        add(c1);
        add(c2);
        add(c3);
        add(c4); 
        add(l5);
        l.add("BCA");
        l.add("MCA");
        l.add("PGDCA");
        add(l);
        add(l6);
        add(ta);
        b1.addActionListener(this); 
        add(b1);
        b2.addActionListener(this);
        add(b2);
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==b1){
            try{
                String myDriver = "com.mysql.jdbc.Driver";
                String myUrl = "jdbc:mysql://localhost:3306/sonoo?useSSL=false";
                Class.forName(myDriver);
                Connection con = DriverManager.getConnection(myUrl, "root", "12345678");
                String query = "insert into details (RollNo, Name, Gender, Hobbies, Course, FeedBack)" + " values (?, ?, ?, ?, ?, ?)";
                PreparedStatement ps = con.prepareStatement(query);
                ps.setString (1, l1.getText());
                ps.setString (2, l2.getText());
                Checkbox chkr = radioGroup.getSelectedCheckbox();
                ps.setString (3, chkr.getLabel());
                Checkbox chk = radioGroup.getSelectedCheckbox();
                ps.setString (4, chk.getLabel());
                ps.setString (5, l.getSelectedItem());
                ps.setString (6, ta.getText());
                ps.execute();
                con.close();
            }catch(Exception ex){
                 ex.printStackTrace(); 
                 System.out.println(ex.getMessage());
            }
        }else if(e.getSource()==b2){
            t1.setText(" ");
            t2.setText(" ");
            r1.setState(false);
            r2.setState(true);
            c1.setState(false);
            c2.setState(false);
            c3.setState(false);
            c4.setState(false);
            l.select("BCA");
            ta.setText(" ");
        }else{}
    }
}

Output: enter image description here

AppletLayout: enter image description here

jotik
  • 17,044
  • 13
  • 58
  • 123
user3382203
  • 169
  • 1
  • 6
  • 25
  • looks like you have incorrect version of mysql jdbc driver. Do MySQL and mysql jdbc driver versions match ? –  Apr 25 '16 at 07:57
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. .. – Andrew Thompson Apr 26 '16 at 03:30
  • .. 3) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). 4) Don't post screen-shots of a DOS window! Instead copy/paste the text contained in it. The text will be less bytes and more useful to search engines. – Andrew Thompson Apr 26 '16 at 03:31

1 Answers1

0

You're getting an AccessControlException when the driver is trying to read the file.encoding property. The applet needs to be given this permission for the driver to work.

Kayaman
  • 72,141
  • 5
  • 83
  • 121