java - 身份验证——在java中使用户名和密码全局化

标签 java mysql

我想验证用户身份。 为此,我使用 mysql 连接。 代码如下:

 JTextField txt_Username= new JTextField();
 String Username;
 txt_Username.addFocusListener(new FocusAdapter() {
        @Override
        public  void focusLost(FocusEvent arg0) {
     {
         Username= txt_Username.getText();
     }
 });

同样有密码代码。 现在我希望此用户名和密码超出其本地范围,以便单击 JButton 将触发 sql 数据库并使用上述用户名和密码进行身份验证。

我对Java很幼稚。有人可以帮忙吗?

我这里有完整的代码。请我需要了解我哪里出了问题:

     import java.awt.EventQueue;


 public class Frame{

 private private JFrame frame1;
 private JTextField textFieldUserName;
 private JTextField txtPassword;

 public String textUserValue;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Frame window = new Frame();

                window.frame1.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @throws SQLException 
 */
public multipleFrame() throws SQLException {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @throws SQLException 
 */
private void initialize() throws SQLException {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JLabel lblNewLabel = new JLabel("");
    lblNewLabel.setIcon(new ImageIcon("C:\\sad.jpg"));
    lblNewLabel.setBounds(10, 36, 196, 187);
    frame.getContentPane().add(lblNewLabel);

    JLabel lbl_Password = new JLabel("PASSWORD");
    lbl_Password.setBackground(new Color(128, 128, 128));
    lbl_Password.setOpaque(true);
    lbl_Password.setBounds(217, 140, 75, 14);
    frame.getContentPane().add(lbl_Password);


    final Connection con =DriverManager.getConnection("jdbc:mysql://localhost:3306/schema","user","pwd");
    final JTextField textFieldUserName = new JTextField();
    textFieldUserName.addFocusListener(new FocusAdapter() {


        @Override
        public  void focusLost(FocusEvent arg0) {
             String textUserValue = textFieldUserName.getText();
             System.out.println(textUserValue+"  hi");
             String textUserValue1 =textUserValue;

        }
    });
    System.out.println(textUserValue+"  HI");

    textFieldUserName.setBounds(324, 79, 108, 20);
    frame.getContentPane().add(textFieldUserName);
    textFieldUserName.setColumns(10);
    System.out.println();

    JLabel label = new JLabel("USERNAME");
    label.setOpaque(true);
    label.setBackground(Color.GRAY);
    label.setBounds(216, 82, 75, 14);
    frame.getContentPane().add(label);

    txtPassword = new JTextField();
    txtPassword.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent arg0) {
             String textPasswordValue = txtPassword.getText();
             System.out.println(textPasswordValue+"  hi");
        }
    });
    txtPassword.setColumns(10);
    txtPassword.setBounds(324, 137, 108, 20);
    frame.getContentPane().add(txtPassword);

    JButton btnLogin = new JButton("LOGIN");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

        }
    });
    btnLogin.setBounds(262, 200, 91, 23);
    frame.getContentPane().add(btnLogin);

    PreparedStatement stmt1 = null;
                try {
                    stmt1 = con.prepareStatement("select username from LOGIN_TABLE where    username='"+textUserValue+"'");
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ResultSet result1 = null;
                try {
                    result1 = stmt1.executeQuery();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    while (result1.next()) {
                        String user_name_db=result1.getString("username");
                        System.out.println(user_name_db);
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    }
 }

最佳答案

将您的用户名变量变为公共(public)变量。并将其放在类内部,而不是任何方法中。

示例:

public String Username;

或者,如果您想保留其隐私,您可以为其创建一个 .getMethod

public String getUsername(){
    return Username;
}

关于java - 身份验证——在java中使用户名和密码全局化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24670567/

相关文章:

java - JTextPane 和 XHTMLRenderer : When to use which

java - 设计数据库连接池

java - 使用数组或列表在 JSP 中自动完成文本框

Java 异常基本原理

mysql - 从 hive 退出后无法重新启动 Hadoop 守护进程

mysql - 如何通过zerofill进行mysql分组?

mysql - MongoDB——子子子?

java - 重新绘制图形问题

java.lang.ClassNotFoundException : con. mysql.jdbc.Driver 1

javascript - 如何用sass为iphone 414px写断点?