2015-04-19 2 views
0

Здесь я построил два класса для учетной записи базы данных: http://puu.sh/hiYKZ/01e0da1578.pngТип или пространство имен имя «Connection» не может быть найдено

Для списка учетных записей, я получил ошибку:

Ошибка 6 Тип или пространство имен имя «Connection» не может быть найден (вы пропали без вести с помощью директивы или ссылка на сборку?)

public Connection acctsConnect(){ 
     try{ 
      Class.forName("C:\\ChattBankMDB.mdb"); 
     }catch(ClassNotFoundException e){ 
      Console.WriteLine("Error: " + e); 
     } 

     Connection connect = null; 

     try{ 
      connect = DriverManager.getConnection("C:\\ChattBankMDB.mdb"); 
     }catch(SQLException e){ 
      Console.WriteLine("Error: " + e); 
     } 

     return connect; 
    } 
    } 
} 
Список

счета:

 class AccountList 
    { 
     private static List<Accounts> custAccounts = new List(); 
    private String userID = ""; 


    public AccountList(){ 

    } 


    public AccountList(String userID){ 
     this.userID = userID; 
    } 


    public void setUserID(String userID){ 
     this.userID = userID; 
    } 


    public String getUserID(){ 
     return this.userID; 
    } 


    public void setCustAccounts(String custId) { 

     Connection connect = acctsConnect(); 
     Statement statement = null; 
     ResultSet result = null; 
     String sql = "SELECT acctNo FROM Accounts Where Cid = '" + custId + "';"; 

     try{ 
      statement = connect.createStatement(); 
      result = statement.executeQuery(sql); 


      while (result.next()){ 
       result.getRow(); 
       Account acct = new Account(result.getString("acctNo")); 
       custAccounts.add(acct);     
      } 
     } 

     finally { 
      connect.close(); 
     } 
    } 


    public List<Accounts> getCustAccounts(){ 
     return custAccounts; 
    } 


    public void clearAccounts(){ 
     this.custAccounts.clear(); 
    } 


    public Connection acctsConnect(){ 
     try{ 
      Class.forName("C:\\ChattBankMDB.mdb"); 
     }catch(ClassNotFoundException e){ 
      Console.WriteLine("Error: " + e); 
     } 

     Connection connect = null; 

     try{ 
      connect = DriverManager.getConnection("C:\\ChattBankMDB.mdb"); 
     }catch(SQLException e){ 
      Console.WriteLine("Error: " + e); 
     } 

     return connect; 
    } 
    } 
} 

Accounts:

