java swing做的登陆界面,怎样判断数据库中用户名和密码?之前代码用户名和密码是定死的,现在想改成从数据库的表中读出Name和Password字段与文本框里输入的比较。。在网上差了很多,说要查询数据库,遍历第一个字段。。请问代码应该怎么改啊,本人基础差。。还请各位帮帮忙
原来的代码是:
package com.finance.ui;import java.awt.*;
import javax.swing.*;import java.awt.Rectangle;
import javax.swing.BorderFactory;
import com.finance.dao.*;
import com.finance.ui.LoginFrame;import com.finance.ui.LoginFrame.LoginFrame_btnOK_actionAdapter;
import com.finance.ui.MainUiFrame;import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*; 
public class LoginFrame extends JFrame {
public static void main(String[] args) {
      LoginFrame mainFrame = new LoginFrame();
      mainFrame.setVisible(true);
}
    public LoginFrame() {
        try {
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setResizable(false);
            this.setTitle("家庭理财-用户登录");
            this.setSize(400,220);
            Dimension frameSize = this.getSize();
            if (frameSize.height > screenSize.height) {
                frameSize.height = screenSize.height;
            }
            if (frameSize.width > screenSize.width) {
                frameSize.width = screenSize.width;
            }
            this.setLocation((screenSize.width - frameSize.width) / 2,
                             (screenSize.height - frameSize.height) / 2);            this.setVisible(true);        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }    private void jbInit() throws Exception {
        getContentPane().setLayout(null);
        pnlContainer.setBounds(new Rectangle(0, 0, 400, 220));
        pnlContainer.setLayout(null);
        lblUserName.setText("用户名:");
        lblUserName.setBounds(new Rectangle(105, 81, 55, 18));
        txtUserName.setBounds(new Rectangle(173, 81, 120, 18));
        lblPWD.setText("密 码:");
        lblPWD.setBounds(new Rectangle(105, 119, 55, 18));
        txtPWD.setBounds(new Rectangle(173, 119, 120, 18));
        jLabel1.setFont(new java.awt.Font("宋体", Font.BOLD, 18));
        jLabel1.setBorder(BorderFactory.createEtchedBorder());
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("欢迎使用家庭理财小秘书");
        jLabel1.setBounds(new Rectangle(77, 20, 235, 45));
        btnOK.setBounds(new Rectangle(91, 157, 81, 23));
        btnOK.setText("确定");
        btnOK.addActionListener(new LoginFrame_btnOK_actionAdapter(this));
        btnCancel.setBounds(new Rectangle(197, 157, 81, 23));
        btnCancel.setText("取消");
        this.getContentPane().add(pnlContainer);
        pnlContainer.add(jLabel1);
        pnlContainer.add(lblUserName);
        pnlContainer.add(lblPWD);
        pnlContainer.add(btnOK);
        pnlContainer.add(btnCancel);
        pnlContainer.add(txtUserName);
        pnlContainer.add(txtPWD);
    }
    public boolean getFlag(){
        return this.flag;
    }
    JPanel pnlContainer = new JPanel();
    JLabel lblUserName = new JLabel();
    JTextField txtUserName = new JTextField();
    JLabel lblPWD = new JLabel();
    JPasswordField txtPWD = new JPasswordField();
    JLabel jLabel1 = new JLabel();
    JButton btnOK = new JButton();
    JButton btnCancel = new JButton();
    private boolean flag=false;
    
    
    
    public void btnOK_actionPerformed(ActionEvent e) {
        if("jyf".equals(this.txtUserName.getText())&&"123".equals(new String(this.txtPWD.getPassword()))){
         System.out.println(this.txtUserName.getText()+this.txtPWD.getPassword());
            this.setFlag(true);
            
            
            boolean packFrame = false;
            MainUiFrame frame = new MainUiFrame();
            // Validate frames that have preset sizes
            // Pack frames that have useful preferred size info, e.g. from their layout
            if (packFrame) {
                frame.pack();
            } else {
                frame.validate();
            }            // Center the window
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension frameSize = frame.getSize();
            if (frameSize.height > screenSize.height) {
                frameSize.height = screenSize.height;
            }
            if (frameSize.width > screenSize.width) {
                frameSize.width = screenSize.width;
            }
            frame.setLocation((screenSize.width - frameSize.width) / 2,
                              (screenSize.height - frameSize.height) / 2);
            System.out.println(this.getFlag());
            
            if(this.getFlag()){
                frame.setVisible(true);
            }else{             }
        }else{
         System.out.println("密码错误!"+this.txtUserName.getText()+this.txtPWD.getPassword());            this.flag=false;
            JOptionPane.showMessageDialog(this,"用户名或密码错误","登录失败",
                                          JOptionPane.ERROR_MESSAGE);
            this.txtUserName.requestFocus();
            this.txtUserName.selectAll();
        }    }
    
public void setFlag(boolean flag) {
this.flag = flag;
}
class LoginFrame_btnOK_actionAdapter implements ActionListener {
    private LoginFrame adaptee;
    LoginFrame_btnOK_actionAdapter(LoginFrame adaptee) {
        this.adaptee = adaptee;
    }     public void actionPerformed(ActionEvent e) {
        adaptee.btnOK_actionPerformed(e);
    }
}
}数据库链接文件写好的
package com.finance.dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import java.sql.*; 
public class DataBase {


    private static Connection con = null;
    public static Connection getCon(){
        try {
            if (con == null) {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:finance", "sa", "sa");
            }
           
        
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return con;
    }
    }

解决方案 »

  1.   

    你先要写一个查询是SQL语句查找用户名和密码,然后在去获得窗体中的用户名和密码,最后在进行判断
      

  2.   

    能不能说的具体点,在哪里添加代码?
    public boolean loginByNameAndPassword(String name,String password){
      String sql = "select * from tbl_user where Name='"+name+"' and Password='"+password+"'";
      try{
       Connection connection = this.getCon();
       PreparedStatement pstat = connection.prepareStatement(sql);
       ResultSet rs = pstat.executeQuery();
       if(rs.next()){
        return true;
       }
      }catch(Exception e){
       System.out.println(e.getMessage());
       return false;
      }
      return false;
     }
    这个类添加到DataBase类里,然后if判断后要怎么改?具体代码是什么?
      

  3.   

    LZ,还是去看看基础的java视频教程吧。
      

  4.   


     你应该朝着这种思路去写:
      
         客户端 :就是一个用户名,密码的输入框。
     然后用一个类去封装。然后用objecOutputStrem 将这个对象发送到服务器
     端 , 得到那个类的用户名,密码,调用 你的 数据库语句,在进行判断