如何用Java存储数据?数据库?

时间:2022-07-11 16:51:10

I am writing an application that I want to store, retrieve, sort, and search data. I am using Java and I have the GUI and some functionality written already but I have no idea how to store data in Java. I have done a VERY little bit of work with SQLite when I was writing an app for my android phone and was looking for something like that maybe.

我正在编写一个我想存储,检索,排序和搜索数据的应用程序。我正在使用Java,我已经编写了GUI和一些功能,但我不知道如何在Java中存储数据。当我为我的Android手机编写应用程序并且正在寻找类似的东西时,我已经对SQLite做了很多工作。

I need to store information that is linked to each other, for example think of an address book. The name, address, phone number, email address, etc. would be linked to each other and go together for one entry in the address book. Similarly I need to store a large amount of data in this way and be able to search it for needed data. I would also like to be able to retrieve the data in a fashion that I could get the address value for "name".

我需要存储彼此链接的信息,例如想一下地址簿。姓名,地址,电话号码,电子邮件地址等将相互链接,并在地址簿中一起输入一个条目。同样,我需要以这种方式存储大量数据,并能够搜索所需的数据。我还希望能够以一种我可以获得“名称”的地址值的方式检索数据。

I need to know the easiest way to accomplish this. After googleing it I found information on Java DB but I don't know if this is exactly what I am looking for or how to use it.

我需要知道实现这一目标的最简单方法。在google之后我发现了有关Java DB的信息,但我不知道这是否正是我正在寻找的或如何使用它。

Please answer by providing me with information on what to use to store the data and if possible point me in the direction of a tutorial on how to use it.

请通过向我提供有关用于存储数据的内容的信息来回答,如果可能的话,请指导我如何使用它的教程。

---------------------------------------EDIT--------------------------------------------

- - - - - - - - - - - - - - - - - - - -编辑 - - - - - ----------------------------------

I appreciate the Help but I am still not getting it. I want to use SQLite and I followed the link that Kristian Antonsen gave and I downloaded the SQLiteJDBC thing but now I am lost. I am clearly in new territory here and completely lost.

我很感激帮助,但我仍然没有得到它。我想使用SQLite,然后我按照Kristian Antonsen给出的链接,我下载了SQLiteJDBC,但现在我迷路了。我显然在这里的新领域,完全迷失了。

Can someone give me an "idiots guide" on how to use this and make it work in my application?

有人可以给我一个关于如何使用它并使其在我的应用程序中工作的“白痴指南”吗?

8 个解决方案

#1


10  

There are lots of ways to accomplish this, here's a few

有很多方法可以实现这一点,这里有一些

#2


3  

A very simple and easy to use database you can use is db4o. The good thing about db4o is that you can store plain old Java objects in your database! No need to map them to tables and back!

您可以使用的一个非常简单易用的数据库是db4o。 db4o的好处是你可以在数据库中存储普通的旧Java对象!无需将它们映射到表格并返回!

Alternatively, if you like to use the SQL language (similar to SQLite), you can use H2DB. JavaDB, is also another option. Both of H2DB and JavaDB can be used as embedded databases and distributed with your app.

或者,如果您想使用SQL语言(类似于SQLite),则可以使用H2DB。 JavaDB也是另一种选择。 H2DB和JavaDB都可以用作嵌入式数据库,并随您的应用程序一起分发。

If you decide to use SQL, you need to learn JDBC (Java Database Connectivity). It lets you communicate with the SQL database of your choice. If JDBC seemed too level for your taste, then you can learn JPA (Java Persistence API).

如果您决定使用SQL,则需要学习JDBC(Java数据库连接)。它允许您与您选择的SQL数据库进行通信。如果JDBC看起来太符合您的口味,那么您可以学习JPA(Java Persistence API)。

However, IMHO, unless proven otherwise, db4o seems the best option for your use case and situation in my opinion.

但是,恕我直言,除非另有证明,db4o似乎是我认为最适合您的用例和情况。

#3


1  

Use MySQL server to store data in localserver service.

使用MySQL服务器在localserver服务中存储数据。

Driver Class="java.sql.Driver"

