java嵌入式derby表/视图

发布于 2024-10-17 19:27:35 字数 2086 浏览 5 评论 0原文

我创建了 Embedded Derby 数据库,它给了我错误。尽管我有 APP 架构,其中

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)

此处创建的 REST 表是 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();
        }
    }

}

I created Embedded Derby database it give me error.although I have APP schema in which table REST created

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)

here is java class:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心意如水 2024-10-24 19:27:35

我知道我在这方面落后了四年,但我终于找到了如何解决我的问题(这就是为什么我在这个帖子上)。

我使用数据库脚本编辑器创建了表。我在桌子周围使用了双引号。当我进入服务选项卡(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();
        }
    }
}

干杯!

I know I am four years behind on this, but I finally found out how I resolved my problem (hence why I am on this thread).

I created my table using the database script editor. I used double quotes around my table. I found this out when I went to the service tab (netbeans), and right clicked on my table to view data. I got this:

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

So, I thought I would translate that to my java. And, Bingo.
Hopefully this will shed some light on your application, I copied and pasted some of your code and hopefully this will work.

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

Cheers!

栩栩如生 2024-10-24 19:27:35

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

Table names are case sensitive. Make sure the table you created is actually called "REST" with all caps or if it's "Rest" or "rest".

半寸时光 2024-10-24 19:27:35

我在嵌入式数据库方面遇到了同样的问题,最终清除了相同的问题。这肯定会对您有所帮助 aparna-java.blogspot.com 请检查此

I had the same trouble with embedded db and finally cleared the same .This will help you for sure aparna-java.blogspot.com please check this

女中豪杰 2024-10-24 19:27:35

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

在数据库透视图:窗口>透视图>打开透视图>其他>数据库开发),右键单击数据库连接文件夹>新建>Derby:

您必须确保“数据库位置”文件夹位于与(编辑驱动程序定义 > JAR 列表)中提到的 Derby.jar 位置相同的位置。就像下面的快照一样:

Derby Jar 位置

Derby DB 位置

希望这会有所帮助。

I did the following fix, using Eclipse:

(In Database Perspective: Window>Perspective>Open Perspective>Other>Database Development), right clicking Database Connection folder>New>Derby:

You have to make sure the 'Database Location' folder is at the same location as the Derby.jar location mentioned at (Edit Driver Definition>JAR List). Like see below snapshot:

Derby Jar Location

Derby DB Location

Hope this helps.

迷路的信 2024-10-24 19:27:35

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

You might find it easier to learn Derby by starting with the Derby tutorials: http://db.apache.org/derby/docs/10.7/getstart/cgstutorialintro.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文