如何在Dropwizard项目(Angular 7前端)中创建和开始使用嵌入式Apache Derby数据库 [英] How to create and get started with Embedded Apache Derby database in Dropwizard project (Angular 7 front-end)

查看:130
本文介绍了如何在Dropwizard项目(Angular 7前端)中创建和开始使用嵌入式Apache Derby数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Derby文档并按照所有说明进行操作。我已经成功安装了它(将其提取到我的Linux机器上并设置DERBY_HOME路径)。我有一个完整的REST API项目,其中包含Angular 7前端和Dropwizard后端。我在后端对一些数据进行了硬编码,并创建了我需要的所有HTTP API方法(GET,POST,PATCH,DELETE)。

I'm reading through Derby documentation and following all the instructions. I've successfully installed it (extracted it to my Linux machine and set the DERBY_HOME path). I have a complete REST API project with Angular 7 front-end and Dropwizard backend. I hard coded some data in the backend, and created all the HTTP API methods I need (GET, POST, PATCH, DELETE).

该应用程序具有完整的功能,但是现在我需要在其中实现嵌入式Derby版本。我对此类数据库有0个经验,并且因为Dropwizard已经给我带来了足够的麻烦,所以我不知道该如何开始。

The application is fully functional, but now I need to implement the Embedded version of Derby into it. I have 0 experience with such databases, and because Dropwizard gave me enough trouble already, I cannot figure out how to get started.

我是否创建了一个新类并开始使用在那里,如何创建这些SQL文件以及如何存储数据?对于类似的问题,我找不到具体的答案。如果已有详细的解释和示例,请随时向我提供资源。我知道这是一个菜鸟问题,但我几乎不了解HTTP的工作原理(基本知识),并设法使用Angular和Dropwizard完全创建了功能性REST。

Do I create a new class and get started there, how to create those SQL files and how to store data? I can't find a concrete answer to similar questions, if there are detailed explanations and examples already out there, please feel free to provide me with the resources. I know this is a noob question, but I just barely learned how HTTP works (the basics) and managed to completely create a functional REST using Angular and Dropwizard.

推荐答案

将嵌入式数据库像一个成熟的数据库一样,与您的应用程序一起打包并在同一JVM中运行,而不是位于不同的环境中,并且可能需要网络连接。两者之间应用相同的机制。

Consider the embedded database like a full-fledged database, that instead of being in a different environment, and maybe requiring a network connection, is packed along with your application and run in the same JVM. The same mechanisms applies between the two.

嵌入式Derby驱动程序位于 derby.jar 文件中,因此必须将其保存在应用程序的 classpath 中。它应该位于%DERBY_INSTALL%\lib\ 下,其中%DERBY_INSTALL%是安装目录。您可以通过图像查看包含它的位置。

The embedded Derby Driver is located inside the derby.jar file, so it is required to have it in the classpath of your application. It should be located under %DERBY_INSTALL%\lib\, where %DERBY_INSTALL% is the installation directory. You can see by the image where it is contained.

来自 Oracle


在类路径中找到的所有JDBC 4.0驱动程序都会自动加载
。 (但是,必须在JDBC 4.0之前使用Class.forName方法手动加载任何驱动程序
。)

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)

那是什么就是说,如果Derby驱动程序是JDBC 4.0驱动程序,则无需通过 DriverManager 获得连接的任何其他操作。

如果它不是JDBC 4.0驱动程序,则必须使用

What that means is that if the Derby driver is a JDBC 4.0 driver, you don't have to do anything else besided getting a connection via DriverManager.
If it is not a JDBC 4.0 driver, you'll have to instantiate the Driver with

Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();

显然,您需要上面的那段代码。

现在只需获取按住 Connection 对象。

Appartently you'll need that piece of code above.
Now simply get a hold on a Connection object.

DriverManager.getConnection("jdbc:derby:dbName;create=true");

从此处开始,您可以创建 Statement (s)如您所愿。这意味着您可以创建表,插入行,更新行,删除行等。

From there on, you can create Statement(s) as you like. Which means you can create tables, insert rows, update rows, delete rows, etc.

要正常关闭嵌入式Derby数据库,您需要使用

To gracefully shutdown the embedded Derby database, you need to use

DriverManager.getConnection("jdbc:derby:dbName;shutdown=true"); // see the same database name "dbName"

您可以创建一个实用程序类来保存 EmbeddedDataSource。

You can create a utility class to hold an EmbeddedDataSource (docs), which will provide connections around your application.

public final class EmbeddedDerby {
   private static final DataSource DATA_SOURCE;

   static {
      // Initialize DATA_SOURCE with EmbeddedDataSource
   }

   ...

   public static Connection getConnection() {
     return DATA_SOURCE.getConnection();
   }
}

这篇关于如何在Dropwizard项目(Angular 7前端)中创建和开始使用嵌入式Apache Derby数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