Driver Class =“java.sql.Driver”

You can insert data to mysql RDBMS model in SQL-like statements.

您可以在类似SQL的语句中将数据插入mysql RDBMS模型。

Don't forget to put MySQL J Connector to your java library folder.

不要忘记将MySQL J Connector放到java库文件夹中。

#4


0  

I would like to support Kristian Antonsen on his comment because HashMaps are the best method to store data in Java. If you want to store data in alphabetic order, TreeMap is the more convenient way.

我想支持Kristian Antonsen的评论,因为HashMaps是用Java存储数据的最佳方法。如果要按字母顺序存储数据,TreeMap是更方便的方式。

#5


0  

Aside from what Kristian Antonsen already mentioned, I should also mention Hibernate. I just recently picked it up by going through a series of YouTube videos such as this playlist

除了Kristian Antonsen已经提到的内容之外,我还应该提到Hibernate。我最近通过浏览一系列YouTube视频(例如此播放列表)来选择它

From what I gathered, it is a better solution that's built on top of JDBC (which can be tedious).

从我收集的内容来看,这是一个基于JDBC构建的更好的解决方案(可能很乏味)。

I would suggest you make the decision based on what your project needs and the time you have.

我建议你根据项目需求和时间做出决定。

#6


0  

In order to use SQLite, try following this discussion: Java and SQLite

为了使用SQLite,请尝试以下讨论:Java和SQLite


The old version (I believe it still holds):

旧版本(我相信它仍然有效):

The Easiest Way: serialize your data object on exit, de-serialize it on startup. For a sample application, prototype, or a quick test it works wonders, and does not require much coding.

最简单的方法:在退出时序列化您的数据对象,在启动时对其进行反序列化。对于示例应用程序,原型或快速测试,它可以创建奇迹,并且不需要太多编码。

For downvoters: yes I do use this approach every time I need to start with something. I don't need to worry much about details, and I know I can change my persistence layer later on. It does help to postpone important storage decisions until I know what kind of data I have.

对于downvoters:是的,每次我需要从一些东西开始时我都会使用这种方法。我不需要担心细节,我知道我可以稍后更改我的持久层。它确实有助于推迟重要的存储决策,直到我知道我有什么样的数据。

Less easy way would involve a database, but that requires a proper data design—if you don't share it, it's hard to recommend anything specific. The reasonable choices could be SQLite, mySQL, MongoDB or almost any other database. What fits better in your case is hard to say.

不太简单的方法涉及数据库,但这需要适当的数据设计 - 如果你不共享它,很难推荐任何具体的。合理的选择可能是SQLite,mySQL,MongoDB或几乎任何其他数据库。在你的情况下更适合的是很难说的。

#7


0  

You can use Apache Derby (Java DB) as a native DB embedded in JDK. A short tutorial can be found here:

您可以将Apache Derby(Java DB)用作嵌入JDK的本机DB。可以在这里找到一个简短的教程:

https://netbeans.org/kb/docs/ide/java-db.html

https://netbeans.org/kb/docs/ide/java-db.html

#8


0  

