0

Currently trying to insert an object into the Customer table using a test SQL run. However, everything I run the test, the compiler returns the following exception:

Exception in thread "main" java.lang.RuntimeException: error finding Customer
    at edu.depauw.csc480.dao.CustomerDAO.find(CustomerDAO.java:78)
    at edu.depauw.csc480.dao.CustomerDAO.insert(CustomerDAO.java:87)
    at edu.depauw.csc480.dao.DatabaseManager.insertCustomer(DatabaseManager.java:84)
    at edu.depauw.csc480.Test.main(Test.java:18)
Caused by: java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.
    at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
    at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)

I have looked at the code over and over again, however, I can't seem to find an error as it compiles fine, just doesn't run. Below is my create method & find method from my CustomerDAO class.

public Customer find(int custID) {
        if (cache.containsKey(custID)) return cache.get(custID);

        try {
            String qry = "select name from CUSTOMER where custID = ?";
            PreparedStatement pstmt = conn.prepareStatement(qry);
            pstmt.setInt(1, custID);
            ResultSet rs = pstmt.executeQuery();

            String name = rs.getString("name");
            String address = rs.getString("address");
            String email = rs.getString("email");

            rs.close();

            Customer cust = new Customer (this, custID, name, address, email);

            cache.put(custID, cust);
            return cust;
        } catch (SQLException e) {
            dbm.cleanup();
            throw new RuntimeException("error finding department", e);
        }
    }

    // Insert new CartItem & returns it if the key does not already exist.

    public Customer insert (int custID, String name, String address, String email) {
        try {
            // make sure that the deptid is currently unused
            if (find(custID) != null)
                return null;

            String cmd = "insert into CUSTOMER(custID, name, address, email) "
                       + "values(?, ?, ?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(cmd);
            pstmt.setInt(1, custID);
            pstmt.setString(2, name);
            pstmt.setString(3, address);
            pstmt.setString(4, email);

            pstmt.executeUpdate();

            Customer cust = new Customer(this, custID, name, address, email);

            cache.put(custID, cust);

            return cust;
        }
        catch(SQLException e) {
            dbm.cleanup();
            throw new RuntimeException("error inserting new department", e);
        }
    }

Any ideas on what is causing the error?

EDIT: Here is my CustomerDAO.java class, which handles the creation of the table along with other SQL commands.

public class CustomerDAO {

    private Connection conn;
    private DatabaseManager dbm;
    private Map<Integer, Customer> cache;

    public CustomerDAO (Connection conn, DatabaseManager dbm) {
        this.conn = conn;
        this.dbm = dbm;
        this.cache = new HashMap<Integer, Customer>();
    }

    static void create(Connection conn) throws SQLException {
        Statement stmt = conn.createStatement();
        String s = "create table CUSTOMER("
                + "name string, "
                + "custID string, "
                + "address string, "
                + "email string " 
                + "primary key(custID))";
        stmt.executeUpdate(s);
    }

    // Modifies CartItem table and adds foreign key constraints (tables need to already be created)

    //static void addConstraints(Connection conn) throws SQLException {
        //Statement stmt = conn.createStatement();
        //String s = "alter table Customer "
        //      + "foreign key(shoppingCartID) references shoppingCart(shoppingCartID)";
        //stmt.executeUpdate(s);
    //}

    //Finds CartItem object by primary key.

    public Customer find(int custID) {
        if (cache.containsKey(custID)) return cache.get(custID);

        try {
            String qry = "select name from CUSTOMER where custID = ?";
            PreparedStatement pstmt = conn.prepareStatement(qry);
            pstmt.setInt(1, custID);
            ResultSet rs = pstmt.executeQuery();

            String name = rs.getString("name");
            String address = rs.getString("address");
            String email = rs.getString("email");

            rs.close();

            Customer cust = new Customer (this, custID, name, address, email);

            cache.put(custID, cust);
            return cust;
        } catch (SQLException e) {
            dbm.cleanup();
            throw new RuntimeException("error finding department", e);
        }
    }

    // Insert new CartItem & returns it if the key does not already exist.

    public Customer insert (int custID, String name, String address, String email) {
        try {
            // make sure that the deptid is currently unused
            if (find(custID) != null)
                return null;

            String cmd = "insert into CUSTOMER(custID, name, address, email) "
                       + "values(?, ?, ?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(cmd);
            pstmt.setInt(1, custID);
            pstmt.setString(2, name);
            pstmt.setString(3, address);
            pstmt.setString(4, email);

            pstmt.executeUpdate();

            Customer cust = new Customer(this, custID, name, address, email);

            cache.put(custID, cust);

            return cust;
        }
        catch(SQLException e) {
            dbm.cleanup();
            throw new RuntimeException("error inserting new department", e);
        }
    }


    //Clears Data from CartItem Table

    void clear() throws SQLException {
        Statement stmt = conn.createStatement();
        String s = "delete from CUSTOMER";
        stmt.executeUpdate(s);
        cache.clear();
    }

}
TopChef
  • 43,745
  • 10
  • 28
  • 27
  • What's the JDBC connection URL you're using? Specifically, what database user are you connecting as and have you verified that the `CUSTOMERS` table is actually owned by/is visible to that user? – QuantumMechanic Apr 17 '12 at 02:31
  • Here is the URL I am using: private final String url = "jdbc:derby:shoppingdb"; However, I wasn't sure what to put here. Is there a particular convention? I modeled mine after another piece of code that I found on the net. – TopChef Apr 17 '12 at 02:35
  • How did the table get created in the database in the first place? Have you verified the table even exists? – QuantumMechanic Apr 17 '12 at 02:38
  • I wrote the Data Source manually, in a seperate model package (contains three tables). The tables exist, they just don't seem to be creating. – TopChef Apr 17 '12 at 02:42
  • have you checked that your connection is in the same schema that your `Customer` table is? – Luiggi Mendoza Apr 17 '12 at 02:57
  • How do I go about doing that? – TopChef Apr 17 '12 at 03:01
  • check this: http://stackoverflow.com/a/584617/1065197 – Luiggi Mendoza Apr 17 '12 at 03:22

2 Answers2

0

The error says that it can't find the table 'Customer'. Check your dababase if you maybe misspelled the table name. Maybe you have a problem with your connection, try retrieving data from any other table in your database.

0

The key is Here

Caused by: java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.

the solution ways :

first step, check the connection of create method and query method are same database or not.

second step, debug your code. Set a breakpoint in the create method, when the method executed
then check table 'CUSTOMER' is create or not .

Mark Yao
  • 408
  • 2
  • 6
  • Yes, I understand that much. However, i'd like to know exactly why that table isn't being created? My create function of my DAO seems sound (copied my CustomerDAO class for reference). – TopChef Apr 17 '12 at 02:52