0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Khalifa Gad
  • 361
  • 1
  • 6
  • 26
  • The URL `jdbc:derby:elsarhEmbedded` is a **relative** url, you are probably connecting to a different database from NetBeans. The database used by your application is probably empty and has no tables. – Mark Rotteveel Mar 31 '18 at 17:16
  • @MarkRotteveel i see it's documented there (https://db.apache.org/derby/docs/10.0/manuals/develop/develop15.html) like that, but if it wrong what i have to do ? is it a good idea to set the absolute path ? – Khalifa Gad Mar 31 '18 at 17:32
  • i haven't add 'create=true;' to the url to create a differernt if it doesn't exist . – Khalifa Gad Mar 31 '18 at 17:35

1 Answers1

0

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

Khalifa Gad
  • 361
  • 1
  • 6
  • 26