CRUD NetBeans Platform Application with Embedded JavaDB

If you guys have followed this tutorial: CRUD tutorial for NetBeans Platform 7.0, and tried to make it an Embedded JavaDB application based on the following tutorial: Embedded Database for NetBeans Platform CRUD, and you receive this Table/View does not exist. error message, you need to edit a persistence.xml file as follows.

After creating entity classes from database, edit a persistence.xml file by adding:

<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>

into the file within the existing <properties></properties> tags as shown below. (Notes: add the above line to persistence.xml before you create a dist folder).


<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:CustDB;create=true"/>
<property name="javax.persistence.jdbc.password" value="app"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value="app"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>

Next, you just do the “Clean and Build All” to create a “dist” folder containing a “.jar” file. The .jar file will then be used for the CustomerLibrary wrapper module.

In my understanding, the reason is that the create=true property in the connection string URL, jdbc:derby:CustDB;create=true, will only create a new database, but does not create tables.

In addition, when making a connection using Services tab, a new database is created in $NetBeans_Home/.netbeans/7.0/derby. However, when creating a database from running the application, the database is created in $Project_Home or DBManager folder (Click the Files tab to see a newly-created database).

That’s why we still see tables existed when using Services tab, but the application issues “Table/View does not exist.” error message.

Cheers….

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

16 Responses to CRUD NetBeans Platform Application with Embedded JavaDB

  1. stonercoder says:

    Greetings,
    Since Services points to $NetBeans_Home/.netbeans/7.0/derby and not to the database in the project’s folder, do you know a way to manipulate the tables (other than programmatically)? I mean via the services or some other tool?

    • mrtextminer says:
      1. On the Services tab, you make a connection to your database as usual. (The connection that has the following URL connection string, jdbc:derby:CustDB;create=true)
      2. Then, you click on the “APP” and “Tables” nodes to expand them, which will show you a list of database tables.
      3. Next, you make a right-click on any table to show its context menu.
      4. Finally, you can choose to do “View Data…” or “Execute SQL Command” If you use “View Data…”, you can manipulate tables using mouse clicks.
      • stonercoder says:

        What if the database exists and it is already in the project’s folder and not in the derby default. How can you specify another database directory? I am trying something like jdbc:derby:file:/home/user/dbname but it does not work. Is it not possible to modify an existing embedded database via netbeans?

      • mrtextminer says:

        Sorry. I don’t know about that. Maybe, you need to look at the Netbeans configuration.

    • Matt says:

      The solution to your problem is to go to the services tab and specify in the database url the exact file location. For instance if your database is created in the user’s application data when you run it in the debug mode, simply create a new connection and specify the jdbc url as the following:

      jdbc:derby:C:\Users\YOURUSERNAME\Documents\NetBeansProjects\YOURPROJECT\DATABASENAME

      don’t include the ;create=true however. Let your program do the creation if necessary.

      Not sure on Linux/Mac though. Probably very similar

    • aterehin says:

      Dear Stonercoder,
      have you found an answer to your question?
      “What if the database exists and it is already in the project’s folder and not in the derby default. How can you specify another database directory? I am trying something like jdbc:derby:file:/home/user/dbname but it does not work. Is it not possible to modify an existing embedded database via netbeans?”

      • aterehin says:

        Probably I’ve found the workaround here. You can use your Derby Network Driver to work with your DB instead of Embedded, but you can connect from your appl. via embedded. Stupid, but works

  2. David Moss says:

    Thank you so much for this article. I was about to give up on the NB Platform and embedded Derby after wrestling with this issue for days.

  3. Sanjay Singh says:

    Mr.TextMiner, the solution given by you to resolve the problem of ‘Table/view does not exist’ is not working..
    I had wasted about my one week working on java derby. I got success in using derby network driver but still failed to use derby EmbeddedDriver because of error “Table/View ‘TableName’ does not exist”. Please, give a solution..
    waiting for your rply…

    • mrtextminer says:

      I am not sure what you did. The only difference between my post and the tutorial is adding “property name” element for drop-and-create-tables to the persistence.xml file. Did you exactly follow the Netbeans IDE tutorial?

      • Sanjay Singh says:

        Hi, Mr.TextMinert
        Please help me…I am waiting for your rply..
        I think that I had exactly followed the Netbeans IDE tutorial.

  4. Sanjay Singh says:

    Sir, I am following you only.
    The steps I had taken are as follow:
    I had created a table ‘college’ in sample database provided by netbeans. Then, saved its grab structure.
    Maked a new Database with EmbeddedDriver of name “EmbDatabase;create=true” with user “APP” and password “app”. Then, recreate the table in EmbDatabase using the grab structure of college table. Then, create a Database Schema under persistence from NewFile wizard and a entity class from DatabaseSchema. Then, i had edit the persistence.xml file as described by you.
    I had also added the derby.jar and derbytools.jar in the library of my project.
    Still I am getting the error: Table/View ‘APP.COLLEGE’ does not exist.
    Here is the code:-

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    query = jTextField1.getText();
    try
    {
    ConnectMe();
    }
    catch(Exception e)
    {
    System.out.println(“exception in actionPerformed method of button 1 is: “+e);
    }
    }
    public void ConnectMe() throws Exception
    {
    try
    {
    Class.forName(“org.apache.derby.jdbc.EmbeddedDriver”).newInstance();
    Connection con = DriverManager.getConnection(“jdbc:derby:EmbDatabase;create=true”, “APP”, “app”);
    Statement stmt = con.createStatement();
    stmt.executeUpdate(query);
    stmt.close();
    con.close();
    DriverManager.getConnection(“jdbc:derby:EmbDatabase;shutdown=true”);
    }
    catch(SQLException e)
    {
    System.out.println(“Exception in ConnectMe method is: “+e);
    }
    }

    This is the copy of my persistence.xml file (after editting):-

    org.eclipse.persistence.jpa.PersistenceProvider
    DatabasePackage.College

    Please Sir, help me….

  5. Sanjay Singh says:

    Please, someone tell me what to do…..

  6. Crazy Guy says:

    Thanks a lot for this article. However I’ve had the problem where I save some data to the embedded database, close the app, and when I re-open it, the data is gone. It appears that on startup the tables are getting dropped and then re-created, thus deleting all my precious data! So my work-around is to set the Table Generation Strategy to Create (not Drop and Create):

  7. Pingback: Basanta Thapa's Blog » Important Links for Learning Netbeans Platform

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s