Easiest Way to Use Database in Sample Java Project?

≯℡__Kan透↙ 提交于 2021-02-11 12:53:30

问题


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.

I am creating a sample project to show off the features of IntelliJ to my company, in the hopes they will convert from 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.

Here's my requirements though:

  • It needs to be easy to start the instance via Maven
  • It can't be Derby (no Derby drivers in our Maven repo)
  • It must work with Hibernate and Spring
  • Everything must run through Maven - no IDE specific stuff
  • 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? 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?

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


回答1:


You might try sqllite for a simple database that persists data to a file. There is a jdbc driver for it here: https://bitbucket.org/xerial/sqlite-jdbc along with instructions for use. You can see that when you specify the connection, you pass in a file name. The driver itself can be downloaded via maven.

It seems like there's some question as to whether you can use hibernate with it: Does Hibernate Fully Support SQLite. It seems like it might be worth a shot.

One step up in complexity is hsqldb. I often use it for testing hibernate persistence layers in memory (with the production app using mysql or oracle). So I know that it works with spring/hibernate: http://hsqldb.org/.

Have fun!




回答2:


If you just need a database and don't necessarily have to create one using IntelliJ. I would write the database in SQL and then connect to it using 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.




回答3:


I suggest 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



来源:https://stackoverflow.com/questions/22876460/easiest-way-to-use-database-in-sample-java-project

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