我被分配了一个大学作业,以便使用java小程序将数据插入mysql表。当我按下提交按钮时,命令提示符会抛出很多异常(已附加图片),并且表中没有数据插入。
以下是我的代码:
//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{}
}
}
输出:
AppletLayout:
最佳答案
驱动程序尝试读取AccessControlException
属性时,您将得到一个file.encoding
。小程序需要获得此权限才能使驱动程序正常工作。