我通过SQL命令为“MyDbTest”连接创建了一个名为BOOK的表:
CREATE TABLE BOOK(ID INT,DESCRIPTION VARCHAR(20),UNITCOST VARCHAR(20),ISBN VARCHAR(20),NBOFPAGES INT);
我使用以下命令运行Derby服务器:“startNetworkServer.bat”
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
System.out.println("\n\n>>> Executing : " + Main.class.toString() + " <<<\n");
persistBook(new Book(new Long(5000), "H2G2", "Best IT Scifi Book", new Float(12.5), "1234-5678-5678",
new Integer(247)));
Book book = findBook(new Long(5000));
System.out.println("# " + book);
}
/**
* Gets a database connection
*/
static {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:derby://localhost:1527/MyDbTest;create=true", "app", "app");
}
/**
* Persists the book to the database
*/
private static void persistBook(Book book) throws SQLException {
String query = "INSERT INTO BOOK (ID, TITLE, DESCRIPTION, UNITCOST, ISBN, NBOFPAGE) VALUES (?, ?, ?, ?, ?, ?)";
try (PreparedStatement stmt = getConnection().prepareStatement(query)) {
stmt.setLong(1, book.getId().longValue());
stmt.setString(2, book.getTitle());
stmt.setString(3, book.getDescription());
stmt.setFloat(4, book.getUnitCost().longValue());
stmt.setString(5, book.getIsbn());
stmt.setInt(6, book.getNbOfPage().intValue());
stmt.executeUpdate();
}
}
}
我收到了错误:
线程“main”中的异常java.sql.SQLSyntaxErrorException: 表/视图'BOOK'不存在。
引起:ERROR 42X05:表/视图'BOOK'不存在。
我还阅读了这篇文章中提出的解决方案:Is it necessary to create tables each time you connect the derby database?。 不幸的是,他们都没有帮助我。
答案 0 :(得分:0)
出现此错误是因为我在SELECT语句中使用数据库名称而不是表名称。