How to create and get started with Embedded Apache Derby database in Dropwizard project (Angular 7 front-end)

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 16:45:02

问题


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

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.

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.


回答1:


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.

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.

From Oracle

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

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

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");

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

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

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

prior to quitting the main application. It is not mandatory, but recommended.


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


来源:https://stackoverflow.com/questions/54854010/how-to-create-and-get-started-with-embedded-apache-derby-database-in-dropwizard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!