import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class Login extends JFrame{
private static final int FRAME_WIDTH=300;
private static final int FRAME_HEIGHT=200;
private static final int FRAME_X_ORIGIN=150;
private static final int FRAME_Y_ORIGIN=250;
private static final int BUTTON_WIDTH=80;
private static final int BUTTON_HEIGHT=30;
private JTextField username; // 登录名输入框
private JPasswordField password;
private JLabel hint,name,pword;
/**
 * @param args
 */
public static void main(String[] args) {
Login login=new Login();
login.setVisible(true);
login.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// TODO Auto-generated method stub }

public Login(){
JPanel shuru1,shuru2,update,rupdate,
     controlPanel,buttonPanel;
JButton enterBtn,cancelBtn;
this.setResizable(false);

enterBtn=new JButton("确定");
enterBtn.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
cancelBtn=new JButton("取消");
cancelBtn.setSize(BUTTON_WIDTH, BUTTON_HEIGHT);

Container contentPane=getContentPane();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
    setTitle("系统登录");
setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
contentPane.setLayout(new GridLayout(4,1));
update=new JPanel(new BorderLayout());
rupdate=new JPanel();
hint=new JLabel("系统登录");
rupdate.add(hint);
update.add(rupdate,BorderLayout.CENTER);
contentPane.add(update);
shuru1=new JPanel();
shuru1.add(name=new JLabel("用户名"));
shuru1.add(username=new JTextField(10));
contentPane.add(shuru1);
shuru2=new JPanel();
shuru2.add(pword=new JLabel("密码"));
shuru2.add(password=new JPasswordField(10));
contentPane.add(shuru2);
controlPanel=new JPanel(new BorderLayout());
buttonPanel=new JPanel();
buttonPanel.add(enterBtn);
buttonPanel.add(cancelBtn);
controlPanel.add(buttonPanel,BorderLayout.SOUTH);
contentPane.add(controlPanel);
enterBtn.addActionListener( // 登录按钮事件处理
 new ActionListener() {
public void actionPerformed(ActionEvent evt) {
char[] pw = password.getPassword(); // 得到密码
String inputusername = username.getText();
String inputpassword = new String(pw);

Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;

String[] username=new String[100] ;//必须new才能使用
String[] password=new String[100] ;//
try {
 String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
  Class.forName(driverName).newInstance();
 String url = "jdbc:odbc:Database";
connection = DriverManager.getConnection(url, "sa", "19881004");

statement = null;
resultSet = null;
String strSql = null;
try {
strSql = "SELECT * FROM Mytable";  //用户表
statement = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery(strSql);
 for(int i=0;;i++)
if (resultSet.next()) {
//int id = resultSet.getInt("id");
username[i] = resultSet.getString("username");
password[i] = resultSet.getString("password");
//if (resultSet.next()) {
// NoticeBean bean = new NoticeBean(id, title, content);
//}自己修改的

 }
else break;
} catch (SQLException ex1) {
System.out.println("查询失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (resultSet != null) {
 resultSet.close();
resultSet = null;
              }
     if (statement != null) {
statement.close();
statement = null;
}
      if (connection != null) {
connection.close();
connection = null;
}
 } catch (SQLException ex) {  
 System.err.println(ex.getMessage());
}    }  
int i;
for(i=0;i<username.length;i++)
if (inputusername.equals(username[i])
&& inputpassword.equals(password[i])) 
break;

if(i<username.length){
 MainFrame archMan=new MainFrame();  
archMan.setVisible(true);

//在用户表中找到对应用户进入档案管理系统
dispose();
}
else{
JOptionPane.showMessageDialog(Login.this,"用户名或密码输入错误!", "Error!", 1);
}

} });
  cancelBtn.addActionListener( // 初始化按钮事件处理
   new ActionListener() {
public void actionPerformed(ActionEvent evt) { // dispose();
System.exit(0);
}
}); }}

解决方案 »

  1.   

    我的神啊.. 这样查表,这样匹配账号.很少见哦..看这个http://topic.csdn.net/t/20050419/14/3948952.html
      

  2.   

    1 只测试字母和数字的用户名和密码,不要包含中文看看
    2 在控制台,把拿到的用户名和密码打印出来,确认一下
    3 for(i=0;i<username.length;i++)
    if (inputusername.equals(username[i])
    && inputpassword.equals(password[i])) 
    break;这段代码,必要时你可以把每个比对过程都打印出来,看看到底有没有匹配的
    System.out.println(inputusername+" VS "+ username[i]);
      

  3.   

    按老紫竹说的做后,username[i]和password[i]都得到值了,但把“System.out.println(inputusername+" VS "+ username[i]);”这段代码加到“for(i=0;i<username.length;i++)
    if (inputusername.equals(username[i])
    && inputpassword.equals(password[i]))  
    break;”这段之后,编译器就报出如下问题:
    Login [Java Application]
    Login at localhost:1356
    Daemon Thread [AWT-Windows] (Running)
    Thread [AWT-Shutdown] (Running)
    Thread [AWT-EventQueue-0] (Suspended (exception ArrayIndexOutOfBoundsException))
    EventDispatchThread.run() line: 156
    Thread [DestroyJavaVM] (Running)
    Thread [AWT-EventQueue-0] (Running)
    F:\eclipse\eclipse\JRE\bin\javaw.exe (2010-7-23 下午02:43:22)
    小弟看不懂啊,请大家多多指点,小弟感激不尽!
      

  4.   

    我将“for(i=0;i<username.length;i++)
    if (inputusername.equals(username[i])
    && inputpassword.equals(password[i]))  
    break;”这段代码改成“
    for( i=0;i<username.length;i++)
    System.out.println(username[i]+" VS "+ password[i]);

    之后,我发现控制台输出:
    panliuhua   VS 19881004 
    panguangjun   VS 19510501  
    hepeifen  VS 19510614  
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null
    null VS null