title: Insert data into mysql table using java applets
body: <p>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. </p>
  
  <p>Following is my code:</p>
  
  <pre><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{}
      }
  }
  </code></pre>
  
  <p>Output:
  <a href="https://i.stack.imgur.com/oTgd5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oTgd5.png" alt="enter image description here"></a></p>
  
  <p>AppletLayout:
  <a href="https://i.stack.imgur.com/e7YYh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/e7YYh.png" alt="enter image description here"></a></p>