使用JAVA的awt或者swing包编写代码实现以下功能。要求按“登录”按钮则以数据库“chat DB"(sql server 2000)的用户表”中取出用户名和用户密码进行验证,按“取消”按钮关闭窗口,并退出应用程序请高手给出代码,谢谢!!!

解决方案 »

  1.   

    我给出关键的代码:
    1.用listener监听button事件
    jButton2.addActionListener(new ActionListener{  
    public void actionPerformed(ActionEvent e) {
        QuerySQL(textField.getText());
      }
    }2.访问SQL的语句:
    import java.sql.*;
    Connection conn=null;
    ResulstSet re=null;
    Statement stat=null;
    try{
    Class.forName("sun.jdbc.odbe.JdbcOdebDriver");
    String url="jdbc:odbc:YourDB";
    conn=DriverManager.getConnect(url);
    String sql="select * from db_name";
    rs=statement.executeQuery(sql);
    while(rs.next()){
    for(int i=1;i<col;i++)           //此处的代码是为了取得一条记录的所有属性值,
                                     //所以用for。col是每条记录的属性值的个数
    String info=rs.getString(i);    //info记录了此条记录的此列属性的信息
    }
    }catch(Exception e){}
      

  2.   

    package util.gui.dlg;import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    import java.sql.*;
    import oracle.jdbc.driver.*;public abstract class LoginDialog extends JDialog implements ActionListener , KeyListener
    {
    // GUI elements
    private JTextField  userNameTF  = new JTextField(10);
    private JTextField  passwordTF  = new JPasswordField(10);

    private JButton okButton = new JButton( "确定" );
    private JButton cancelButton = new JButton( "取消" );

    // return value
    private boolean returnValue;
    private String userCode;

    public LoginDialog( JFrame f)
    {
    super( f , "登录窗口" , true );

    addListener();
    setGUI();
    }

    public boolean isLegalUser()
    {
    return returnValue;
    }
    public String getUserCode()
    {
    return userCode;
    }

    /**
     * implements KeyListener
     */
     public void keyPressed( KeyEvent e )
     {
      if( e.getKeyCode() == KeyEvent.VK_ENTER )
     {
      check();
     }
     }
     public void keyReleased( KeyEvent e )
     {
     }
     public void keyTyped( KeyEvent e )
     {
     }
    /**
     * implements ActionListener
     */
    public void actionPerformed( ActionEvent e )
    {
    Object o = e.getSource();
    if( o == okButton )
    {
    check();
    }
    if( o == cancelButton )
    {
    LoginDialog.this.dispose();
    System.exit( 0 );
    };

    }

    protected abstract boolean check( String userName , String password );
    //----------------------------------------------------------
    //
    private void setGUI()
    {
    JPanel mainPanel = new JPanel();
    this.getContentPane().add( mainPanel );

    GridLayout grid = new GridLayout( 3, 2 );
    mainPanel.setLayout( grid );

    mainPanel.add( new JLabel( "用户名:" ) );
    mainPanel.add( userNameTF );
    mainPanel.add( new JLabel( "密  码:" ) );
    mainPanel.add( passwordTF );
    mainPanel.add( okButton );
    mainPanel.add( cancelButton );

    pack();
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension d = tk.getScreenSize();
    int height = new Double( d.getHeight()/2 - getHeight()/2 ).intValue();
    int width = new Double( d.getWidth()/2  - getWidth()/2 ).intValue();

    setLocation( width , height );
    setVisible( true );
    }

    private void check()
    {
    if( isJTextFieldEmpty( userNameTF )  || isJTextFieldEmpty( passwordTF ) )
    return;
    else
    {
    String userName = userNameTF.getText();
    String password = passwordTF.getText();

    returnValue = check( userName , password );
    if( returnValue ) // login successful
    {
    userCode = userName;
    LoginDialog.this.dispose();
    }
    else // login failed
    {
    passwordTF.setText( "" );
    JOptionPane.showMessageDialog( this , "口令或密码错误!" );
    }
    }
    }
    private void addListener()
    {
    okButton.addActionListener( this );
    cancelButton.addActionListener( this );
    userNameTF.addKeyListener( this );
    passwordTF.addKeyListener( this );


    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    } private boolean isJTextFieldEmpty( JTextField tf)
    {
    String str = tf.getText();
    if( str.equals( "" ) )
    {
    JOptionPane.showMessageDialog( null , "文本框为空!" );
    //tf.setFocusable( true );
    return true;
    }
    return false;
    }

    public static void main( String[] args )
    {
    JFrame f = new JFrame();
    System.out.println( "sdfsdf" );
    //LoginDlg dlg = new LoginDlg(f);
    System.out.println( "sdfsdf" );

    }
    }
      

  3.   

    上面是我写的一个登录对话框父类,用的时候只要覆盖
    protected abstract boolean check( String userName , String password );
    函数就可以了,比如
    protected boolean check( String userName , String password )
    {
         //SQL语句验证
         return true;//return false
    }