【问题标题】:JDBC Code Won't Create TableJDBC 代码不会创建表
【发布时间】:2012-04-17 02:25:01
【问题描述】:

当前尝试使用测试 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();
    }

}

【问题讨论】:

  • 您使用的 JDBC 连接 URL 是什么?具体来说,您连接的是哪个数据库用户,您是否验证了 CUSTOMERS 表实际上由该用户拥有/对该用户可见?
  • 这里是我使用的 URL: private final String url = "jdbc:derby:shoppingdb";但是,我不知道该放什么。有特定的约定吗?我模仿我在网上找到的另一段代码。
  • 最初是如何在数据库中创建表的?您是否验证过该表是否存在?
  • 我在一个单独的模型包(包含三个表)中手动编写了数据源。表存在,只是它们似乎没有创建。
  • 您是否检查过您的连接与您的Customer 表的架构相同?

标签: java jdbc


【解决方案1】:

关键在这里

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

解决方法:

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

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

【讨论】:

  • 是的,我明白了。但是,我想确切地知道为什么没有创建该表?我的 DAO 的创建功能看起来不错(复制了我的 CustomerDAO 类以供参考)。
【解决方案2】:

错误表明它找不到表“客户”。如果您可能拼错了表名,请检查您的数据库。也许您的连接有问题,请尝试从数据库中的任何其他表中检索数据。

【讨论】:

  • 您是否尝试从数据库中的任何其他表中获取数据?
  • 是的,同样的异常被抛出(TABLE 不存在)。我为上面的客户表添加了我的 DAO。
  • + "电子邮件字符串," 你忘了逗号
  • 你确定你的表是真的创建的吗?用查询浏览器检查一下。
猜你喜欢
  • 2011-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2012-01-10
相关资源
最近更新 更多