0

i've a problem connecting java to mc access to check a user authentication, when i run this program it doesn't give me any error message(i presume there's no problem with connection) but it also doesn't check whether the account number and password are valid or not. The others method like withdraw, deposit and getBalance are working fine.
Any help or hint will be greatly appreciated.

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;
  }
}

Bank class.

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;
  }




}
1
  • Awful code. I wouldn't write it this way. Start by learning the Java coding standards. Your data member names should be lower case. Upper case is the .NET standard, I believe. Commented May 3, 2012 at 23:18

1 Answer 1

1

You probably don't have an error message because you do nothing when you catch an exception in checkValid. Add ex.printStackTrace() inside your catch block to have more information on what's happening.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.