Search code examples
javajdbcjavafx-8derbyembedded-database

jdbc with embedded derby in javafx application: Table/View 'TABLE NAME' does not exist exception


I am creating a JavaFX standalone application and using embedded derby database and connecting to it via JDBC. The problem is whenever I'm trying to interact with the database tables, I'm getting "java.sql.SQLSyntaxErrorException: Table/View 'BUILDINGS' does not exist.".

I have searched a lot, but I cannot resolve it. I have looked up those three reasons which mentioned here, but I'm still getting the same error. This post sounds very similar to my case, but I cannot figure out how he resolved it. I have checked the database URL from connection meta-data and from netbeans services tab and it's the same.

Here are code snippets which related to the problem:

public class DataBaseUtility {

public Connection derbyDBConnection(String url){
    Connection con = null;

    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    try {
        Class.forName(driver).newInstance();
        con = DriverManager.getConnection(url, "", "");
        con.setSchema("APP");
    } catch (ClassNotFoundException | SQLException | 
            InstantiationException | IllegalAccessException ex) {
        Logger.getLogger(DataBaseUtility.class.getName())
                .log(Level.SEVERE, null, ex);
    }
    return con;
}

public ResultSet databaseQuerying(Connection con, String query){

    ResultSet resultSet = null;
    try {
        PreparedStatement preStmt = con.prepareStatement(query);
        resultSet = preStmt.executeQuery();
    } catch (SQLException ex) {
        Logger.getLogger(DataBaseUtility.class.getName()).log(Level.SEVERE, null, ex);
    }
    return resultSet;
}}

and

public class MainApp extends Application {

public static DataBaseUtility dbUtility ;
public static Connection con;

@Override
public void init(){
    dbUtility = new DataBaseUtility();
    con = dbUtility.derbyDBConnection("jdbc:derby:elsarhEmbedded");
    try {
        DatabaseMetaData dbmd = con.getMetaData();
        System.out.println("\n----------------------------------------------------") ;
        System.out.println("Database Name     = " + dbmd.getDatabaseProductName()) ;
        System.out.println("Database User Name= " + dbmd.getUserName()) ;
        System.out.println("Database Version  = " + dbmd.getDatabaseProductVersion()) ;
        System.out.println("Driver Name       = " + dbmd.getDriverName()) ;
        System.out.println("Driver Version    = " + dbmd.getDriverVersion()) ;
        System.out.println("Database URL      = " + dbmd.getURL()) ;
        System.out.println("----------------------------------------------------") ;

 /*
    -------------the result from the above ----------
    Database Name     = Apache Derby
    Database User Name= APP
    Database Version  = 10.14.1.0 - (1808820)
    Driver Name       = Apache Derby Embedded JDBC Driver
    Driver Version    = 10.14.1.0 - (1808820)
    Database URL      = jdbc:derby:elsarhEmbedded
    */

    } catch (SQLException ex) {
        Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I have tried to execute command to the database from netbeans and insert data into the same table and it works.


Solution

  • As @Mark Rotteveel said it's all about the relative paths and directories, and it's well explained in the answer of @BlueRat at this question