Search code examples
tableviewderbyjavadb

Netbeans derby embedded error


I have created a GUI alp with a database. I have a problem with the Embedded connection. I made this Embedded connection in Services tab > drivers > Java DB (Embedded) and connect using. I enter the data as follows: enter image description here

;create=true is what I write because it generally will not create a database folder in the app folder. I create a table and I put that URL in jer the code con = DriverManager.getConnection

When I start the app and when I enter the data in the respective fields that should fill in this database, the following error appears: java.-sql.SQLSyntaxErrorException: Table/View 'nameTable' does not exists!

I first added derby.jar driver to the library and then I added Java DB driver which comes with Netbeans, but the error remains. The same error appears no matter what I do.

Here a code:

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
   try {     Connection con;

            Connection db = null;

        con = DriverManager.getConnection("jdbc:derby:testBase;create=true ", "app", "admin77");

        Statement stmt = con.createStatement();
        String Query ="INSERT  INTO LINGU (NAME , CONCTRACTNO , EMAIL , PHONE , VIBER ) VALUES ('"+fNameLname.getText()+"' , '"+contTxt.getText()+"' , '")
        stmt.execute(Query);

       JOptionPane.showMessageDialog(null,"You have successfully added this vendor to the list of Lingu vendors.");

        fNameLname.setText(null);
        contTxt.setText(null);
        emailTxT.setText(null);
        phoneTxT.setText(null);
        viberBox.setSelectedIndex(0);
    }     
    catch(SQLException ex){
        JOptionPane.showMessageDialog(null, ex.toString());
    }
}                                       

Table:

http://i.imgur.com/nJKa9zV.png


Solution

  • With a JDBC Connection URL like jdbc:derby:testBase;create=true, you are telling Derby to access a database named testBase, which is to be found in a folder named testBase relative to whatever is the current working directory when you run your program.

    And you are also telling Derby that, if there is no database in a folder named testBase relative to whatever is the current working directory when you run your program, Derby should go ahead and create a new fresh empty database in that location.

    This approach makes it easy to confuse yourself, because if you run the program once in one directory, and create some tables and load some data, and then run the program again in a different directory, it will seem like all your tables have vanished and all your data is gone.

    (It isn't, it's just that you've now created two different databases, and you're confused about which database you're using with which run of your program.)

    Note that, from the point of view of Derby, the NetBeans IDE is just another program that is using Derby, and the NetBeans IDE, itself, has its own "current working directory", and it is almost certainly not the same as the current working directory that you use when you run your program by hand.

    Derby's ability to use relative filesystem paths for database locations, and its ability to quietly manufacture a new empty database on demand (create=true) are very nice features, but they can also be very confusing when you are just starting to learn Derby.

    A fairly simple alternative is to avoid using relative database path names in your JDBC Connection URL, and instead always use an absolute database pathname, like:

    jdbc:derby:/path/to/my/db/folder/testBase
    

    Then you will find that you are always using the same database, and your data will be there the next time you go open that database.