我正在运行需要连接到我的数据库的应用程序。这是代码:
package edu.depauw.csc480.dao;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.derby.jdbc.EmbeddedDriver;
import edu.depauw.csc480.model.CartItem;
import edu.depauw.csc480.model.Customer;
import edu.depauw.csc480.model.ShoppingCart;
public class DatabaseManager {
private Driver driver;
private Connection conn;
private CartItemDAO cartItemDAO;
private CustomerDAO custDAO;
private ShoppingCartDAO scDAO;
private final String url = "jdbc:derby:shoppingdb";
public DatabaseManager() {
driver = new EmbeddedDriver();
Properties prop = new Properties();
prop.put("create", "false");
// try to connect to an existing database
try {
conn = driver.connect(url, prop);
conn.setAutoCommit(false);
}
catch(SQLException e) {
// database doesn't exist, so try creating it
try {
prop.put("create", "true");
conn = driver.connect(url, prop);
conn.setAutoCommit(false);
create(conn);
}
catch (SQLException e2) {
throw new RuntimeException("cannot connect to database", e2);
}
}
cartItemDAO = new CartItemDAO(conn, this);
custDAO = new CustomerDAO (conn, this);
scDAO = new ShoppingCartDAO(conn, this);
}
private void create(Connection conn) throws SQLException {
CartItemDAO.create(conn);
CustomerDAO.create(conn);
ShoppingCartDAO.create(conn);
CartItemDAO.addConstraints(conn);
CustomerDAO.addConstraints(conn);
ShoppingCartDAO.addConstraints(conn);
conn.commit();
}
//Find row (by key) functions:
public ShoppingCart findSCart(int ssn) {
return scDAO.find(ssn);
}
public Customer findCust (int custID) {
return custDAO.find(custID);
}
public CartItem findCartItem (int cartItemID) {
return cartItemDAO.find(cartItemID);
}
//Insert row functions:
public ShoppingCart insertShoppingCart(int sCartID, int tPrice, int custID) {
return scDAO.insert(sCartID, tPrice, custID);
}
public Customer insertCustomer (int custID, String name, String address, String email) {
return custDAO.insert(custID, name, address, email);
}
public CartItem insertCartItem (int cartItemID, int sCartID, int productID, int quantity) {
return cartItemDAO.insert(cartItemID, sCartID, productID, quantity);
}
//Utility functions:
/**
* Commit changes since last call to commit
*/
public void commit() {
try {
conn.commit();
}
catch(SQLException e) {
throw new RuntimeException("cannot commit database", e);
}
}
/**
* Abort changes since last call to commit, then close connection
*/
public void cleanup() {
try {
conn.rollback();
conn.close();
}
catch(SQLException e) {
System.out.println("fatal error: cannot cleanup connection");
}
}
/**
* Close connection and shutdown database
*/
public void close() {
try {
conn.close();
}
catch(SQLException e) {
throw new RuntimeException("cannot close database connection", e);
}
// Now shutdown the embedded database system -- this is Derby-specific
try {
Properties prop = new Properties();
prop.put("shutdown", "true");
conn = driver.connect(url, prop);
} catch (SQLException e) {
// This is supposed to throw an exception...
System.out.println("Derby has shut down successfully");
}
}
/**
* Clear out all data from database (but leave empty tables)
*/
public void clearTables() {
try {
// This is not as straightforward as it may seem, because
// of the cyclic foreign keys -- I had to play with
// "on delete set null" and "on delete cascade" for a bit
cartItemDAO.clear();
custDAO.clear();
scDAO.clear();
} catch (SQLException e) {
throw new RuntimeException("cannot clear tables", e);
}
}
}
控制台抛出RunTimeException,因为我正在调用一个要插入到未创建的表中的对象(我的印象是它将通过调用上面的数据库管理器实例来创建)。
以下是要返回的错误:
Exception in thread "main" java.lang.RuntimeException: error: ShoppingCart not found
at edu.depauw.csc480.dao.ShoppingCartDAO.find(ShoppingCartDAO.java:73)
at edu.depauw.csc480.dao.ShoppingCartDAO.insert(ShoppingCartDAO.java:82)
at edu.depauw.csc480.dao.DatabaseManager.insertShoppingCart(DatabaseManager.java:80)
at edu.depauw.csc480.Test.main(Test.java:20)
Caused by: java.sql.SQLSyntaxErrorException: Table/View 'SHOPPINGCART' does not exist.
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement20.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement30.<init>(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedPreparedStatement40.<init>(Unknown Source)
at org.apache.derby.jdbc.Driver40.newEmbedPreparedStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.prepareStatement(Unknown Source)
at edu.depauw.csc480.dao.ShoppingCartDAO.find(ShoppingCartDAO.java:58)
... 3 more
Caused by: java.sql.SQLException: Table/View 'SHOPPINGCART' does not exist.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
... 17 more
Caused by: ERROR 42X05: Table/View 'SHOPPINGCART' does not exist.
at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindTableDescriptor(Unknown Source)
at org.apache.derby.impl.sql.compile.FromBaseTable.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.FromList.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.SelectNode.bindNonVTITables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bindTables(Unknown Source)
at org.apache.derby.impl.sql.compile.DMLStatementNode.bind(Unknown Source)
at org.apache.derby.impl.sql.compile.CursorNode.bindStatement(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown Source)
at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown Source)
at org.apache.derby.impl.sql.conn.GenericLanguageConnectionContext.prepareInternalStatement(Unknown Source)
... 11 more