gpt4 book ai didi

连接 MySQL 的 JAVA Bukkit 插件

转载 作者:行者123 更新时间:2023-11-29 22:16:33 26 4
gpt4 key购买 nike

我正在尝试将我的 MineCraft 插件连接到 SQL 数据库。但我好像做不到...我是java编程新手,所以就到这里吧。我真的不知道错误是从哪里来的。我的主 Java 文件..

    package com.TristanCode.MyWebVote;

import java.sql.SQLException;
import org.bukkit.event.Listener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.Connection;
import code.husky.mysql.MySQL;

public class MyWebVote extends JavaPlugin implements Listener {

MySQL sql;
Connection c = null;

//On Server Start/Stop.
public void onEnable() {
System.out.println("[MyWebVote] by TristanCode has been enabled!");
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new Main(this), this);

//Get configuration file
getConfig().options().copyDefaults(true);
saveConfig();

//Activate SQL
connect();
}
public void onDisable() {
System.out.println("[CashCraft] by TristanCode has been disabled!");
}

private synchronized void connect() {
System.out.println("Starting connection...");
sql = new MySQL(this, getConfig().getString("database.host"), getConfig().getString("database.port"), getConfig().getString("database.dbname"), getConfig().getString("database.user"), getConfig().getString("database.pass"));

try {
c = sql.openConnection();
c.prepareStatement("CREATE DATABASE IF NOT EXISTS Tokens").execute();
c.prepareStatement("CREATE TABLE IF NOT EXISTS Tokens (id INTEGER not null,uuid VARCHAR(70) not null,tokens FLOAT(30),PRIMARY KEY (id))").executeUpdate();
System.out.println("Connection has been established successfully! It took ms to connect.");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

}
}

这是主要的错误日志。

[14:38:48 WARN]: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[14:38:48 WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[14:38:48 WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[14:38:48 WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[14:38:48 WARN]: at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[14:38:48 WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[14:38:48 WARN]: at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1116)
[14:38:48 WARN]: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:343)
[14:38:48 WARN]: at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2334)
[14:38:48 WARN]: at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2371)
[14:38:48 WARN]: at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2163)
[14:38:48 WARN]: at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
[14:38:48 WARN]: at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
[14:38:48 WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[14:38:48 WARN]: at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[14:38:48 WARN]: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[14:38:48 WARN]: at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[14:38:48 WARN]: at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[14:38:48 WARN]: at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:378)
[14:38:48 WARN]: at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
[14:38:48 WARN]: at java.sql.DriverManager.getConnection(DriverManager.java:571)
[14:38:48 WARN]: at java.sql.DriverManager.getConnection(DriverManager.java:215)
[14:38:48 WARN]: at code.husky.mysql.MySQL.openConnection(MySQL.java:58)
[14:38:48 WARN]: at com.TristanCode.MyWebVote.MyWebVote.connect(MyWebVote.java:37)
[14:38:48 WARN]: at com.TristanCode.MyWebVote.MyWebVote.onEnable(MyWebVote.java:26)
[14:38:48 WARN]: at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321)
[14:38:48 WARN]: at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:332)
[14:38:48 WARN]: at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:404)
[14:38:48 WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:341)
[14:38:48 WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:313)
[14:38:48 WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:721)
[14:38:48 WARN]: at org.bukkit.Bukkit.reload(Bukkit.java:543)
[14:38:48 WARN]: at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25)
[14:38:48 WARN]: at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140)
[14:38:48 WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621)
[14:38:48 WARN]: at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:607)
[14:38:48 WARN]: at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:371)
[14:38:48 WARN]: at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:336)
[14:38:48 WARN]: at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632)
[14:38:48 WARN]: at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540)
[14:38:48 WARN]: at java.lang.Thread.run(Thread.java:745)
[14:38:48 WARN]: Caused by: java.net.ConnectException: Connection refused
[14:38:48 WARN]: at java.net.PlainSocketImpl.socketConnect(Native Method)
[14:38:48 WARN]: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
[14:38:48 WARN]: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
[14:38:48 WARN]: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
[14:38:48 WARN]: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
[14:38:48 WARN]: at java.net.Socket.connect(Socket.java:579)
[14:38:48 WARN]: at java.net.Socket.connect(Socket.java:528)
[14:38:48 WARN]: at java.net.Socket.<init>(Socket.java:425)
[14:38:48 WARN]: at java.net.Socket.<init>(Socket.java:241)
[14:38:48 WARN]: at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:254)
[14:38:48 WARN]: at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:292)

最佳答案

我从未使用过这种方式连接到 SQL Server,假设您的代码是正确的,请检查您的主机以确保数据库端口已转发,并确保您的 IP 允许远程 SQL。如果一切都已完成并且效果良好,请尝试使用它进行连接。

Connection connection = DriverManager.getConnection("jdbc:mysql://"+getConfig().getString("database.host")+"/"+getConfig().getString("database.port")+"/"+getConfig().getString("database.dbname"), getConfig().getString("database.user"), getConfig().getString("database.pass"));

关于连接 MySQL 的 JAVA Bukkit 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com