MySQL - Parameter index out of range!?

Discussion in 'Plugin Development' started by bennie3211, Aug 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    bennie3211

    Hi all,

    Today i started to make a global exp points system that I can use on my server so I don't have to write it in every plugin that needs an exp points shop. But now I'm facing some problems, and I can't figure it out :l So Now I come here to ask for help :)

    This is my server log:

    This is my MySQL class:

    Code:java
    1. package MySQL;
    2.  
    3. import java.sql.Connection;
    4. import java.sql.DriverManager;
    5. import java.sql.PreparedStatement;
    6. import java.sql.ResultSet;
    7. import java.sql.SQLException;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.PlayerLoginEvent;
    14. import org.bukkit.plugin.Plugin;
    15.  
    16. public class MySQL implements Listener {
    17.  
    18. //By dutch_kids/GonnaKillYou2 Aka bennie3211
    19.  
    20. public static String databaseName;
    21. public static String dbTableName;
    22. public static String Ip;
    23. public static String Port;
    24. public static String userName;
    25. public static String password;
    26. public static Connection connection;
    27.  
    28. public MySQL(String ip, String port, String user, String pass, String dbName, String tableName) {
    29. userName = user;
    30. password = pass;
    31. Port = port;
    32. Ip = ip;
    33. databaseName = dbName;
    34. dbTableName = tableName;
    35.  
    36. connect();
    37.  
    38. if (connection == null) {
    39.  
    40. System.out.println("[ExpPoints] No connection could be made to the MySQL database. Plugin disabled!");
    41.  
    42. Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("ExpPoints");
    43.  
    44. if (plugin != null) {
    45. plugin.getServer().getPluginManager().disablePlugin(plugin);
    46. }
    47.  
    48. return;
    49. }
    50.  
    51. disconnect();
    52. System.out.println("[ExpPoints] Could successfully connect to database while testing the connection!");
    53. }
    54.  
    55. public MySQL() {
    56. }
    57.  
    58. public static void connect() {
    59.  
    60. try {
    61. connection = DriverManager.getConnection("jdbc:mysql://" + Ip
    62. + ":" + Port + "/" + databaseName, userName, password);
    63. } catch (SQLException e) {
    64. e.printStackTrace();
    65. }
    66. }
    67.  
    68. public static void disconnect() {
    69.  
    70. try {
    71. if (connection != null) {
    72. connection.close();
    73. connection = null;
    74. }
    75.  
    76. } catch (Exception e) {
    77. e.printStackTrace();
    78. }
    79. }
    80.  
    81. public synchronized static boolean databaseContainsPlayer(Player pl) {
    82. try {
    83.  
    84. if (connection == null || connection.isClosed()) {
    85. connect();
    86. }
    87.  
    88. PreparedStatement ps = connection.prepareStatement("Select * FROM `data` WHERE UUID = ?;");
    89.  
    90. ps.setString(1, pl.getUniqueId().toString());
    91.  
    92. ResultSet result = ps.executeQuery();
    93. Boolean bool = result.next();
    94.  
    95. ps.close();
    96. result.close();
    97.  
    98. System.out.println("[ExpPoints] Database contains player " + pl.getName() + ".");
    99.  
    100. return bool;
    101.  
    102. } catch (Exception e) {
    103. e.printStackTrace();
    104. return false;
    105. } finally {
    106. disconnect();
    107. }
    108. }
    109.  
    110. public synchronized static void addPlayerToDatabase(Player pl) {
    111.  
    112. try {
    113.  
    114. if (connection == null || connection.isClosed()) {
    115. connect();
    116. }
    117.  
    118. PreparedStatement ps = connection.prepareStatement("INSERT INTO `" + dbTableName + "` values(?, ?);");
    119.  
    120. ps.setString(1, pl.getUniqueId().toString());
    121. ps.setInt(2, 0);
    122.  
    123. ps.execute();
    124. ps.close();
    125.  
    126. System.out.println("[ExpPoints] Adding player " + pl.getName() + " to the database.");
    127.  
    128. } catch (Exception e) {
    129. e.printStackTrace();
    130. } finally {
    131. disconnect();
    132. }
    133. }
    134.  
    135. public synchronized static void giveExpPoints(Player pl, Integer i) {
    136.  
    137. try {
    138.  
    139. if (!databaseContainsPlayer(pl)) {
    140. addPlayerToDatabase(pl);
    141. }
    142.  
    143. if (connection == null || connection.isClosed()) {
    144. connect();
    145. }
    146.  
    147. int previousExp = 0;
    148.  
    149. PreparedStatement ps = connection.prepareStatement("SELECT Exp FROM '" + dbTableName + "` WHERE UUID = ?;");
    150.  
    151. ps.setString(1, pl.getUniqueId().toString());
    152.  
    153. ResultSet result = ps.executeQuery();
    154. result.next();
    155.  
    156. previousExp = result.getInt("Exp");
    157.  
    158. PreparedStatement update = connection.prepareStatement("UPDATE `" + dbTableName + "` SET Exp = ? WHERE UUID = ?;");
    159.  
    160. update.setInt(1, Integer.valueOf(previousExp + i));
    161. update.setString(2, pl.getUniqueId().toString());
    162. update.executeUpdate();
    163.  
    164. ps.close();
    165. result.close();
    166. update.close();
    167.  
    168. System.out.println("[ExpPoints] Adding " + i + " ExpPoints to player " + pl.getName() + ".");
    169.  
    170. } catch (Exception e) {
    171. e.printStackTrace();
    172. } finally {
    173. disconnect();
    174. }
    175. }
    176.  
    177. public synchronized static int getExpPoints(Player pl) {
    178.  
    179. try {
    180.  
    181. if (!databaseContainsPlayer(pl)) {
    182. addPlayerToDatabase(pl);
    183. }
    184.  
    185. if (connection == null || connection.isClosed()) {
    186. connect();
    187. }
    188.  
    189. PreparedStatement ps = connection.prepareStatement("SELECT Exp FROM `" + dbTableName + "` WHERE UUID = ?;");
    190.  
    191. ps.setString(1, pl.getUniqueId().toString());
    192.  
    193. ResultSet result = ps.executeQuery();
    194. result.next();
    195. Integer total = result.getInt("Exp");
    196.  
    197. ps.close();
    198. result.close();
    199.  
    200. System.out.println("[ExpPoints] Player " + pl.getName() + " has an amount of " + total + " ExpPoints.");
    201.  
    202. return total;
    203.  
    204. } catch (Exception e) {
    205. e.printStackTrace();
    206. return -1;
    207. } finally {
    208. disconnect();
    209. }
    210. }
    211.  
    212. public synchronized static boolean checkExp(Player pl, Integer i) {
    213.  
    214. Integer exp = getExpPoints(pl);
    215.  
    216. if (exp >= i) {
    217. return true;
    218. }
    219.  
    220. return false;
    221. }
    222.  
    223. public synchronized static void takeExp(Player pl, Integer i) {
    224.  
    225. try {
    226.  
    227. if (!databaseContainsPlayer(pl)) {
    228. addPlayerToDatabase(pl);
    229. }
    230.  
    231. if (connection == null || connection.isClosed()) {
    232. connect();
    233. }
    234.  
    235. Integer totalExp = 0;
    236.  
    237. PreparedStatement ps = connection.prepareStatement("SELECT Exp FROM `" + dbTableName + "` WHERE UUID = ?;");
    238.  
    239. ps.setString(1, pl.getUniqueId().toString());
    240. ResultSet result = ps.executeQuery();
    241.  
    242. result.next();
    243.  
    244. totalExp = result.getInt("Exp");
    245.  
    246. totalExp -= i;
    247.  
    248. if (totalExp < 0) {
    249. totalExp = 0;
    250. }
    251.  
    252. PreparedStatement update = connection.prepareStatement("UPDATE `" + dbTableName + "` SET Exp = ? WHERE UUID = ?;");
    253.  
    254. update.setInt(1, Integer.valueOf(totalExp));
    255. update.setString(2, pl.getUniqueId().toString());
    256. update.executeQuery();
    257.  
    258. ps.close();
    259. result.close();
    260. update.close();
    261.  
    262. System.out.println("[ExpPoints] Player " + pl.getName() + " was taken " + i + " ExpPoints.");
    263.  
    264. } catch (Exception e) {
    265. e.printStackTrace();
    266. } finally {
    267. disconnect();
    268. }
    269. }
    270.  
    271. public static synchronized boolean transferExpFromP2P(Player p1, Player p2, Integer i) {
    272.  
    273. if (p1 == null || p2 == null || i == 0) {
    274. return false;
    275. }
    276.  
    277. if (!checkExp(p1, i)) {
    278. return false;
    279. }
    280.  
    281. try {
    282. takeExp(p1, i);
    283. giveExpPoints(p2, i);
    284. return true;
    285.  
    286. } catch (Exception e) {
    287. e.printStackTrace();
    288. return false;
    289. }
    290. }
    291.  
    292. @EventHandler
    293. public void onRegisterPlayerInDatabase(PlayerLoginEvent event) {
    294.  
    295. Player pl = event.getPlayer();
    296.  
    297. if (!databaseContainsPlayer(pl)) {
    298. addPlayerToDatabase(pl);
    299. }
    300. }
    301. }


    And this is how I'm calling the methods (on command):

    Code:java
    1. package Commands;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9.  
    10. import MySQL.MySQL;
    11.  
    12. public class GiveExpCommand implements CommandExecutor {
    13.  
    14. @SuppressWarnings("deprecation")
    15. @Override
    16. public boolean onCommand(CommandSender sender, Command command, String Label, String[] args) {
    17.  
    18.  
    19. if (args.length == 2) {
    20.  
    21. Player pl = Bukkit.getPlayer(args[0]);
    22.  
    23. if (pl == null) {
    24.  
    25. sender.sendMessage(ChatColor.GREEN + "[Exp] " + ChatColor.RED + " Player is offline or doesn't exists!");
    26. return false;
    27. }
    28.  
    29. try {
    30.  
    31. Integer.parseInt(args[1]);
    32. } catch (NumberFormatException e) {
    33.  
    34. sender.sendMessage(ChatColor.GREEN + "[Exp] " + ChatColor.RED + " No integer given.");
    35. return false;
    36. }
    37.  
    38. Integer ExpPointsToGive = Integer.parseInt(args[1]);
    39.  
    40. MySQL.giveExpPoints(pl, ExpPointsToGive);
    41. return true;
    42. }
    43.  
    44. sender.sendMessage(ChatColor.GREEN + "[Exp] " + ChatColor.RED + "No arguments given!");
    45. return false;
    46. }
    47. }


    So does anyone see what I'm doing wrong? I don't see it anyways xD I've looked at it for couple of hours. So if anyone can tell me whats wrong please tell me :)

    PS: I don't know if you need to know this, but the first time I executed the command, it was done in game, the second time through console.

    PSS: Yes, The database and tables and other kind of stuff are working 100%, because adding the player and checking if the player is inside the database works :)

    Up

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page