0

我在将 java 连接到 mc 访问以检查用户身份验证时遇到问题,当我运行此程序时,它没有给我任何错误消息(我认为连接没有问题)但它也不会检查帐号是否和密码是否有效。其他方法,如提款、存款和 getBalance 工作正常。
任何帮助或提示将不胜感激。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class Logic
{
  private enum  State { ACCOUNT_NO, PASSWORD, PROCESSING };
  private State state = State.ACCOUNT_NO;
  private long  number;
  private long  acNumber = 0;
  private long  acPIN = 0;
  private char  op = ' ';
  private Bank  bank = new Bank();

  private JTextArea display1, display2;


  public Logic( JTextArea area1, JTextArea area2 )
  {
    display1 = area1; display2 = area2;
    display2.setText( "Welcome: Enter your account number" );
  }

  public void process( String button )
  {
    String info = null;

    if ( button.length() == 1 )
    {
      char c = button.charAt(0);
      if ( c >= '0' && c <= '9' )               // Digit
      {
        number = number * 10 + c-'0';           // Build number
        display1.setText( "" + number );
      }
      return;
    }

    if ( button.equals( "CLR" ) )
    {
      number = 0;
      display1.setText( "" + number );
    }

    if ( button.equals( "Ent" ) )
    {
      switch ( state )
      {
        case ACCOUNT_NO:
          bank.setAcNumber( number );
          number = 0;
          state = State.PASSWORD;
          display1.setText( "" );
          display2.setText( "Now enter your password" );
          break;
        case PASSWORD:
          bank.setAcPasswd( number );
          number = 0;
          display1.setText( "" );
          if ( bank.checkValid() )
          {
            state = State.PROCESSING;
            display2.setText( "Now enter transaction" );
          } else {
            state = State.ACCOUNT_NO;
            display2.setText( "Invalid: Start again" );
          }
          break;
        default :
      }
      return;
    }

    if ( state != State.PROCESSING ) 
    {
      state = State.ACCOUNT_NO;
      display2.setText( "But you are not loged in\n"  );
      display2.append( "Welcome: Enter your account number" );
      return;
    }

    if ( button.equals( "W/D" ) )               // Clear Result
    {
      display1.setText( "" );
      if ( bank.withdraw( number ) )
      {
        display2.setText( "Withdrawn: " + number );
      } else {
        display2.setText( "You do not have surficient funds" );
      }
      number = 0;
      return;
    }

    if ( button.equals( "Bal" ) )               // Clear Result
    {
      number = 0;
      display2.setText( "Your balance is: " + bank.getBalance() );
      return;
    }

    if ( button.equals( "Dep" ) )               // Clear Result
    {
      bank.deposit( number );
      display1.setText( "" );
      display2.setText( "Deposited: " + number );
      number = 0;
      return;
    }

    if ( button.equals( "Fin" ) )               // Clear Result
    {
      state = State.ACCOUNT_NO;
      number = 0;
      display2.setText( "Welcome: Enter your account number" );
      return;
    }

    return;
  }

  public long getResult()
  {
    return number;
  }

  public long getNumber()
  {
    return number;
  }
}

银行类。

import java.sql.*;

class Bank
{
  long AcNumber = 0;
  long AcPasswd = 0;
  long balance = 0;
  long deposit = 0;
  long withdraw = 0;
  long amount = 0;
  Connection con;
  Statement st;
  ResultSet rs;


  public void connect()
  {
     try
     {
     String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
     Class.forName(driver);
     String db = "jdbc:odbc:db3";
     con = DriverManager.getConnection(db);
     st = con.createStatement();
     }

     catch (Exception ex)
     {
         System.err.println("Can not load JDBC/ODBC driver.");
         System.exit( -1 ); 
     }

  }

  public void setAcNumber( long accNumber ) 
    {
         AcNumber = accNumber;
    }

  public void setAcPasswd( long accNumber ) 
    { 
         AcPasswd = accNumber;
    }

  public boolean checkValid() 
    {    
       try {
           String sql = "select user,pass from Table1 where user='"+AcNumber+"'and pass='"+AcPasswd+"'";
           rs = st.executeQuery(sql);

           int count = 0;
           while(rs.next())
            {
                count = count + 1;
            }
           if(count == 1)
           {
               System.out.println("User Found, Now enter transaction");

           }
           else
           {
               System.out.println("user not found");

           } 

        }

          catch (Exception ex)
           {

           }

       return true;

    }

  public boolean withdraw( long amount ) 
     {        
      balance =  balance - amount;    
      System.out.println( "Bank: Withdrawm " + amount ); 
      return true;   
     }

  public void deposit( long amount ) 
     {    
      balance =  balance + amount;            
      System.out.println( "Bank: deposit " + amount ); 
      return; 
     }

  public long getBalance() { 

      System.out.println( "Bank: Balance: "+ balance );
      return balance;
  }




}
4

1 回答 1

1

您可能没有错误消息,因为当您在 checkValid. 添加ex.printStackTrace()到您的 catch 块中以获取有关正在发生的事情的更多信息。

于 2012-05-03T23:17:07.137 回答