java Java读写本地数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6523845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java reading and writing to a local database?
提问by Jon Storm
I have a program which is constantly getting information from a website and is constantly updating. As of now I store all that information in an arraylist
and then when I am done I write it to a text file.
我有一个程序,它不断从网站获取信息并不断更新。到目前为止,我将所有这些信息存储在一个文件中arraylist
,然后在完成后将其写入一个文本文件。
I need to manipulate that information; however, it creates a massive text file and I can't constantly read and write information to a text file because it takes too long. So someone told me to look into using a database. The only database I have ever used was a MySQL database for a website, never with java.
我需要操纵这些信息;然而,它创建了一个庞大的文本文件,我不能经常在文本文件中读取和写入信息,因为它花费的时间太长。所以有人告诉我研究使用数据库。我曾经使用过的唯一数据库是用于网站的 MySQL 数据库,从来没有使用过 java。
Is there any way to make a database local? As in only on my computer (don't want to pay for web hosting when I am the only one accessing this information at my computer)? I was looking a little bit into SQLite but one of the things I saw was that it does not allow concurrency. I'm thinking of multi-threading my program and having it read and write different sections at the same time. Is this possible?
有没有办法让数据库本地化?就像仅在我的计算机上一样(当我是唯一一个在我的计算机上访问此信息的人时,不想为网络托管付费)?我正在研究 SQLite,但我看到的一件事是它不允许并发。我正在考虑对我的程序进行多线程处理并让它同时读取和写入不同的部分。这可能吗?
Basically what I'm asking for here is:
基本上我在这里要求的是:
- What type of database should I use?
- How do I install that database on my computer?
- Some information on how to use Java jdbc?(was reading a little into it before)
- Any tutorials on any of the above (video, text, etc)
- 我应该使用什么类型的数据库?
- 如何在我的计算机上安装该数据库?
- 关于如何使用 Java jdbc 的一些信息?(之前读过一点)
- 以上任何教程(视频、文本等)
回答by Hyman Edmonds
It sounds like your friend is correct and a database would greatly help you improve performance.
听起来您的朋友是正确的,数据库将极大地帮助您提高性能。
- You can really use any type of database. You can even continue using MySQL by installing it locally and using Connector/Jto connect in Java. Though embeddable databases are probably the easiest. One option is Hypersonic DB (HSQLDB)
- There is no need to "install" embeddable databases. Simply add their library (.jar file) to your project's build path. When you "connect" to the database, you can usually tell the database to use a location on your filesystem to store its data.
- JDBC stands for Java DataBase Connectivity. It is the API used by Java applications to communicate with databases. One of the important features of JDBC is that it (usually) does not matter which database you are talking to as the API for accessing any database is the same*.
- Here is one tutorial for JDBC: JDBC(TM) Database Access
- 您真的可以使用任何类型的数据库。您甚至可以通过在本地安装 MySQL 并使用Connector/J在 Java 中连接来继续使用 MySQL 。尽管可嵌入数据库可能是最简单的。一种选择是Hypersonic DB (HSQLDB)
- 无需“安装”可嵌入数据库。只需将他们的库(.jar 文件)添加到项目的构建路径中。当您“连接”到数据库时,您通常可以告诉数据库使用文件系统上的某个位置来存储其数据。
- JDBC 代表 Java 数据库连接。它是 Java 应用程序用来与数据库通信的 API。JDBC 的重要特性之一是它(通常)与您正在交谈的数据库无关,因为访问任何数据库的 API 都是相同的*。
- 这是 JDBC 的一个教程:JDBC(TM) 数据库访问
*The API for accessing databases--e.g. sending queries and retrieving results--will generally be the same but JDBC can't hide differences in things like SQL. If you use an ORM like Hibernate, it can usually make it much easier to handle all the little incompatibilities between databases.
* 用于访问数据库的 API——例如发送查询和检索结果——通常是相同的,但 JDBC 无法隐藏 SQL 之类的差异。如果您使用像Hibernate这样的 ORM ,它通常可以更轻松地处理数据库之间的所有小不兼容性。
回答by sudocode
回答by leventov
What you really need is an embedded Java database, you can find a huge list of those here: Java Embedded Databases Comparison
您真正需要的是嵌入式 Java 数据库,您可以在此处找到大量列表:Java 嵌入式数据库比较