Search code examples
javadatabasenetbeansdatabase-connectionderby

Java not able to see the derby database table when running


The Problem:

I am creating an application that requires the use of an integrated database but I am having issues getting my application to connect to the database/table. Unfortunately, my knowledge of Java connecting to databases is rather limited but everything I have read and watched seems to point to my code being correct.

My Code:

public static void main(String[] args) {
    try{
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection con = DriverManager.getConnection("jdbc:derby:datahold;");
        Statement stat = con.createStatement();

        ResultSet rs = stat.executeQuery("select * from APP.DATASTORE");
        ResultSetMetaData meta=rs.getMetaData();
        int columnCount = meta.getColumnCount();
        for (int x = 1; x <= columnCount; x++)
            System.out.format("%20s",meta.getColumnName(x)+ " | ");

        while (rs.next()){
            System.out.println("");
            for (int x = 1; x <= columnCount; x++) System.out.format("%20s", rs.getString(x)+ " | ");
        }
        if (stat != null) stat.close();
        if (con != null) con.close();
    } catch(Exception e) {
        System.out.print(e);
    }
}

The Error:

java.sql.SQLSyntaxErrorException: Table/View 'APP.DATASTORE' does not exist.BUILD SUCCESSFUL (total time: 1 second)

My Data Base Setup:

Data Base Setup

I have tried removing the "APP." but this then results in the application not being able to find a table "ROOT.DATASTORE".

If anyone is able to help me out here that would be great!

UPDATE:

I can now see that when running the application, it is creating the database files in the root of the package. Therefore, the embedded driver connection must be working (at least that's the way I see it). My question is should the database be located here or should it be held in the "dist" folder?

I am assuming it is not able to see the table because it is looking in the wrong location.


Solution

  • So the answer to this question is rather simple as I found out after HOURS of tinkering.

    Netbeans kindly creates a persistent xml file that stores some data that the application uses when it starts up. More precisely, when the database starts up. One of these options is which connection to use.

    All I had to do was change this connection to my embedded driver connection and it is now working.