强烈建议使用 Bukkit 的插件数据库 API 来替代此插件。
SQLibrary
SQLibrary 由 PatPeter 从 alta189 接手维护。截至目前,它支持 13 种数据库、一个工厂类以及一个查询构建器。
教程
首先,如果您正在使用 Maven,请将以下元素添加到您的 pom.xml 中:
<repository>
<id>dakani</id>
<name>Dakani Nexus 仓库</name>
<url></url>
</repository>
<dependency>
<groupId>lib.PatPeter.SQLibrary</groupId>
<artifactId>SQLibrary</artifactId>
<version>7.1</version>
</dependency>
除非子类具有更多功能,否则你应始终将实现保存至父类。因此,首先创建你的变量:
构造数据库对象可以硬编码到定义中,也可以在方法中赋值。构造函数不会自动打开数据库连接,以避免抛出 SQLException(尽管这一点曾被考虑过相当长一段时间)。以下是针对最流行的数据库的构造函数:MySQL、SQLite 和 H2。
sql = new MySQL(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
"localhost",
3306,
"myplugin",
"minecraft",
"password1");
sql = new SQLite(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
this.getDataFolder().getAbsolutePath(),
"MyPlugin",
".sqlite");
sql = new H2(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
this.getDataFolder().getAbsolutePath(),
"MyPlugin",
".h2");
对于 SQLite 和 H2,扩展名是可选的,但默认会使用“.db”。对于 MySQL,主机名和端口号是可选的,但会默认为这些值。接下来,我们需要打开数据库连接:
if (sql.open()) {
// ...
}
如果你知道你的插件只会间歇性地使用数据库,你应该先检查它是否已打开:
if (!sql.isOpen()) {
sql.open();
}
如果在打开数据库连接时发生任何错误,它们将被打印到控制台。
支持的数据库
- Firebird
- FrontBase
- DB2
- H2
- Informix
- Ingres
- MaxDB
- MicrosoftSQL
- MySQL
- Mongo
- mSQL
- Oracle
- Ovrimos
- PostgreSQL
- SQLite
安装
- 下载 jar 文件。
- 在 Eclipse 中,进入项目属性 ==> Java 构建路径 ==> 添加外部 Jar,并添加 SQLibrary.jar。
- 告知用户在与你的插件一起安装时也要安装 SQLibrary.jar
请勿从源代码安装!
如果你直接将 SQLibrary 安装到你的插件中,将会出现以下问题:
- 你无法让插件与 SQLibrary.jar 插件并行运行。Bukkit 通常会先在你的插件中找到 SQLibrary 类,从而阻止 SQLibrary.jar 依赖项的运行。
- 你无法让你的插件与其他直接从源代码使用 SQLibrary 的插件并行运行。存在相同的问题。
- 更新肯定会引发这些问题并给你带来诸多麻烦。
从你的插件下载 SQLibrary
你现在可以将此代码放入你的插件中,以便在运行时从你的插件下载并加载 SQLibrary:
特别感谢
链接
I would highly suggest using Bukkit's plugin databases API instead of this plugin.
SQLibrary
SQLibrary was taken over from alta189 by PatPeter. So far it has support for 13 databases, a factory, and one query builder.
Tutorial
First, add these elements to your pom.xml if you're using Maven:
<repository>
<id>dakani</id>
<name>Dakani Nexus Repo</name>
<url></url>
</repository>
<dependency>
<groupId>lib.PatPeter.SQLibrary</groupId>
<artifactId>SQLibrary</artifactId>
<version>7.1</version>
</dependency>
You should always save implementations to a superclass, unless that child class has more functionality. So, start off by creating your variable:
Constructing your database object can be either hardcoded into the definition, or assigned in the method. The constructor does not automatically open the database connection so that it does not have to throw an SQLException (though this was considered for quite some time). Below are the constructors for the most popular databases: MySQL, SQLite, and H2.
sql = new MySQL(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
"localhost",
3306,
"myplugin",
"minecraft",
"password1");
sql = new SQLite(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
this.getDataFolder().getAbsolutePath(),
"MyPlugin",
".sqlite");
sql = new H2(Logger.getLogger("Minecraft"),
"[MyPlugin] ",
this.getDataFolder().getAbsolutePath(),
"MyPlugin",
".h2");
For SQLite and H2, the extension is optional but will default to ".db". For MySQL, the hostname and port number are optional, but will default to those values. Next, we need to open our database connection:
if (sql.open()) {
// ...
}
If you know that your plugin will only use the database intermittently, you should check to see if it is open first:
if (!sql.isOpen()) {
sql.open();
}
If any errors occur while opening the database connection, they will print to console.
Supported Databases
- Firebird
- FrontBase
- DB2
- H2
- Informix
- Ingres
- MaxDB
- MicrosoftSQL
- MySQL
- Mongo
- mSQL
- Oracle
- Ovrimos
- PostgreSQL
- SQLite
Installation
- Download the jar.
- In Eclipse, go to Project Properties ==> Java Build Path ==> Add External Jar and add SQLibrary.jar.
- Tell your users to install SQLibrary.jar along with your plugin
Do not install from source!
If you install SQLibrary directly into your plugin, the following bad things will occur:
- You cannot run your plugin alongside SQLibrary.jar plugins. Bukkit will (usually) find the SQLibrary classes in your plugin first, and block the SQLibrary.jar dependency.
- You cannot run your plugin alongside other plugins using SQLibrary directly from source. Same issue.
- Updating will definitely cause these problems and cause you much pain.
Download SQLibrary from your plugin
You can now place this code in your plugin to download and load SQLibrary from your plugin at runtime:
Special Thanks
- alta189, for starting this library.
- Belphemur, for adding the first version of our factory package.
- omwah, for adding Maven to SQLibrary.
- moose51789, for adding Spout support.
- Mitsugaru, for hosting our Maven repository, finding bugs, and making fixes.
- max9403, for supplying the code to download SQLibrary at runtime.
Links
评论 0
登录后参与评分
还没有评论,来抢沙发吧