使用 Java 创建新的本地数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13692538/
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
Creating a new local database with Java
提问by scubbo
My aim is to create a local database that can be read and written to with Java. I have some experience with manipulating a local sqlite database with Python, and with interacting with existing networked databases on Microsoft Azure via VB.Net, but the Java formulation for creating a database is escaping me.
我的目标是创建一个可以用 Java 读取和写入的本地数据库。我有一些使用 Python 操作本地 sqlite 数据库的经验,以及通过 VB.Net 与 Microsoft Azure 上现有的网络数据库交互的经验,但是用于创建数据库的 Java 公式却让我望而却步。
Most sources (like the JDBC Docs) seem to assume that you are accessing a database through a network protocol, or a database hosted on localhost. My desired implementation is to create and store the database in a file (or collection of files), so that it can be stored and accessed locally, without network connectivity (presumable through the "file:" protocol).
大多数来源(如JDBC Docs)似乎都假设您是通过网络协议访问数据库,或者是在本地主机上托管的数据库。我想要的实现是在一个文件(或文件集合)中创建和存储数据库,以便它可以在本地存储和访问,而无需网络连接(假设通过“文件:”协议)。
The JDBC Tutoriallooks like it will be very useful once I am up and running, but is currently beyond my scope, since I don't even have an existing database yet.
该JDBC教程看起来将是非常有用的,一旦我和运行,但目前超出了我的范围,因为我甚至不具有现有的数据库呢。
Manysourceshave suggested solutions like H2, MySQL, Derby, or Hypersonic DB. However, I'm loath to install extensions (if that's the right term) for a number of reasons:
许多来源都提出了解决方案,如 H2、MySQL、Derby 或 Hypersonic DB。但是,出于多种原因,我不愿意安装扩展程序(如果这是正确的术语):
- This project is initially intended to help me learn my way around Java - widening the scope of the project will dilute my experience with the "base" language and, probably, increase the temptation to engage in "cargo cult programming"
- If this project does ever get distributed to other users (admittedly unlikely, but still!), I don't want to force them into installing more than the core of Java.
- I simply don't know how to install extensions (add-ons? modules?) in Java - one baby-step at a time!
- 这个项目最初是为了帮助我学习 Java 的方式——扩大项目的范围会稀释我对“基础”语言的经验,并且可能会增加从事“货物崇拜编程”的诱惑
- 如果这个项目真的被分发给其他用户(虽然不太可能,但仍然如此!),我不想强迫他们安装 Java 核心以外的内容。
- 我只是不知道如何在 Java 中安装扩展(附加组件?模块?)——一次一个小步骤!
For similar reasons, installing Microsoft SQL Server would not be productive.
出于类似的原因,安装 Microsoft SQL Server 不会有成效。
Thisanswer looks close to what I'm aiming for; however, it gives the error:
这个答案看起来接近我的目标;但是,它给出了错误:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/?user=root&password=rootpassword
and trying "jdbc:file://targetFile.sql" gives a similar error.
并尝试 "jdbc:file://targetFile.sql" 给出了类似的错误。
I've seen the term "embedded" database, which I think is a subset of "local database" (i.e. a local database is stored on the same system - an embedded database is a local database that is only used by a single application) - if I've got those definitions wrong, please feel free to correct me!
我见过术语“嵌入式”数据库,我认为它是“本地数据库”的子集(即本地数据库存储在同一系统上 - 嵌入式数据库是仅由单个应用程序使用的本地数据库) - 如果这些定义有误,请随时纠正我!
回答by Roveris
Most likely, the reason for which you are getting the error, is due to the fact that you are not registering the driver (using reflection...) before actually using it for establishing a connection and so on.
最有可能的是,您收到错误的原因是在实际使用驱动程序建立连接等之前没有注册驱动程序(使用反射...)。
Presumably you will want to do something along the lines of Class.forName("driver")
想必你会想做一些类似的事情 Class.forName("driver")
and then cast that if necessary and then registering it in the DriverManager before calling the getConnection()
method.
然后在必要时进行转换,然后在调用该getConnection()
方法之前将其注册到 DriverManager 中。
Here is a very useful link that might help you out in solving the issue:
这是一个非常有用的链接,可以帮助您解决问题:
http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
However, if you really want to use a local database/file you might want to have a look at SQLite, that might be one way to go about it, although I recommend going for the MySQL approach, as it is a lot easier to configure and learn how stuff works with JDBC.
但是,如果你真的想使用本地数据库/文件,你可能想看看 SQLite,这可能是一种方法,虽然我建议使用 MySQL 方法,因为它更容易配置并了解 JDBC 是如何工作的。
If you are still considering SQLite check this out:
如果您仍在考虑使用 SQLite,请查看:
I see you need some guidance in importing external .jar files into your code (i.e. 3rd party libraries like the ones you will be using for a JDBC driver). Are you using an IDE (e.g. Eclipse, Netbeans, etc.) or are you writing in a text editor and compiling manually?
我看到您需要一些指导来将外部 .jar 文件导入您的代码(即 3rd 方库,例如您将用于 JDBC 驱动程序的库)。您是在使用 IDE(例如 Eclipse、Netbeans 等)还是在文本编辑器中编写并手动编译?
回答by leventov
A number of embedded pure Java databases appeared recently, which have a really simple interface, usually just java.util.Map
, don't involve using JDBC or other SQL artifacts, and store their data in a single file or directory:
最近出现了许多嵌入式纯 Java 数据库,它们的界面非常简单,通常只是java.util.Map
,不涉及使用 JDBC 或其他 SQL 工件,并将它们的数据存储在单个文件或目录中:
The main downside is that most of such databases provide only the simples key-value model.
主要缺点是大多数此类数据库仅提供简单的键值模型。
回答by Bobb Dizzles
DBC can be used with any database that has a JDBC driver, which isn't necessarily a database in "network mode", it can be used with embedded databases as well.
DBC 可以与任何具有 JDBC 驱动程序的数据库一起使用,它不一定是“网络模式”下的数据库,它也可以与嵌入式数据库一起使用。
Here are some Java and embeddable databases:
以下是一些 Java 和可嵌入数据库:
http://www.h2database.com/html/main.html
回答by barracel
Java's JDK does not include any implementation of a database nor drivers to access it. It only provides JDBC as an abstraction to connect to a "database". Is up to you to include all the needed libraries in your code.
Java 的 JDK 不包括任何数据库的实现,也不包括访问它的驱动程序。它仅提供 JDBC 作为连接到“数据库”的抽象。由您决定在您的代码中包含所有需要的库。
If you want to have a self contained code you can simply include the .jar file of a embeddable database in you classpath. That way you can create the instance of the database in your code and minimize the external dependencies.
如果你想要一个自包含的代码,你可以简单地在你的类路径中包含一个可嵌入数据库的 .jar 文件。这样你就可以在你的代码中创建数据库的实例并最小化外部依赖。
You can find herea list of java embeddable databases
您可以在此处找到Java 可嵌入数据库的列表
You can find herean example of how to embed HSQLDB in your code.
您可以在此处找到有关如何在代码中嵌入 HSQLDB 的示例。