【问题标题】:Embedded DB, Java Web Project嵌入式数据库、Java Web 项目
【发布时间】:2015-04-19 08:05:43
【问题描述】:

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

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

你们能给我一些线索吗?

【问题讨论】:

    标签: java eclipse web-applications derby


    【解决方案1】:

    您可以在分发包中找到使用 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))");
    

    【讨论】:

    • 嗨,Mostafa,感谢您的回答。我按照手册vogella.com/tutorials/EclipseDataToolsPlatform/article.html 进行操作,但对数据库位置的位置感到困惑。我已经下载了 DB,它可以从终端访问。但仍然对如何将数据库添加到我的项目感到困惑
    • 嵌入式数据库是在 Java 应用程序中运行的最小数据库服务器。您不需要运行数据库服务器,因为您的应用程序将运行它自己的嵌入式数据库服务器。如果嵌入式是你想要的,你真的不需要做任何事情,只需要初始化一个连接。我更新了我的帖子给你一个例子。
    • 实际上对于嵌入式数据库,您必须指定驱动程序私有静态最终字符串驱动程序=“org.apache.derby.jdbc.EmbeddedDriver”; Class.forName(driver) ;但无论如何谢谢你的线索!
    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2011-10-18
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多