简体   繁体   English

在示例Java项目中使用数据库的最简单方法?

[英]Easiest Way to Use Database in Sample Java Project?

I'll preface this with: I have very little database experience. 我将以以下内容作为开头:我几乎没有数据库经验。 I understand the concept of a database and how to use a database within Java, but I have no idea how to create one and my knowledge of SQL is essentially zilch. 我了解数据库的概念以及如何在Java中使用数据库,但是我不知道如何创建数据库,而我的SQL知识本质上是zilch。

I am creating a sample project to show off the features of IntelliJ to my company, in the hopes they will convert from Eclipse. 我正在创建一个示例项目,向我的公司展示IntelliJ的功能,希望它们可以从Eclipse转换而来。 One of the big features for our company will be database integration, so I would like to show off the Database tools. 我们公司的主要功能之一就是数据库集成,因此我想炫耀数据库工具。 To do this, I need a database in the project. 为此,我在项目中需要一个数据库。 It doesn't need to be big or fancy or proper... it just needs to be there . 它不需要大,花哨或适当……它只需要在那儿即可 It will never scale up. 它永远不会扩大规模。 It will never be more than 10 rows and 4 or 5 columns. 它永远不会超过10行和4或5列。

Here's my requirements though: 不过,这是我的要求:

  • It needs to be easy to start the instance via Maven 通过Maven启动实例很容易
  • It can't be Derby (no Derby drivers in our Maven repo) 不能是Derby(我们的Maven存储库中没有Derby驱动程序)
  • It must work with Hibernate and Spring 它必须与Hibernate和Spring一起使用
  • Everything must run through Maven - no IDE specific stuff 一切都必须通过Maven运行-没有IDE专用的东西
  • The database content needs to be persistent so I can push it to the project repository (as in, not an in-memory DB that is deleted as soon as the connection closes) 数据库内容需要是持久的,因此我可以将其推送到项目存储库(例如,不是连接关闭后立即删除的内存数据库)

How can I create this? 我该如何创建呢? How do I create the file with the DB content? 如何使用数据库内容创建文件? How do I start the instance via Maven? 如何通过Maven启动实例? If it is something like HSQLite and the DB content is stored in a *.sql file, how do I do I automate starting an instance and running the file with a single maven command? 如果它类似于HSQLite,并且数据库内容存储在* .sql文件中,如何使用单个maven命令自动启动实例并运行该文件?

The key here is easy. 关键很简单。 I want a dummy (like me) with no idea how to do this to be able to run this project and try it for him or herself. 我想要一个不知道该如何做的假人(像我一样)来运行该项目并为他或她自己尝试。

Thanks for any help that's out there! 感谢您提供的任何帮助!

David 大卫

You might try sqllite for a simple database that persists data to a file. 您可以尝试将sqllite用于将数据持久保存到文件的简单数据库。 There is a jdbc driver for it here: https://bitbucket.org/xerial/sqlite-jdbc along with instructions for use. 这里有一个jdbc驱动程序: https : //bitbucket.org/xerial/sqlite-jdbc以及使用说明。 You can see that when you specify the connection, you pass in a file name. 您可以看到,当您指定连接时,您传入文件名。 The driver itself can be downloaded via maven. 驱动程序本身可以通过maven下载。

It seems like there's some question as to whether you can use hibernate with it: Does Hibernate Fully Support SQLite . 关于是否可以将hibernate与它一起使用似乎存在一些问题: Hibernate是否完全支持SQLite It seems like it might be worth a shot. 似乎值得一试。

One step up in complexity is hsqldb. hsqldb是提高复杂性的第一步。 I often use it for testing hibernate persistence layers in memory (with the production app using mysql or oracle). 我经常用它来测试内存中的休眠持久层(使用mysql或oracle的生产应用程序)。 So I know that it works with spring/hibernate: http://hsqldb.org/ . 所以我知道它可以与spring / hibernate一起使用: http : //hsqldb.org/

Have fun! 玩得开心!

If you just need a database and don't necessarily have to create one using IntelliJ. 如果您只需要一个数据库,而不必使用IntelliJ创建一个数据库。 I would write the database in SQL and then connect to it using IntelliJ. 我会用SQL编写数据库,然后使用IntelliJ连接到它。 That's just my take on it cause you'd have to use external modules/packages if you wanted to create it through Java. 这只是我的看法,因为如果要通过Java创建它,则必须使用外部模块/软件包。

I suggest h2 http://www.h2database.com/html/main.html 我建议H2 http://www.h2database.com/html/main.html
It's java, it can load sql, it will create a db file if you use file mode 它是Java,可以加载sql,如果使用文件模式,它将创建一个db文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM