开发者

java embedded derby table/view

开发者 https://www.devze.com 2023-02-11 09:55 出处:网络
I created Embedded Derby database it give me error.although I have APP schema in which table REST created

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, "开发者_C百科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 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!


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".


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


A simple example @ http://javabyranjith.blogspot.in/2016/08/apache-derby-with-java-example.html. Hope it can be helpful


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号