Hi There is HSQL Database.(http://hsqldb.org/) This is java baseed file system to store in like data as tables. You can also use this with hibernate

嗨有HSQL数据库。(http://hsqldb.org/)这是基于java的文件系统,用于存储与表格类似的数据。您也可以使用hibernate

#1


10  

There are lots of ways to accomplish this, here's a few

有很多方法可以实现这一点,这里有一些

#2


3  

A very simple and easy to use database you can use is db4o. The good thing about db4o is that you can store plain old Java objects in your database! No need to map them to tables and back!

您可以使用的一个非常简单易用的数据库是db4o。 db4o的好处是你可以在数据库中存储普通的旧Java对象!无需将它们映射到表格并返回!

Alternatively, if you like to use the SQL language (similar to SQLite), you can use H2DB. JavaDB, is also another option. Both of H2DB and JavaDB can be used as embedded databases and distributed with your app.

或者,如果您想使用SQL语言(类似于SQLite),则可以使用H2DB。 JavaDB也是另一种选择。 H2DB和JavaDB都可以用作嵌入式数据库,并随您的应用程序一起分发。

If you decide to use SQL, you need to learn JDBC (Java Database Connectivity). It lets you communicate with the SQL database of your choice. If JDBC seemed too level for your taste, then you can learn JPA (Java Persistence API).

如果您决定使用SQL,则需要学习JDBC(Java数据库连接)。它允许您与您选择的SQL数据库进行通信。如果JDBC看起来太符合您的口味,那么您可以学习JPA(Java Persistence API)。

However, IMHO, unless proven otherwise, db4o seems the best option for your use case and situation in my opinion.

但是,恕我直言,除非另有证明,db4o似乎是我认为最适合您的用例和情况。

#3


1  

Use MySQL server to store data in localserver service.

使用MySQL服务器在localserver服务中存储数据。

Driver Class="java.sql.Driver"

Driver Class =“java.sql.Driver”

You can insert data to mysql RDBMS model in SQL-like statements.

您可以在类似SQL的语句中将数据插入mysql RDBMS模型。

Don't forget to put MySQL J Connector to your java library folder.

不要忘记将MySQL J Connector放到java库文件夹中。

#4


0  

I would like to support Kristian Antonsen on his comment because HashMaps are the best method to store data in Java. If you want to store data in alphabetic order, TreeMap is the more convenient way.

我想支持Kristian Antonsen的评论,因为HashMaps是用Java存储数据的最佳方法。如果要按字母顺序存储数据,TreeMap是更方便的方式。

#5


0  

Aside from what Kristian Antonsen already mentioned, I should also mention Hibernate. I just recently picked it up by going through a series of YouTube videos such as this playlist

除了Kristian Antonsen已经提到的内容之外,我还应该提到Hibernate。我最近通过浏览一系列YouTube视频(例如此播放列表)来选择它

From what I gathered, it is a better solution that's built on top of JDBC (which can be tedious).

从我收集的内容来看,这是一个基于JDBC构建的更好的解决方案(可能很乏味)。

I would suggest you make the decision based on what your project needs and the time you have.

我建议你根据项目需求和时间做出决定。

#6


0  

In order to use SQLite, try following this discussion: Java and SQLite

为了使用SQLite,请尝试以下讨论:Java和SQLite


The old version (I believe it still holds):

旧版本(我相信它仍然有效):

The Easiest Way: serialize your data object on exit, de-serialize it on startup. For a sample application, prototype, or a quick test it works wonders, and does not require much coding.

最简单的方法:在退出时序列化您的数据对象,在启动时对其进行反序列化。对于示例应用程序,原型或快速测试,它可以创建奇迹,并且不需要太多编码。

For downvoters: yes I do use this approach every time I need to start with something. I don't need to worry much about details, and I know I can change my persistence layer later on. It does help to postpone important storage decisions until I know what kind of data I have.

对于downvoters:是的,每次我需要从一些东西开始时我都会使用这种方法。我不需要担心细节,我知道我可以稍后更改我的持久层。它确实有助于推迟重要的存储决策,直到我知道我有什么样的数据。

Less easy way would involve a database, but that requires a proper data design—if you don't share it, it's hard to recommend anything specific. The reasonable choices could be SQLite, mySQL, MongoDB or almost any other database. What fits better in your case is hard to say.

不太简单的方法涉及数据库,但这需要适当的数据设计 - 如果你不共享它,很难推荐任何具体的。合理的选择可能是SQLite,mySQL,MongoDB或几乎任何其他数据库。在你的情况下更适合的是很难说的。

#7


0  

You can use Apache Derby (Java DB) as a native DB embedded in JDK. A short tutorial can be found here:

您可以将Apache Derby(Java DB)用作嵌入JDK的本机DB。可以在这里找到一个简短的教程:

https://netbeans.org/kb/docs/ide/java-db.html

https://netbeans.org/kb/docs/ide/java-db.html

#8


0  

Hi There is HSQL Database.(http://hsqldb.org/) This is java baseed file system to store in like data as tables. You can also use this with hibernate

嗨有HSQL数据库。(http://hsqldb.org/)这是基于java的文件系统,用于存储与表格类似的数据。您也可以使用hibernate