2011-02-15 91 views
0

我創建嵌入式Derby數據庫,它給我error.although我在哪個表REST創建Java的嵌入式Derby表/視圖

java.sql.SQLSyntaxErrorException: Table/View 'REST' does not exist. 
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source) 
at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) 
at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown Source) 

這裏是java類APP模式:

public class Main 
{ 
    private static String dbURL = "jdbc:derby:tes;create=true"; 
    private static String tableName = "REST"; 
    // jdbc Connection 
    private static Connection conn = null; 
    private static Statement stmt = null; 

    public static void main(String[] args) 
    { 
     createConnection(); 
     insertRestaurants(5, "LaVals", "Berkeley"); 
     selectRestaurants(); 
     shutdown(); 
    } 

    private static void createConnection() 
    { 
     try 
     { 
     // System.setProperty("derby.system.home", "/Users/myuser/futbol"); 
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 
      //Get a connection 
      conn = DriverManager.getConnection(dbURL); 
     } 
     catch (Exception except) 
     { 
      except.printStackTrace(); 
     } 
    } 
    private static void insertRestaurants(int id, String restName, String cityName) 
    { 
     try 
     { 
      stmt = conn.createStatement(); 
      stmt.execute("insert into REST values (" + 
        id + ",'" + restName + "','" + cityName +"')"); 
      stmt.close(); 
     } 
     catch (SQLException sqlExcept) 
     { 
      sqlExcept.printStackTrace(); 
     } 
    } 

} 

回答

1

表名區分大小寫。確保您創建的表格實際上被稱爲「REST」,並帶有全部上限或「Rest」或「Rest」。

+0

是的兩個都是大寫的,但仍然給我同樣的錯誤ALTHOGH它工作正常,當我右鍵單擊db並選擇執行命令。但在Java它給出了這個錯誤。 – 2011-02-15 18:49:20

+1

表名在使用雙引號創建時僅區分大小寫。至少這就是標準要求的內容,據我所知德比符合標準 – 2011-02-15 18:56:13

1

我知道我在這方面落後了四年,但我終於發現我是如何解決我的問題的(因此我爲什麼在這個線程上)。

我使用數據庫腳本編輯器創建了我的表格。我在桌子周圍用雙引號。當我去服務選項卡(netbeans)時,我發現了這一點,並右鍵單擊我的表來查看數據。我得到了這個:

select * from {schema}."{table}" 

所以,我想我會把它翻譯成我的java。而賓果。 希望這會對你的應用程序產生一些影響,我複製並粘貼了一些代碼,希望這會起作用。

public class Db { 
    private final String url = "jdbc:derby://localhost:1527/{db}"; 
    private final String tab = "{schema}.\"REST\""; 

    private static Connection createConnection() throws Exception { 
     Connection conn = null; 
     conn = DriverManager.getConnection(url); 
     return conn; 
    } 

    private static void insertRestaurants(int id, String restName, String cityName) 
    { 
     try 
     { 
      Connection conn = createConnection(); 
      stmt = conn.createStatement(); 
      stmt.execute("insert into " + table + " values (" + 
        id + ",'" + restName + "','" + cityName +"')"); 
      stmt.close(); 
     } 
     catch (SQLException sqlExcept) 
     { 
      sqlExcept.printStackTrace(); 
     } 
    } 
} 

乾杯!

0

我做了以下修訂,使用Eclipse:

(在數據庫的角度來看:窗口>透視圖>打開透視圖>其它>數據庫開發),右鍵單擊數據庫連接文件夾>新建>德比:

您必須確保'Database Location'文件夾與(編輯驅動程序定義> JAR列表)中提到的Derby.jar位置位於相同的位置。像見下文快照:

Derby Jar Location

Derby DB Location

希望這有助於。