0
  1. I installed a Derby DB and I opened a connection named "MyDbTest".
  2. 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);

  3. I ran the Derby server by using the command: "startNetworkServer.bat"

  4. 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.

  1. I changed "jdbc:derby://localhost:1527/MyDbTest;create=true" to "jdbc:derby://localhost:1527/MyDbTest;create=false"
  2. I don't use "in-memory" configuration in Derby.
  3. I don't think I connect to the DB as another user (I'm not sure about it. How can I check it?).
CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
  • The process that you're going through to create the table is not clear. So hard to say. Maybe your program isn't connecting to the same db? Maybe different users and/or schemas? – BillRobertson42 Nov 18 '17 at 22:06
  • I thought about it. I typed in ij shell: "show connections" and I got: CONNECTION0* - jdbc:derby:MyDbTest * = current connection – CrazySynthax Nov 18 '17 at 22:09
  • 1
    Also, there's no JPA in the code. It's just JDBC. On top of that, the code doesn't close the database connection. That should be fixed. – BillRobertson42 Nov 18 '17 at 22:09
  • OK. Can you propose code that will close the DB ? – CrazySynthax Nov 18 '17 at 22:10
  • 1
    3 common reasons for "table does not exist" in Derby: https://stackoverflow.com/questions/22996818/is-it-necessary-to-create-tables-each-time-you-connect-the-derby-database/23051822#23051822 – Bryan Pendleton Nov 18 '17 at 22:54
  • 1
    Possible duplicate of [Is it necessary to create tables each time you connect the derby database?](https://stackoverflow.com/questions/22996818/is-it-necessary-to-create-tables-each-time-you-connect-the-derby-database) – BillRobertson42 Nov 19 '17 at 03:21
  • I read it. It didn't solve my problem. – CrazySynthax Nov 19 '17 at 07:24

1 Answers1

1

I had this error because I was using the database name in the SELECT statement instead of the name of the table.