如何通过Java向MS Access添加记录

时间:2013-04-20 11:33:28

标签: java database

我试图将一行记录添加到我现有的MS数据库中。 基本上,这些记录将被输入到GUI中,然后在用户单击“提交”时添加到数据库中。

我使用3个JLabel,3个JTextfield和1个JButton创建了GUI。 用户需要输入名称,数量和价格,然后单击“提交”按钮。

JLabel newproductname = new JLabel("Enter Product Name");
JTextField npn = new JTextField(7);
JLabel newproductprice = new JLabel("Enter Product Price");
JTextField npp = new JTextField(7);
JLabel newproductstock = new JLabel("Enter Product Stock");
JTextField nps = new JTextField(7);
JButton addnewitem = new JButton("Add New Item");

这是我用来创建GUI的方法,显然我已将所有这些添加到下面的面板

我是Java的新手,所以我很欣赏任何人都能理解的术语,谢谢!

2 个答案:

答案 0 :(得分:0)

使用ActionListioner事件。当你点击按钮时,会触发一个事件,该事件将通过ActionListioner接口(event handling )处理,它有一个方法actionPerformed(ActionEvent)你需要给出这个方法的实现来处理你的事件。 I-E

class GUI implements ActionListener{
   JLabel newproductname = new JLabel("Enter Product Name");
JTextField npn = new JTextField(7);
JLabel newproductprice = new JLabel("Enter Product Price");
JTextField npp = new JTextField(7);
JLabel newproductstock = new JLabel("Enter Product Stock");
JTextField nps = new JTextField(7);
JButton addnewitem = new JButton("Add New Item");

void storeMethod(String npn, string npp, string nps){
   // some implementation 
}

public void actionPerformed(ActionEvent ae){
    storeMethod(npn.getText(), npp.getText(), nps.getText());
}

void drawForm(){
 // some implementation
addnewitem.addActionListener(this);
}

main(){
new GUI().drawForm();
}

}

这里有一个link,它可以帮助您与MS-Assess建立连接。 从这里您可以找到源代码link2

答案 1 :(得分:0)

要使用Java在MS Access中添加记录,您需要使用JDBC来插入记录。 Refer Here