【问题标题】:java embedded derby table/viewjava嵌入式德比表/视图
【发布时间】:2011-02-15 18:30:43
【问题描述】:

我创建了 Embedded Derby 数据库,它给了我错误。虽然我有 APP 架构,其中创建了表 REST

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类:

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

}

【问题讨论】:

    标签: java derby embedding


    【解决方案1】:

    我知道我在这方面落后了四年,但我终于发现了我是如何解决我的问题的(这就是我在这个线程上的原因)。

    我使用数据库脚本编辑器创建了我的表。我在桌子周围使用了双引号。当我转到服务选项卡(netbeans)并右键单击我的表以查看数据时,我发现了这一点。我得到了这个:

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

    所以,我想我会把它翻译成我的 java.lang.还有,宾果。 希望这会对您的应用程序有所帮​​助,我复制并粘贴了您的一些代码,希望这能奏效。

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

    干杯!

    【讨论】:

      【解决方案2】:

      表名区分大小写。确保您创建的表实际上是全部大写的“REST”,或者它是“Rest”还是“rest”。

      【讨论】:

      • 是的,两者都是大写,但仍然给我同样的错误,尽管当我在 db 中单击并选择执行命令时它工作正常。但在 java 中它给出了这个错误。
      • 表名只有在使用双引号创建时才区分大小写。至少这是标准要求的,据我所知,Derby 符合那里的标准
      • 感谢您的评论!我没有意识到表格是区分大小写的。
      【解决方案3】:

      我在嵌入式数据库上遇到了同样的问题,最后清除了同样的问题。这肯定会帮助你aparna-java.blogspot.com请检查这个

      【讨论】:

        【解决方案4】:

        【讨论】:

          【解决方案5】:

          我使用 Eclipse 进行了以下修复:

          (在Database Perspective中:Window>Perspective>Open Perspective>Other>Database Development),右键Database Connection文件夹>New>Derby:

          您必须确保“数据库位置”文件夹与(编辑驱动程序定义>JAR 列表)中提到的 Derby.jar 位置位于同一位置。如下图所示:

          Derby Jar Location

          Derby DB Location

          希望这会有所帮助。

          【讨论】:

            【解决方案6】:

            您可能会发现从 Derby 教程开始学习 Derby 会更容易:http://db.apache.org/derby/docs/10.7/getstart/cgstutorialintro.html

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2015-05-11
              • 2023-04-09
              • 1970-01-01
              • 1970-01-01
              • 2018-09-10
              • 1970-01-01
              • 2014-03-08
              • 1970-01-01
              相关资源
              最近更新 更多