Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用swing中的java在jButton上执行特定的SQL查询_Java_Swing_Jbutton - Fatal编程技术网

如何使用swing中的java在jButton上执行特定的SQL查询

如何使用swing中的java在jButton上执行特定的SQL查询,java,swing,jbutton,Java,Swing,Jbutton,单击jButton时是否可以直接执行SQL查询 例如,在jDialog中,我有一个用于用户名的文本字段和一个savebutton 按钮动作: @Action public void saveNewUser() { Statement s = con.createStatement(); s.executeUpdate("INSERT INTO User (username) " + " VALUES ('"+ username + "')") } 因此,我需要从Username文本字段

单击jButton时是否可以直接执行SQL查询

例如,在jDialog中,我有一个用于用户名的文本字段和一个savebutton

按钮动作:

@Action
public void saveNewUser() {
  Statement s = con.createStatement();
  s.executeUpdate("INSERT INTO User (username) " + " VALUES ('"+ username + "')")
}

因此,我需要从Username文本字段获取字符串,但是如何获取呢?

您应该查看Swing API。textfield将是JTextField。以下是指向Java JDK API的链接:以下是直接指向JTextField的链接:

下面是一些非常简化的代码,可以满足您的需要。我忽略了布局、i18n和其他各种改进:

JTextField usernameTextField = new JTextField("Username:");
JButton saveButton = new JButton("Save");

saveButton.addActionListener(new ActionListener())
{
  public void actionPerformed(ActionEvent e)
  {
    String username = usernameTextField.getText();
    //Put validation code here if you want

    //Then put your SQL insert statement code here
  }
});

//Generally you will be adding them to a JPanel, but can be a JFrame for simple cases
panel.add(usernameTextField);
panel.add(saveButton);

...

您应该检查Swing API。textfield将是JTextField。以下是指向Java JDK API的链接:以下是直接指向JTextField的链接:

下面是一些非常简化的代码,可以满足您的需要。我忽略了布局、i18n和其他各种改进:

JTextField usernameTextField = new JTextField("Username:");
JButton saveButton = new JButton("Save");

saveButton.addActionListener(new ActionListener())
{
  public void actionPerformed(ActionEvent e)
  {
    String username = usernameTextField.getText();
    //Put validation code here if you want

    //Then put your SQL insert statement code here
  }
});

//Generally you will be adding them to a JPanel, but can be a JFrame for simple cases
panel.add(usernameTextField);
panel.add(saveButton);

...

是的,一切皆有可能。阅读swing教程以掌握框架。是的,一切皆有可能。阅读swing教程以掌握框架。