简体   繁体   中英

Insert data into mysql table using java applets

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: 在此输入图像描述

AppletLayout: 在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM