支持sql
https://www.mcbbs.net/forum.php?mod=viewthread&tid=783267&extra=page%3D1%26filter%3Dtypeid%26typeid%3D1029
支持sqlite
https://github.com/StartZYP/Minecarft_PexTime/blob/master/src/Top/q44920040/Minecarft_PexTime/Dao.java
SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快
不像常见的客户-服务器范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要部分。所以主要的通信协议是在编程语言内的直接API调用。这在消耗总量、延迟时间和整体简单性上有积极的作用。整个数据库(定义、表、索引和数据本身)都在宿主主机上存储在一个单一的文件中。它的简单的设计是通过在开始一个事务的时候锁定整个数据文件而完成的。
物品序列化存储https://github.com/StartZYP/ReplaceItem/blob/master/src/main/java/com/ipedg/minecraft/replaceitem/util/Utils.java
以我所写的成就奖励系统为例,需要存储一个玩家的成就点数,可以进行增删查改,以实现一个不同成就奖励的解锁:
main方法中:
// An highlighted block
new BukkitRunnable() {
@Override
public void run() {
SQLManager.get().enableMySQL();
}
}.runTaskAsynchronously(this);
SQLManager:数据的操作与游戏中事件及状态交互
package com.project.minecraft.dao;
import com.project.minecraft.main;
import org.bukkit.command.CommandSender;
import java.sql.*;
public class SQLManager {
public static SQLManager instance = null;
private static Connection connection = null;
public static SQLManager get(){
return instance == null ? instance = new SQLManager(): instance;
}
public void enableMySQL()
{
connectMySQL();
String cmd = SQLCommandCJ.CREATE_TABLE.commandToString();
try {
PreparedStatement ps = connection.prepareStatement(cmd);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
private void connectMySQL(){
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:"+ main.plugin.getConfig().getString("dbName")+".db");
}catch (SQLException | ClassNotFoundException e){
e.printStackTrace();
}
}
public void doCommand(PreparedStatement ps)
{
try {
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void shutdown(){
try{
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
public boolean HasPlayerData(String PlayerName){
boolean hasplayer = false;
try {
String cmd = SQLCommandCJ.HAS_Chengjiu.commandToString();
PreparedStatement preparedStatement = connection.prepareStatement(cmd);
preparedStatement.setString(1,PlayerName);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
hasplayer = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return hasplayer;
}
public void insertData(String PlayerName){
try{
String cmd = SQLCommandCJ.INSERT_VALUE.commandToString();
PreparedStatement preparedStatement = connection.prepareStatement(cmd);
preparedStatement.setString(1,PlayerName);
doCommand(preparedStatement);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void UpDataChengjiu(String PlayerName,int ap){
try{
String cmd = SQLCommandCJ.UPDATE_VALUE.commandToString();
PreparedStatement preparedStatement = connection.prepareStatement(cmd);
preparedStatement.setInt(1,ap);
preparedStatement.setString(2,PlayerName);
doCommand(preparedStatement);
} catch (SQLException e) {
e.printStackTrace();
}
}
public int FindChengjiu(String PlayerName){
int ap = 0;
try{
String cmd = SQLCommandCJ.FIND_Chengjiu.commandToString();
PreparedStatement preparedStatement = connection.prepareStatement(cmd);
preparedStatement.setString(1,PlayerName);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
ap = resultSet.getInt("ap");
}
} catch (SQLException e) {
e.printStackTrace();
}
return ap;
}
}
SQLCommandCJ:实现增删查改
package com.project.minecraft.dao;
public enum SQLCommandCJ {
CREATE_TABLE(
"CREATE TABLE IF NOT EXISTS `Chengjiu` (" +
" 'PlayerName' VARCHAR(30) DEFAULT NULL," +
" 'ap' int DEFAULT 0,"
),
INSERT_VALUE(
"INSERT INTO 'Chengjiu' ('PlayerName','ap')" +
" VALUES "
),
HAS_Chengjiu(
"SELECT PlayerName FROM 'Chengjiu' WHERE PlayerName = ?"
),
FIND_Chengjiu(
"SELECT ap FROM 'Chengjiu' WHERE PlayerName = ?"
),
UPDATE_VALUE(
"UPDATE Chengjiu SET ap = ? WHERE PlayerName = ?"
);
private String command;
SQLCommandCJ(String command) {
this.command = command;
}
public String commandToString(){
return command;
}
}