Java - MySQL - 连接到mysql数据库时出错

时间:2014-07-24 09:55:14

标签: java mysql bukkit

尝试连接MySQL时,我的bukkit插件出错了。我试图在互联网上搜索它,但似乎没有人像我一样有问题。


包含错误的代码(在MySQLHandler类中):

public Connection conn = null;

 private String host, port, database,user, password;

 public MySQLHandler(String host, String port, String database, String user, String password) {
  this.host = host; this.port = port; this.database = database; this.user = user; this.password = password;
 }

 String url = "jdbc:mysql://" + host + ":" + port + "/" + database; 

 public synchronized void connect() {

  try {
   conn = DriverManager.getConnection(url, user, password);

   conn.createStatement().execute("CREATE TABLE IF NOT EXISTS `tokens` (`Name` varchar(32), `amount` int)");

  } catch (SQLException e) { e.printStackTrace(); }
 }

我得到的错误:

[11:41:57 WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExce
ption: Cannot load connection class because of underlying exception: 'java.lang.
NumberFormatException: For input string: "null"'.
[11:41:57 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance
0(Native Method)
[11:41:57 WARN]:        at sun.reflect.NativeConstructorAccessorImpl.newInstance
(Unknown Source)
[11:41:57 WARN]:        at sun.reflect.DelegatingConstructorAccessorImpl.newInst
ance(Unknown Source)
[11:41:57 WARN]:        at java.lang.reflect.Constructor.newInstance(Unknown Sou
rce)
[11:41:57 WARN]:        at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
[11:41:57 WARN]:        at com.mysql.jdbc.Util.getInstance(Util.java:382)
[11:41:57 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
ava:1013)
[11:41:57 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
ava:987)
[11:41:57 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
ava:982)
[11:41:57 WARN]:        at com.mysql.jdbc.SQLError.createSQLException(SQLError.j
ava:927)
[11:41:57 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegist
eringDriver.java:314)
[11:41:57 WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
[11:41:57 WARN]:        at java.sql.DriverManager.getConnection(Unknown Source)
[11:41:57 WARN]:        at me.bramhaag.Tokeconomy.MySQLHandler.connect(MySQLHand
ler.java:31)
[11:41:57 WARN]:        at me.bramhaag.Tokeconomy.Main.onEnable(Main.java:63)
[11:41:57 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
in.java:316)
[11:41:57 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
JavaPluginLoader.java:324)
[11:41:57 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
mplePluginManager.java:405)
[11:41:57 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.loadPlugin
(CraftServer.java:478)
[11:41:57 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.enablePlug
ins(CraftServer.java:396)
[11:41:57 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.reload(Cra
ftServer.java:868)
[11:41:57 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:301)
[11:41:57 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
oadCommand.java:24)
[11:41:57 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
mmandMap.java:180)
[11:41:57 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
mmand(CraftServer.java:769)
[11:41:57 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchSe
rverCommand(CraftServer.java:755)
[11:41:57 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.aB(Dedic
atedServer.java:319)
[11:41:57 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
tedServer.java:284)
[11:41:57 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
aftServer.java:583)
[11:41:57 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
craftServer.java:489)
[11:41:57 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
run(SourceFile:628)
[11:41:57 WARN]: Caused by: java.lang.NumberFormatException: For input string: "
null"
[11:41:57 WARN]:        at java.lang.NumberFormatException.forInputString(Unknow
n Source)
[11:41:57 WARN]:        at java.lang.Integer.parseInt(Unknown Source)
[11:41:57 WARN]:        at java.lang.Integer.parseInt(Unknown Source)
[11:41:57 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.port(NonRegisteri
ngDriver.java:831)
[11:41:57 WARN]:        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegist
eringDriver.java:305)
[11:41:57 WARN]:        ... 20 more

有人可以帮助我并发现错误吗?

2 个答案:

答案 0 :(得分:1)

可能url解析器尝试解析端口,但它得到的只是null

url放在构造函数中,并确保实际将某些内容传递给其参数。

答案 1 :(得分:1)

可能由Port引起,因为它为空,请检查answer它可能对您有所帮助