JDBC代码不会创建表

时间:2012-04-17 02:25:02

标签: java jdbc

目前正在尝试使用测试SQL运行将对象插入到Customer表中。但是,我运行测试的所有内容,编译器都返回以下异常:

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)

我一遍又一遍地查看代码,然而,我似乎无法找到错误,因为它编译得很好,只是没有运行。以下是我的创建方法&从我的CustomerDAO类中查找方法。

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

有关导致错误的原因的任何想法?

编辑:这是我的CustomerDAO.java类,它处理表的创建以及其他SQL命令。

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

}

2 个答案:

答案 0 :(得分:0)

错误表示无法找到表'Customer'。如果您的表名拼写错误,请检查您的数据库。也许您的连接有问题,请尝试从数据库中的任何其他表中检索数据。

答案 1 :(得分:0)

关键在于

Caused by: java.sql.SQLSyntaxErrorException: Table/View 'CUSTOMER' does not exist.
解决方法:

第一步,检查create方法和查询方法的连接是否与数据库相同。

第二步,调试你的代码。在执行方法时,在create方法中设置断点         然后检查表'CUSTOMER'是否创建。