我正在尝试创建一个 GUI 程序,简单的 LOGIN 维护。我有 4 个 gui 组件,即:jlabel、jtextfield、jpasswordfield 和 jbutton。到目前为止,这是我的代码:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.*;
public class BankGui extends JApplet implements ActionListener{
// GUI components
JLabel lblUser, lblPass;
JTextField txtUser;
JPasswordField txtPass;
JButton btnOk, btnClear;
// connections to MYSQL
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet resultSet = null;
//public static Scanner in = new Scanner(System.in);
public void init(){
Container c = getContentPane();
c.setLayout( new FlowLayout() );
lblUser = new JLabel( "Username: " );
c.add( lblUser );
txtUser = new JTextField( 10 );
c.add( txtUser );
lblPass = new JLabel( "Password:" );
c.add( lblPass );
txtPass = new JPasswordField( 10 );
c.add( txtPass );
btnOk = new JButton( "OK" );
btnOk.addActionListener( this );
c.add( btnOk );
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if( btnOk ){
}
}
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/USERS", "root", "root");
statement = connection.createStatement();
resultSet = statement
.executeQuery("SELECT lname, fname FROM employees");
} catch (Exception e) {
e.printStackTrace();
}
}
}
但我被困在方法 actionperformed 和我的 connect 方法中。我不知道该放什么来验证登录的人是否是授权用户。