我正在尝试使用一个按钮将多个 TextField 插入到我的数据库中。我可以通过以下方式将原始字符串插入数据库:在一些建议之后,我做了一些更改。
public class DataEntry extends MySQLclass {
static Label jNaam = new Label("Naam"); static TextField lNaam = new TextField(30);
static Label jEmail =new Label("E-Mail"); static TextField lEmail=new TextField(30);
static Label jAdres =new Label("Adres"); static TextField lAdres =new TextField(30);
static Label jPlaats =new Label("Plaats"); static TextField lPlaats =new TextField(100);
static Label jPostcode =new Label("Postcode"); static TextField lPostcode =new TextField(6);
static Label jTelefoon =new Label("Telefoon");static TextField lTelefoon =new TextField(13);
static Label jBedrijfsNaam =new Label("Bedrijfsnaam"); static TextField lBedrijfsNaam =new TextField(30);
public static void main(String[] args) throws Exception {
Frame frm=new Frame("DataEntry frame");
Label lbl = new Label("Please fill this blank:");
frm.add(lbl);
frm.setSize(850, 500);
frm.setVisible(true);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Panel p = new Panel();
Panel p1 = new Panel();
p.setLayout(new GridLayout(100,1));
p.add(jNaam); p.add(lNaam);
p.add(jEmail); p.add(lEmail);
p.add(jAdres); p.add(lAdres);
p.add(jPlaats); p.add(lPlaats);
p.add(jPostcode); p.add(lPostcode);
p.add(jTelefoon); p.add(lTelefoon);
p.add(jBedrijfsNaam); p.add(lBedrijfsNaam);
// Button Submit=new Button("Save");
//p.add(Submit);
p1.add(p);
frm.add(p1,BorderLayout.NORTH);
Button Submit = new Button("Save");
p.add(Submit);
Submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pushData();
}
});
}
public static void pushData() {
try {
preparedStatement.setString(1, lNaam.getText());
preparedStatement.setString(2, lEmail.getText());
preparedStatement.setString(3, lAdres.getText());
preparedStatement.setString(4, lPlaats.getText());
preparedStatement.setString(5, lPostcode.getText());
preparedStatement.setString(6, lTelefoon.getText());
preparedStatement.setString(7, lBedrijfsNaam.getText());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
http://i.imgur.com/U380tP3.jpg
这是我收到的新错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at DataEntry.pushData(DataEntry.java:66)
at DataEntry$2.actionPerformed(DataEntry.java:58)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)