2015-04-19 107 views
0

这里,我已经建了两个班的帐户数据库: http://puu.sh/hiYKZ/01e0da1578.png类型或命名空间名称“连接”找不到

对于帐户列表,我收到的错误:

错误6类型或命名空间名称“连接”找不到 (是否缺少using指令或程序集引用?)

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

帐户:

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

你的类名是帐户和你正在做的帐户列表。将您的阵列列表更改为通用列表<>。对于Connection,请检查哪个名称空间定义了该类,并将其放入即导入该名称空间。

​​位于上面的代码顶部。

ArrayList<Account> custAccounts = new ArrayList(); 

变化

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

嘿,谢谢!我已经采取了3个学期的Java,应该知道我会搞砸c# – Hiy

+0

你的错误得到解决? –

相关问题