class Accounts 
    { 
      private String acctNo; 
    private String custId; 
    private String type; 
    private double balance; 
    private String message; 

    /** 
    * No-Arg Constructor 
    */ 

     /** 
    *sets account number 
    *@param acctNo 
    */ 
    public void setAcctNo(String acctNo) { 
     this.acctNo = acctNo; 
    } 

    /** 
    *sets customer id 
    * @param custId 
    */ 
    public void setCustId(String custId) { 
     this.custId = custId; 
    } 

    /** 
    * sets account type 
    * @param acctType 
    */ 
    public void SetType(String type) { 
     this.type = type; 
    } 

    /** 
    * sets balance for the account 
    * @param balance 
    */ 
    public void setBalance(double balance) { 
     this.balance = balance; 
    } 

    /** 
    * returns account number 
    * @return String 
    */ 
    public String getAcctNo() { 
     return acctNo; 
    } 

    /** 
    * returns customer id 
    * @return String 
    */ 
    public String getCustId() { 
     return custId; 
    } 

    /** 
    * returns account type 
    * @return String 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * returns account balance 
    * @return double 
    */ 
    public double getBalance() { 
     return balance; 
    } 

    /** 
    * returns account information into string form 
    * @return String 
    */ 
    public String getAcct() { 
     return this.acctNo + " " + this.custId + " " + this.type + " " + this.balance; 
    } 

    /** 
    * returns message set by other methods in class 
    * @return String 
    */ 
    public String getMessage(){ 
     return this.message; 
    } 
    public void deposit(String acctNo, double depAmount) { 

     Connection connect = acctConnect(); 
     Statement statement = connect.createStatement(); 
     ResultSet result = null; 
     String sql = "Select balance From Accounts Where acctNO = '" + acctNo + "';"; 
     String update = null; 
     int updateSet = 0; 
     double balance = 0.00; 
     double newBalance; 

     result = statement.executeQuery(sql); 

     /* retrieves the balance of the current account */ 
     while (result.next()) { 
      balance = result.getDouble("Balance"); 
     } 

     /* updates the balance in this object and the database */ 
     newBalance = balance + depAmount; 
     update = "Update Accounts Set Balance = '" + newBalance + "' Where acctNO = '" + acctNo + "';"; 
     updateSet = statement.executeUpdate(update); 
     this.balance = newBalance; 

     /* closes connection */ 
     connect.close(); 
    } 
     public void withdraw(String acctNo, double withdrawal) { 

     Connection connect = acctConnect(); 
     Statement statement = connect.createStatement(); 
     ResultSet result = null; 
     String sql = "Select balance From Accounts Where acctNO = '" + acctNo + "';"; 
     String update = null; 
     int updateSet = 0; 
     double balance = 0.00; 
     double newBalance; 

     result = statement.executeQuery(sql); 

     /* gets balance of the current account from the database */ 
     while (result.next()) { 
      balance = result.getDouble("Balance"); 
     } 

     /* checks to see if the withdrawal amount is more than the balance 
     and if so the exception is thrown and an insufficient funds message is 
     set */ 
     if (balance < withdrawal){ 
      this.message = "Insufficcient Funds"; 
      throw new Exception(); 
     }else{ 

     /* sets new balance and updates the current database account balance */ 
     newBalance = balance - withdrawal; 
     update = "Update Accounts Set Balance = '" + newBalance + "' Where acctNo = '" + acctNo + "';"; 
     updateSet = statement.executeUpdate(update); 
     this.balance = newBalance; 

     } 

     /* closes connection to database */ 
     connect.close(); 
    } 
public void transfer(String fromAcct, String toAcct, double transfer) { 

     /* Call withdraw method on from account */ 
     withdraw(fromAcct, transfer); 

     /* deposit to to account */ 
     deposit(toAcct, transfer); 

     /* both methods close their own database connection so it is not necessary to do so here */ 
    } 

    /** 
    * Sets up new account for existing customers 
    * @param acctNo 
    * @param custID 
    * @param type 
    * @param balance 
    * @throws SQLException 
    */ 
    public void estabAccount(String custID, String type, double balance) { 
     Console.WriteLine(custID + " " + type + " " + balance); 
     /* Establish connection and update the database with a new row containing the new account info */ 
     Connection connect = acctConnect(); 
     Statement statement = connect.createStatement(); 
     String sql = "Insert Into accounts (Cid, Type, Balance) Values ('" + custID + "', '" + type + "', " + balance + ");"; 
     Console.WriteLine(sql); 
     /* Execute Query and close connection */ 
     statement.executeUpdate(sql); 
     connect.close(); 

    } 

    } 
} 
+0

Java или C#? выглядит как код Java для меня –

+1

Arraylist не является общим в C# –

ответ

0

Ваше имя класса Accounts и вы делаете список счета. Измените своих арраистов на общий список <>. Для соединения проверьте, какое пространство имен определено этим классом и помещено в i.e импортировать это пространство имен.

using namespace_where_Connection_is в верхней части приведенного выше кода.

ArrayList<Account> custAccounts = new ArrayList(); 

изменение

List<Accounts> custAccounts = new List<Accounts>(); 
+0

Привет, спасибо! Я взял 3 семестра java и должен был знать, что я бы испортил C# – Hiy

+0

. Ур ошибки решены? –

Смежные вопросы