2

我正在尝试创建一个 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 方法中。我不知道该放什么来验证登录的人是否是授权用户。

4

2 回答 2

2

您无法执行SQL 操作,Applet请将其更改为 Java 应用程序(扩展Frame/JFrame)。阅读这篇文章 - Applet 可以做什么和不能做什么

您必须SELECT使用 WHERE 子句编写语句。

String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";

编辑:

static在块中注册 JDBC 驱动程序

 static {
   try{
       Class.forName("com.mysql.jdbc.Driver");
   }catch(Exception ex) { 
       System.err.println(ex);
   }
 }

并在方法中编写代码actionPerformed

PreparedStatement stmt=null;
boolean isFound=false;
try{
   connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/USERS", "root", "root");
   String sql="SELECT userName FROM employees WHERE UserName=? and Password=?";
   stmt=connection.prepareStatement(sql);
   stmt.setString(1,txtUser.getText());
   stmt.setString(2,txtPass.getPassword());             
   resultSet=stmt.executeQuery();
   if(resultSet.next()){
     isFound=true;
   }
  //     
}catch(SQLException ex){
   System.err.println(ex);
}finally{
  if(stmt!=null){
      try{ 
         stmt.close();
      }catch(Exception ex) { /* */ }
  }
 if(connection!=null){
      try{ 
         connection.close();
      }catch(Exception ex) { /* */ }
  }
}
于 2012-07-12T06:29:51.500 回答
0

将您的代码更改为此

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getActionCommand().equals("OK")){
                     if(connect(txtUser.getText(), txtPass.getText())) {
                                // Valid
                     }
                     else {
                           //Invalid
                     }

            }
        }

        public boolean connect(String usr, String pwd){

            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(
                            "jdbc:mysql://localhost:3306/USERS", "root", "root");
                 PreparedStatement ps= connection.prepareStatement("SELECT lname, fname FROM employees where username=? and password =?");
                 ps.setString(1,usr);
                 ps.setString(2,pwd);

                resultSet = ps.executeQuery();

              if(resultSet.next()) {
                       return true;
              }

            } catch (Exception e) {
                e.printStackTrace();
            }

    return false;
        }
于 2012-07-12T06:35:28.127 回答