找不到类型或命名空间名称“连接”

时间:2015-04-19 04:56:05

标签: c#

这里我为帐户数据库构建了两个类: 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();

    }

    }
}

1 个答案:

答案 0 :(得分:0)

您的班级名称是“帐户”,您正在制作“帐户”列表。将您的arraylists更改为通用List&lt;&gt ;.对于Connection,检查哪个命名空间定义了类,并将其放入,即导入该命名空间。

using namespace_where_Connection_is位于上方代码的顶部。

ArrayList<Account> custAccounts = new ArrayList();

更改为

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