如何从JPanel测试用户输入?

时间:2016-12-20 05:17:43

标签: java mysql swing jframe jtextfield

我正在尝试使用SQL(mySQL workbench)和Java(Eclipse)构建一个简单的登录数据库。我不确定如何针对SQL中的数据库测试字符串输入(JTextField和JPasswordField),甚至对任何其他字符串也是如此。

如果登录凭据等于指定的字符串,我希望Java打印一个语句。这是我目前的代码:

import javax.swing.*;
import java.sql.*;


public class Hello1 extends JFrame {




private static final long serialVersionUID = 1487932324102279819L;


public static void main(String[] args) {

    JFrame frame = new JFrame("Frame Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,200);



    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);

    String username0 = "java";
    String password = "password";

    Statement stmt = null;
    try{
        String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
        Connection connection = DriverManager.getConnection(url, username0, password);
        stmt = connection.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT * from userids ");
        while(rs.next()) {
            String user = rs.getString("username");
            String pass = rs.getString("paswrd");
            System.out.println(user);
            System.out.println(pass);
        }
        connection.close();

        }

        catch (Exception e) {
             System.err.println("Got an exception! ");
             System.err.println(e.getMessage());
        }
     }



private static void placeComponents(JPanel panel) {

    panel.setLayout(null);
    JLabel userLabel = new JLabel("Username");
    userLabel.setBounds(10,20,80,25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 20, 165, 25);
    panel.add(userText);
    String user = userText.getText();       

    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(10,50,80,25);
    panel.add(passwordLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(100,50,165,25);
    panel.add(passwordText);
    String pass = passwordText.getSelectedText();

    if(user.equals('b') && pass.equals('t')){
        System.out.println("Correct Login");
    }
    System.out.println(pass);

    JButton loginB = new JButton("Login");
    loginB.setBounds(10, 80, 80, 25);
    panel.add(loginB);


}

}

我尝试使用.getText()和.getSelectedText()方法获取每个userText / passwordText中的文本。

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么当您点击async按钮时需要输入密码。

初始化按钮后,请在Login方法中添加以下代码。

placeComponents

动作侦听器可能是最容易实现的 - 也是最常见的 - 事件处理程序。您可以实现一个动作侦听器来定义用户执行某些操作时应该执行的操作。

您可以在ActionListener here上阅读更多内容。

您可以参考this example 查看如何使用密码字段。

答案 1 :(得分:0)

您应该在准备好的语句中将用户名和密码作为参数传递并执行它,然后使用以下命令检查是否存在匹配:

public void login(String user,String pass){
    String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
    Connection connection = DriverManager.getConnection(url, username0, password);
    PreparedStatement stmt = connection.prepareStatement("SELECT * from userids where username=? and password=? ");
    stmt.setString(1, user);
    stmt.setString(2, pass);
    ResultSet rs= stmt.executeQuery();

    if(rs.hasNext()){
    //continue with operation
    }else{
    //show user doesn't exist
    }
}

然后将事件添加到按钮:

loginB.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
String user = userTextBox.getText().toString();
char[] pass = passwordText.getPassword();
            login(user,pass);
        }
    });