在javafx应用程序中使用嵌入式derby的jdbc:表/视图'表名称'不存在例外

时间:2018-03-31 14:17:53

标签: java jdbc javafx-8 derby embedded-database

我正在创建一个JavaFX独立应用程序并使用嵌入式derby数据库并通过JDBC连接到它。问题在于,每当我尝试与数据库表进行交互时,我都会得到" java.sql.SQLSyntaxErrorException:Table / View' BUILDINGS'不存在。"。

我搜索了很多,但我无法解决它。我查了提到here的三个原因,但我仍然得到同样的错误。 This帖子听起来与我的情况非常相似,但我无法弄清楚他是如何解决的。我已经从连接元数据和netbeans服务选项卡检查了数据库URL,但它是一样的。

以下是与问题相关的代码段:

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;
}}

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);
    }
}

我试图从netbeans向数据库执行命令并将数据插入到同一个表中并且它可以工作。

1 个答案:

答案 0 :(得分:0)

正如@Mark Rotteveel所说的那样是关于相对路径和目录的,并且在this question

的@BlueRat答案中得到了很好的解释