嵌入式数据库,Java Web项目

时间:2015-04-19 08:05:43

标签: java eclipse web-applications derby

我有一些Java知识,这是我在大学学习期间获得的(不是主要学科)。从那以后,我没有使用Java编程来生活。 最近我决定恢复编程技能,所以我用Vaadin开始了一些真正的java web项目。到目前为止,一些UI已经完成,现在我需要在DB中保存数据。 对不起长时间的介绍,我需要你了解我的水平

问题: 我想在Eclipse IDE中使用嵌入式数据库和Vaadin项目。我已经通过Ivy下载了derby jar文件,我在这里堆叠。所有教程都没有教授如何将Derby(any)DB链接到Web项目。

你能给我一些线索吗?

1 个答案:

答案 0 :(得分:2)

您可以在分发包中找到使用Embedded Derby的应用程序示例。你可以在这里找到相关的教程: https://db.apache.org/derby/papers/DerbyTut/embedded_intro.html

使用它就像使用任何其他jdbc驱动程序一样简单,只有嵌入式数据库在同一进程Java中执行。

以下是一个例子:

        /*
         * This connection specifies create=true in the connection URL to
         * cause the database to be created when connecting for the first
         * time. To remove the database, remove the directory derbyDB (the
         * same as the database name) and its contents.
         *
         * The directory derbyDB will be created under the directory that
         * the system property derby.system.home points to, or the current
         * directory (user.dir) if derby.system.home is not set.
         */
        String protocol = "jdbc:derby:";
        String dbName = "derbyDB"; // the name of the database
        props.put("user", "user1");
        props.put("password", "user1");
        Connection conn = DriverManager.getConnection(protocol + dbName
                + ";create=true", props);
        // Then you can use jdbc classes to create and execute your queries
        // For example :
        Statement s = conn.createStatement();
        // We create a table...
        s.execute("create table location(num int, addr varchar(40))");