尝试使用Button将TextField插入数据库

时间:2013-08-13 09:58:06

标签: java sql button insert

我正在尝试使用按钮将多个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

enter image description here

这是我收到的新错误:

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)

2 个答案:

答案 0 :(得分:0)

 button_object.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    // TODO Auto-generated method stub
                    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();
                }
            }) ;

使用此.. ..

答案 1 :(得分:0)

我已经开始写了,所以我会发布它(我不是在这里创建gui,只是标签和文本字段)

class StackOverflow {
    PreparedStatement preparedStatement;
    Label jNaam;
    TextField lNaam;
    Label jEmail;
    TextField lEmail;
    Label jAdres;
    TextField lAdres;
    Label jPlaats;
    TextField lPlaats;
    Label jPostcode;
    TextField lPostcode;
    Label jTelefoon;
    TextField lTelefoon;
    Label jBedrijfsNaam;
    TextField lBedrijfsNaam;

public StackOverflow() {
    initGui();
}

private void initGui() {
    jNaam = new Label("Naam");
    lNaam = new TextField(30);
    jEmail = new Label("E-Mail");
    lEmail = new TextField(30);
    jAdres = new Label("Adres");
    lAdres = new TextField(30);
    jPlaats = new Label("Plaats");
    lPlaats = new TextField(100);
    jPostcode = new Label("Postcode");
    lPostcode = new TextField(6);
    jTelefoon = new Label("Telefoon");
    lTelefoon = new TextField(13);
    jBedrijfsNaam = new Label("Bedrijfsnaam");
    lBedrijfsNaam = new TextField(30);

    JButton save = new JButton("save");
    save.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            pushData();

        }
    });
}

private 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();
    }

}

}