2014-07-24 92 views
0

我在嘗試連接MySQL時在我的bukkit插件中出現錯誤。我試圖在互聯網上搜索它,但沒有人像我一樣有問題。Java - MySQL - 連接到mysql數據庫時出錯


其中包含錯誤(在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 

有人可以請幫我找到這個錯誤?

+3

你是否意識到你的'url'酒店有JDBC的'值的:mysql://空:空/ null'? – Crozin

+0

我不是一個Java人,但我可以看到你沒有正確初始化你的連接,因爲你從不傳遞主機,端口或數據庫參數。異常說,以及.. –

+0

有人可以告訴我如何給參數的好價值?我將它們的值存儲在config.yml中 – bramhaag

回答

1

可能是url解析器試圖解析端口,但它得到的全部是null

url放入構造函數中,並確保實際傳遞了某些參數。

1

它可能由Port造成的,因爲它爲空,檢查answer它可能幫助你