2012-07-03 142 views
2

我正在嘗試爲我的Minecraft服務器編寫一個店鋪插件,但每當有人執行/ purchase命令時我都會收到錯誤。com.mysql.jdbc.MysqlDataTruncation:數據截斷:截斷不正確DOUBLE值:',3'

這裏的錯誤:

 
2012-07-03 04:27:28 [SEVERE] com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: ',3' 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3591) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2620) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1662) 
2012-07-03 04:27:28 [SEVERE] at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1581) 
2012-07-03 04:27:28 [SEVERE] at com.Chipmunk9998.Cod.CodCommandExecutor.onCommand(CodCommandExecutor.java:1421) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479) 
2012-07-03 04:27:28 [SEVERE] at com.Chipmunk9998.Cod.CodCommandExecutor.onCommand(CodCommandExecutor.java:1443) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166) 
2012-07-03 04:27:28 [SEVERE] at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:479) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetworkManager.b(NetworkManager.java:229) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:567) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:459) 
2012-07-03 04:27:28 [SEVERE] at net.minecraft.server.ThreadServerApplication.run(SourceFile:492) 

這裏是我的代碼:

if (cmdsender == null) { 
    File weaponFile = new File(plugin.getDataFolder(), "weapons.yml"); 
    FileConfiguration weaponData = YamlConfiguration.loadConfiguration(weaponFile); 

    String sql = "UPDATE account_info SET Money = Money - " 
    + weaponData.getString(args[0] + "." + args[1] + ".Price") 
    + " WHERE Username = '" + args[2] + "'"; 

    try { 
    plugin.st.executeUpdate(sql); 
    } catch (SQLException e) { 
    e.printStackTrace(); 
    } 

    sql = "UPDATE account_info SET Bought_" + args[0] + " = Bought_" + args[0] 
    + " + ," + args[1] + " WHERE Username = '" + args[2] + "'"; 

    try { 
    plugin.st.executeUpdate(sql); 
    } catch (SQLException e) { 
    e.printStackTrace(); 
    } 

    plugin.getServer().getPlayer(args[2]).sendMessage(
    "You have successfully bought the " 
    + weaponData.getString(args[0] + "." + args[1] + ".Name") + "." 
); 

    return true; 
} 

我試着用搜索引擎,但找不到任何幫助。

+0

我猜這與你的參數[]有關。你可以做一個簡單的System.out.println()你的args []來檢查它們的值嗎? – Sujay

+0

只需寫入並檢查SQL如System.out.println(sql);我認爲作爲double的期待的價值作爲逗號的字符串傳遞。所以導致查詢失敗。 –

回答

3

看起來你在這一行錯誤:

sql = "UPDATE account_info SET Bought_" + args[0] + " = Bought_" + args[0] 
    + " + ," + args[1] + " WHERE Username = '" + args[2] + "'"; 

有在args[1]前此字符串錯誤的逗號。如果args[0]args[1]args[2]包含13foo分別接sql將評估爲:

UPDATE account_info SET Bought_1 = Bought_1 + ,3 WHERE Username = 'foo' 

其中明確是一個語法錯誤,並解釋你的錯誤信息Data truncation: Truncated incorrect DOUBLE value: ',3'

+0

我試圖添加逗號作爲分割字符串的一種方式,以便我可以列出它們所有的項目,並讓他們選擇他們想要使用的項目。有沒有辦法解決這個問題,或者我可以放在那裏的其他角色? –

+0

@DavidCarpenter:要做到這一點,您需要使用MySQL的['CONCAT()'](http://dev.mysql.com/doc/en/string-functions.html#function_concat)函數並引用您的字符串文字例如'... = CONCAT(Bought_「+ args [0] +」,',「+ args [1] +」')WHERE ...'。但是,您應該可以[規範化](http://en.wikipedia.org/wiki/Database_normalization)存儲您的數據結構。 '(用戶名,購買)'對在一個單獨的表,然後你可以[加入](http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins。 html)適當的查詢。 – eggyal

-2

我在JSP中使用UPDATE查詢來更新數據庫記錄。 「UPDATE customer_details設置Customer_fname = ?, Customer_lname = ?, Customer_home_address = ?, Customer_office_address = ?, Customer_mobile_no = ?, Customer_city = ?, Customer_state = ?, Customer_dob =?其中USER_ID =?

去年USER_ID的參數

....,我意外寫ps.setString(9,UNAME)這與不同的數據類型不同的可變....其中給我數據截斷的誤差:不正確的doubleValue

我糾正它與ps.setInt(9,uid)這是正確的變量和數據類型,然後錯誤失去了....!!

相關問題