我试图获得一个Bukkit插件来连接到MySQL服务器以存储数据,但是即使我将连接器放在构建路径中,它仍然给出java.lang.ClassNotFoundException:com.mysql.cj。 jdbc.Driver在创建预准备语句的一行。
我对SQL还是很陌生,因此可以提供任何帮助。
onEnable():
@Override
public void onEnable() {
try {
getConnection();
} catch (Exception e) {
e.printStackTrace();
System.out.println("SQL Server connection failed, stopping server");
Bukkit.getServer().shutdown();
}
try {
String[] names = { "uuid", "cash" };
String[] types = { "VARCHAR(50) NOT NULL PRIMARY_KEY", "DECIMAL(18,2)" };
createTable("CashBalance", names, types);
} catch (Exception e) {
e.printStackTrace();
}
}
createTable():
public static void createTable(String tableName, String[] columnNames, String[] columnTypes) {
if (tableName != null && columnNames.length > 0 && columnNames.length == columnTypes.length) {
String SQLmakeTable = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnNames[0] + " " + columnTypes[0];
for (int i = 1; i < columnNames.length; i++) {
SQLmakeTable = SQLmakeTable + ", " + columnNames[i] + " " + columnTypes[i];
}
SQLmakeTable = SQLmakeTable + ")";
try {
Connection con = getConnection();
PreparedStatement tableSend = con.prepareStatement(SQLmakeTable);
tableSend.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
stacktrace:
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
[21:04:55 INFO]: java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
[21:04:55 WARN]: java.lang.NullPointerException
[21:04:55 WARN]: at me.TeknoServal.EcoMain.createTable(EcoMain.java:100)
[21:04:55 WARN]: at me.TeknoServal.EcoMain.onEnable(EcoMain.java:42)
[21:04:55 WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:254)
[21:04:55 WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332)
[21:04:55 WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:402)
[21:04:55 WARN]: at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugin(CraftServer.java:421)
[21:04:55 WARN]: at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlugins(CraftServer.java:347)
[21:04:55 WARN]: at net.minecraft.server.v1_13_R2.MinecraftServer.l(MinecraftServer.java:571)
[21:04:55 WARN]: at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:533)
[21:04:55 WARN]: at net.minecraft.server.v1_13_R2.MinecraftServer.a(MinecraftServer.java:411)
[21:04:55 WARN]: at net.minecraft.server.v1_13_R2.DedicatedServer.init(DedicatedServer.java:270)
[21:04:55 WARN]: at net.minecraft.server.v1_13_R2.MinecraftServer.run(MinecraftServer.java:676)
[21:04:55 WARN]: at java.lang.Thread.run(Unknown Source)
启用后,该插件应在SQL数据库中创建一个新表,但根本没有表。
编辑:
连接器版本:mysql-connector-java-8.0.14.jar
测试项目:
public class testing {
public static void main(String[] args) {
String[] columns = { "id", "name" };
String[] types = { "INT NOT NULL PRIMARY KEY AUTO_INCREMENT", "VARCHAR(50)" };
Object[] things = { 1, "George" };
String[] updateColumns = { "id", "name" };
Object[] updateThings = { 3, "Hallo" };
String[] columnSelect = { "name" };
createTable("Blah", columns, types);
insert("Blah", columns, things);
update("Blah", "id", "1", updateColumns, updateThings);
select("Blah", "id", "3", columnSelect);
}
public static void createTable(String tableName, String[] columnNames, String[] columnTypes) {
if (tableName != null && columnNames.length > 0 && columnNames.length == columnTypes.length) {
String SQLmakeTable = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnNames[0] + " "
+ columnTypes[0];
for (int i = 1; i < columnNames.length; i++) {
SQLmakeTable = SQLmakeTable + ", " + columnNames[i] + " " + columnTypes[i];
}
SQLmakeTable = SQLmakeTable + ");";
System.out.println(SQLmakeTable);
}
}
public static void insert(String tableName, String[] columnNames, Object[] contents) {
if (contents.length <= columnNames.length && columnNames.length > 0 && contents.length > 0
&& tableName != null) {
if (contents[0] != null) {
String SQLcolumns = "INSERT INTO " + tableName + " (" + columnNames[0];
String SQLcontents = " VALUES ('" + contents[0] + "'";
if (columnNames.length >= 2) {
for (int i = 1; i < columnNames.length; i++) {
if (contents[i] != null && columnNames[i] != null) {
SQLcolumns = SQLcolumns + ", " + columnNames[i];
SQLcontents = SQLcontents + ", '" + contents[i] + "'";
}
}
}
String SQLmessage = SQLcolumns + ")" + SQLcontents + ");";
System.out.println(SQLmessage);
}
}
}
public static void select(String tableName, String idColumn, String id, String[] columnNames) {
String SQLtable = "SELECT " + columnNames[0];
String SQLdata = " FROM " + tableName;
String SQLlocation = " WHERE " + idColumn + " IN (SELECT DISTINCT " + idColumn + " FROM " + tableName
+ " WHERE " + idColumn + " = " + id + ");";
for (int i = 1; i < columnNames.length; i++) {
if (columnNames[i] != null) {
SQLtable = SQLtable + ", " + columnNames[i];
}
}
String SQLmessage = SQLtable + SQLdata + SQLlocation;
System.out.println(SQLmessage);
}
public static void update(String tableName, String idColumn, String id, String[] columnNames, Object[] contents) {
if (contents.length <= columnNames.length && columnNames.length > 0 && contents.length > 0
&& tableName != null) {
if (contents[0] != null) {
String SQLtable = "UPDATE " + tableName;
String SQLdata = " SET " + columnNames[0] + " = '" + contents[0] + "'";
String SQLlocation = " WHERE " + idColumn + " = " + id + ";";
for (int i = 1; i < columnNames.length; i++) {
if (contents[i] != null && columnNames[i] != null) {
SQLdata = SQLdata + ", " + columnNames[i] + " = '" + contents[i] + "'";
}
}
String SQLmessage = SQLtable + SQLdata + SQLlocation;
System.out.println(SQLmessage);
}
}
}
}
测试输出:
CREATE TABLE IF NOT EXISTS Blah (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50));
INSERT INTO Blah (id, name) VALUES ('1', 'George');
UPDATE Blah SET id = '3', name = 'Hallo' WHERE id = 1;
SELECT name FROM Blah WHERE id IN (SELECT DISTINCT id FROM Blah WHERE id = 3);
GetConnection():
public static Connection getConnection() throws Exception {
try {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/TestBukkit";
String username = "javaUser";
String password = "FunTimesWithJava";
Class.forName(driver);
Connection con = DriverManager.getConnection(url, username, password);
return con;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
答案 0 :(得分:0)
请检查连接器jar文件的版本。
已更新
我认为使用String SQLmakeTable创建查询存在问题。因为在for循环中,每次都会附加““如果不存在则创建表。”。因此,请更正用于创建表的查询的逻辑。
String colDetail = "";
for (int i = 1; i < columnNames.length; i++) {
colDetail = colDetail+ ", " + columnNames[i] + " " + columnTypes[i];
}
SQLmakeTable = SQLmakeTable + colDetail + ")";