- I installed a Derby DB and I opened a connection named "MyDbTest".
I created a table named BOOK for "MyDbTest" connection by the SQL command:
CREATE TABLE BOOK(ID INT, DESCRIPTION VARCHAR(20), UNITCOST VARCHAR(20), ISBN VARCHAR(20), NBOFPAGES INT);
I ran the Derby server by using the command: "startNetworkServer.bat"
- I ran the following code (using Open JPA):
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();
}
}
}
and I got an Error:
Exception in thread "main" java.sql.SQLSyntaxErrorException: Table/View 'BOOK' does not exist.
Caused by: ERROR 42X05: Table/View 'BOOK' does not exist.
I also read the solution proposed in this post: Is it necessary to create tables each time you connect the derby database?. Unfortunately, none of them helped me.
- I changed "jdbc:derby://localhost:1527/MyDbTest;create=true" to "jdbc:derby://localhost:1527/MyDbTest;create=false"
- I don't use "in-memory" configuration in Derby.
- I don't think I connect to the DB as another user (I'm not sure about it. How can I check it?).