Solved MySQL: Syntax Error? I think not.

Discussion in 'Plugin Development' started by Build_and_Break, Jun 2, 2014.

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

    Build_and_Break

    Hello,
    After recently curing an error I had that prevented a certain part of a loop from running, it appears that that part in itself has an error. It seems that when it tries to log information to a MySQL database, it gives me some sort of error on line 112. I'm not sure what it means, because it looks perfectly fine to me. The line executes the logging of all the information. I'm pretty sure the info is correctly formatted. Any leads as to what I need to fix are appreciated.

    Main Class:
    Code:java
    1. package me.Build_.Collective;
    2.  
    3. import code.husky.mysql.MySQL;
    4.  
    5. import java.sql.Connection;
    6. import java.sql.PreparedStatement;
    7. import java.sql.ResultSet;
    8. import java.sql.SQLException;
    9. import java.sql.Statement;
    10. import java.util.logging.Logger;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.Location;
    15. import org.bukkit.World;
    16. import org.bukkit.block.Block;
    17. import org.bukkit.command.Command;
    18. import org.bukkit.command.CommandSender;
    19. import org.bukkit.configuration.file.FileConfiguration;
    20. import org.bukkit.entity.Player;
    21. import org.bukkit.plugin.PluginDescriptionFile;
    22. import org.bukkit.plugin.java.JavaPlugin;
    23. import org.bukkit.scheduler.BukkitScheduler;
    24.  
    25.  
    26.  
    27. //-----> Concluded Imports <-----\\
    28. public class Main extends JavaPlugin {
    29. // -----> c-Logger and Plug-in <-----\\
    30. public final Logger logger = Logger.getLogger("Minecraft");
    31. public static Main plugin;
    32. Statement tableSetup;
    33. Statement grabber;
    34. int logCount = Bukkit.getServer().getOnlinePlayers().length;
    35. // -----> Connection variable <-----\\
    36.  
    37. // -----> Disable Setup <-----\\
    38. @Override
    39. public void onDisable() {
    40. saveConfig();
    41. c = null;
    42. PluginDescriptionFile pdfFile = this.getDescription();
    43. this.logger.info(pdfFile.getName()
    44. + " was successfully disabled. Goodbye!");
    45. }
    46.  
    47. // -----> Enable Setup <-----\\
    48. @Override
    49. public void onEnable() {
    50. // -----> Config.<-----\\
    51. FileConfiguration config = getConfig();
    52.  
    53. config.addDefault("Config.Logging.Interval", 15L);
    54. config.addDefault("Config.Database.hostname", "localhost");
    55. config.addDefault("Config.Database.port", "3306");
    56. config.addDefault("Config.Database.database", "minecraft");
    57. config.addDefault("Config.Database.user", "root");
    58. config.addDefault("Config.Database.password", "password");
    59.  
    60. config.options().copyDefaults(true);
    61. saveConfig();
    62. // -----> Grab MySQL Credentials <-----\\
    63. final String dbHost = getConfig().getString("Config.Database.hostname", "localhost");
    64. final String dbPort = getConfig().getString("Config.Database.port", "3306");
    65. final String dbName = getConfig().getString("Config.Database.database", "minecraft");
    66. final String dbUser = getConfig().getString("Config.Database.user", "root");
    67. final String dbPass = getConfig().getString("Config.Database.password", "password");
    68. final MySQL MySQL = new MySQL(this, dbHost, dbPort, dbName, dbUser,
    69. dbPass);
    70. //-----> Setup Tables <-----\\
    71. c = MySQL.openConnection();
    72. try {
    73. getLogger().info("First try statement init.");
    74. tableSetup = MySQL.openConnection().createStatement();
    75. tableSetup
    76. .execute("CREATE TABLE IF NOT EXISTS `LW` (`ID` INTEGER PRIMARY KEY NOT NULL, `ign` TEXT NOT NULL, `w` TEXT NOT NULL, `x` DOUBLE NOT NULL, `y` DOUBLE NOT NULL, `z` DOUBLE NOT NULL)");
    77. getLogger().info("First try statement complete.");
    78. } catch (SQLException e) {
    79. e.printStackTrace();
    80. }
    81. // -----> Main Enabler <-----\\
    82. PluginDescriptionFile pdfFile = this.getDescription();
    83. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    84. + " was successfully enabled!");
    85.  
    86. BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
    87. scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
    88. @Override
    89. public void run() {
    90. // -----> Main Logger <-----\\
    91. try {
    92. getLogger().info("2nd try started.");
    93. log = MySQL
    94. .openConnection()
    95. .prepareStatement(
    96. "INSERT INTO LW (`ign`, `w`, `x`, `y`, `z`) VALUES (?, ?, ?, ?, ?,);");
    97. getLogger().info("2nd try finished.");
    98.  
    99. for (Player p : Bukkit.getOnlinePlayers()) {
    100. Location loc = p.getLocation();
    101. log.setString(1, p.getName());
    102. log.setString(2, loc.getWorld().getName());
    103. log.setNString(3,
    104. String.valueOf(loc.getX()));
    105. log.setNString(4,
    106. String.valueOf(loc.getY()));
    107. log.setNString(5,
    108. String.valueOf(loc.getZ()));
    109. getLogger().info("Beginning log for" + p.getName());
    110. log.execute();
    111. getLogger().info("Data logged for " + p.getName());
    112. }
    113.  
    114. } catch (SQLException e) {
    115. e.printStackTrace();
    116. }
    117. }
    118. }, 0L, 20L * getConfig().getLong("interval",15L));
    119. }
    120.  
    121. // -----> Main Commands <-----\\
    122. @Override
    123. public boolean onCommand(CommandSender sender, Command cmd,
    124. String commandLabel, String[] args) {
    125. Player run = (Player) sender;
    126. World w = run.getWorld();
    127. if (commandLabel.equalsIgnoreCase("lw")) {
    128. if (args.length == 0) {
    129. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    130. + "-------" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    131. + " Location Watchdog " + "[" + ChatColor.GREEN
    132. + "-------" + ChatColor.GOLD + "]");
    133. run.sendMessage(ChatColor.YELLOW
    134. + " Location Watchdog is a plugin by Build_ that ");
    135. run.sendMessage(ChatColor.YELLOW
    136. + " periodically logs all player locations. It can ");
    137. run.sendMessage(ChatColor.YELLOW
    138. + " later show all the previous locations of a player. ");
    139. run.sendMessage(ChatColor.YELLOW
    140. + " Help: /lw help - Thanks for using LW! ");
    141. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    142. + "--------------------------------" + ChatColor.GOLD
    143. + "]");
    144. } else if (args.length == 1) {
    145. if (args[0].equals("help")) {
    146. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    147. + "LW" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    148. + " Commands: ");
    149. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    150. + "LW" + ChatColor.GOLD + "]" + ChatColor.DARK_RED
    151. + " /lw - About Location Watchdog.");
    152. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    153. + "LW" + ChatColor.GOLD + "]" + ChatColor.DARK_RED
    154. + " /lw get - See LW's Bukkit page.");
    155. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    156. + "LW" + ChatColor.GOLD + "]" + ChatColor.DARK_RED
    157. + " /lw help - Shows this page.");
    158. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    159. + "LW" + ChatColor.GOLD + "]" + ChatColor.DARK_RED
    160. + " /lw version - Check LW's installed version.");
    161. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    162. + "LW" + ChatColor.GOLD + "]" + ChatColor.DARK_RED
    163. + " /lw check <player> - Show a players locations.");
    164. run.sendMessage(ChatColor.GOLD
    165. + "["
    166. + ChatColor.GREEN
    167. + "LW"
    168. + ChatColor.GOLD
    169. + "]"
    170. + ChatColor.DARK_RED
    171. + " /lw done <player> - Finish viewing player locations.");
    172. } else if (args[0].equals("get")) {
    173. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    174. + "LW" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    175. + " Bukkit Dev Page: " + ChatColor.DARK_RED
    176. + "dev.bukkit.org/location-watchdog/");
    177. } else if (args[0].equals("version")) {
    178. PluginDescriptionFile pdfFile = this.getDescription();
    179. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    180. + "LW" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    181. + " Installed Version: " + ChatColor.DARK_RED
    182. + pdfFile.getVersion() + " by Build_.");
    183. } else if (args[0].equals("check")) {
    184. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    185. + "LW" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    186. + " Incorrect syntax! " + ChatColor.DARK_RED
    187. + "/lw check <player>");
    188. } else {
    189. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    190. + "LW" + ChatColor.GOLD + "]" + ChatColor.YELLOW
    191. + " Error: " + ChatColor.DARK_RED
    192. + "Unknown command!" + ChatColor.YELLOW
    193. + " Try /lw help.");
    194. }
    195. } else if (args.length == 2) {
    196. if (args[0].equals("check")) {
    197. if (run.hasPermission("lw.staff")) {
    198. String target = args[1];
    199. String world = run.getWorld().getName();
    200. try {
    201. grabber = c.createStatement();
    202. ResultSet res = grabber
    203. .executeQuery("SELECT * FROM LW WHERE ign = '"
    204. + target
    205. + "' AND w = '"
    206. + world
    207. + "';");
    208. if (res.first()) {
    209. getLogger().info("Res.first for /lw check positive.");
    210. while (res.next()) {
    211. getLogger().info("While start.");
    212. int x = res.getInt("x");
    213. int y = res.getInt("y");
    214. int z = res.getInt("z");
    215. run.sendMessage("X: " + x + ", Y: " + y
    216. + ", Z: " + z);
    217. run.sendBlockChange(
    218. new Location(w, x, y, z), 113,
    219. (byte) 0);
    220. run.sendBlockChange(new Location(w, x,
    221. y + 1, z), 89, (byte) 0);
    222. getLogger().info("While end.");
    223. }
    224.  
    225. getLogger().info("While complete.");
    226.  
    227. run.sendMessage(ChatColor.GOLD + "["
    228. + ChatColor.GREEN + "LW"
    229. + ChatColor.GOLD + "]"
    230. + ChatColor.YELLOW
    231. + " Now viewing logged locations for: "
    232. + ChatColor.WHITE + target
    233. + ChatColor.YELLOW + " in the world: "
    234. + ChatColor.WHITE
    235. + run.getWorld().getName()
    236. + ChatColor.YELLOW + ".");
    237.  
    238. } else {
    239.  
    240. run.sendMessage(ChatColor.GOLD + "["
    241. + ChatColor.GREEN + "LW"
    242. + ChatColor.GOLD + "]"
    243. + ChatColor.YELLOW + " Error: "
    244. + ChatColor.WHITE + target
    245. + ChatColor.DARK_RED
    246. + " doesn't have any logged locations in this world!");
    247. getLogger().info("None found, ln. 254");
    248.  
    249. }
    250. } catch (SQLException e) {
    251. e.printStackTrace();
    252. }
    253. } else {
    254. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    255. + "LW" + ChatColor.GOLD + "]"
    256. + ChatColor.YELLOW + " Error:"
    257. + ChatColor.DARK_RED
    258. + " You don't have permission!");
    259. }
    260. } else if (args[0].equals("done")) {
    261. String target = args[1];
    262. String world = run.getWorld().getName();
    263. try {
    264. ResultSet un = grabber
    265. .executeQuery("SELECT * FROM LW WHERE ign = '"
    266. + target
    267. + "' AND w = '"
    268. + world
    269. + "';");
    270. if (un.first()) {
    271. while (un.next()) {
    272. getLogger().info("While start.");
    273. int x = un.getInt("x");
    274. int y = un.getInt("y");
    275. int z = un.getInt("z");
    276. Block b = w.getBlockAt(x, y, z);
    277. run.sendMessage("X: " + x + ", Y: " + y
    278. + ", Z: " + z);
    279. run.sendBlockChange(
    280. new Location(w, x, y, z),
    281. b.getTypeId(), (byte) b.getData());
    282. run.sendBlockChange(new Location(w, x,
    283. y + 1, z), b.getTypeId(), (byte) b
    284. .getData());
    285. getLogger().info("While end.");
    286. }
    287.  
    288. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN
    289. + "LW" + ChatColor.GOLD + "]"
    290. + ChatColor.YELLOW
    291. + " No longer viewing logged locations for: "
    292. + ChatColor.WHITE + target + ChatColor.YELLOW
    293. + " in the world: " + ChatColor.WHITE
    294. + run.getWorld().getName() + ChatColor.YELLOW
    295. + ".");
    296.  
    297. } else {
    298. run.sendMessage(ChatColor.GOLD
    299. + "["
    300. + ChatColor.GREEN
    301. + "LW"
    302. + ChatColor.GOLD
    303. + "]"
    304. + ChatColor.YELLOW
    305. + " Error: "
    306. + ChatColor.DARK_RED
    307. + " That player doesn't have any logged locations in this world!");
    308. getLogger().info("None, ln. 315");
    309. }
    310.  
    311. } catch (SQLException e) {
    312. e.printStackTrace();
    313. }
    314.  
    315. }
    316. } else {
    317. run.sendMessage(ChatColor.GOLD + "[" + ChatColor.GREEN + "LW"
    318. + ChatColor.GOLD + "]" + ChatColor.YELLOW + " Error: "
    319. + ChatColor.DARK_RED + "Incorrect syntax!"
    320. + ChatColor.YELLOW + " Try /lw help.");
    321. }
    322. return false;
    323. }
    324. return false;
    325. }
    326. }


    Errors:
    Code:
    4:07:58 PM [WARNING] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
    4:07:58 PM [WARNING]    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    4:07:58 PM [WARNING]    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    4:07:58 PM [WARNING]    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    4:07:58 PM [WARNING]    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.Util.handleNewInstance(Util.java:407)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.Util.getInstance(Util.java:382)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
    4:07:58 PM [WARNING]    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
    4:07:58 PM [WARNING]    at me.Build_.Collective.Main$1.run(Main.java:112)
    4:07:58 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:58)
    4:07:58 PM [WARNING]    at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    4:07:58 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:635)
    4:07:58 PM [WARNING]    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283)
    4:07:58 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:583)
    4:07:58 PM [WARNING]    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:489)
    4:07:58 PM [WARNING]    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    GitHub with all code: https://github.com/TacticalCactupi/LW

    Also, I can confirm a connection to the MySQL server.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  2. Offline

    Lolmewn

    This doesn't seem quite right: (?, ?, ?, ?, ?,)
     
  3. Offline

    Build_and_Break

    In MySQL, those are filled by the variables declared below it, as I was taught.

    Lolmewn Oh wait, there's an extra comma. I'll remove that and see what happens.

    Well, I'll be. That did it. I can't believe I was so stupid. Thanks for pointing that out. Now I've got a new set of errors, I'm afraid.

    Code:
    5:46:00 PM [WARNING] java.sql.SQLException: Field 'ID' doesn't have a default value
    5:46:00 PM [WARNING]     at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
    5:46:00 PM [WARNING]     at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362)
    5:46:00 PM [WARNING]     at me.Build_.Collective.Main$1.run(Main.java:112)
    5:46:00 PM [WARNING]     at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftTask.run(CraftTask.java:58)
    5:46:00 PM [WARNING]     at org.bukkit.craftbukkit.v1_7_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:345)
    5:46:00 PM [WARNING]     at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:635)
    5:46:00 PM [WARNING]     at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283)
    5:46:00 PM [WARNING]     at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:583)
    5:46:00 PM [WARNING]     at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:489)
    5:46:00 PM [WARNING]     at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 30, 2016
  4. Offline

    RainoBoy97

    Build_and_Break likes this.
  5. Offline

    Lolmewn

    Build_and_Break Your table has an 'ID' field but no default value. I'm guessing you want to make this auto_increment not_null primary_key?
     
    Build_and_Break likes this.
  6. Offline

    Build_and_Break

    I thought primary keys were automatic. How would I go about making it so each log entry would have a primary key besides adding the field in table setup?

    Oh, okay. Thanks, I'll try that.

    Hmm,

    5:59:20 PM [WARNING] com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AUTO INCREMENT NOT NULL PRIMARY KEY, `ign` TEXT NOT NULL, `w` TEXT NOT NULL, `x`' at line 1

    It doesn't like the way I wrote AUTO INCREMENT NOT NULL PRIMARY KEY, but that's how I assigned variables to every other statement.

    Every other variable, correction. Sorry, my edit button never works.

    Solved it! Thanks for the help on the other stuff, guys.

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

Share This Page