Grabbing data from Mysql with JDBC

Discussion in 'Plugin Development' started by southernzax, Jun 9, 2014.

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

    southernzax

    Hi, I have a fairly simple plugin that stores several lines of data in a table..

    Code:java
    1. if(sender instanceof Player){
    2. sender.sendMessage("Congratulations " + name + ", Your profile has been updated! Use /profile " + name + " to view it!");
    3. Connection conn;
    4. try {
    5. conn = DriverManager.getConnection(url, user, pass);
    6.  
    7. PreparedStatement ps = conn.prepareStatement("INSERT INTO `PROFILE`(USERNAME, AGE, GENDER, ORIENTATION) VALUES (?, ?, ?, ?);");
    8. ps.setString(1, args[0]);
    9. ps.setString(2, args[1]);
    10. ps.setString(3, args[2]);
    11. ps.setString(4, args[3]);
    12. ps.executeUpdate();
    13. conn.close(); //Closes the connection
    14. } catch (SQLException e) {
    15. // TODO Auto-generated catch block
    16. e.printStackTrace();
    17. } //Creates the connection
    18.  
    19.  
    20. return true;
    21. }
    22. }
    23.  


    alright so all that works and what not and I am storing the data in the profile table, but how do i go about say retrieving all four once they are stored and possibly then assigning what I grab to a variable or something for displaying?
     
  2. Make a PreparedStatement that uses executeQuery("SELECT `value` FROM `table` WHERE `condition`;"). This will return a ResultSet object. Then, just use resultSetObject.getInt("value"). This also works for more than just integers.
    (with getString(), getBoolean(), etc.)

    Example Code:
    Code:java
    1. public static int getAge(Player p){
    2. PreparedStatement stmt = MySQL.createStatement();
    3. ResultSet results = stmt.executeQuery("SELECT `AGE` FROM `TABLE` WHERE `USERNAME` = `" + p.getName() + "`;");
    4. if(results.hasNext()){
    5. return results.getInt("AGE");
    6. }
    7. return 0;
    8. }


    Happy coding!
     
  3. Offline

    southernzax

    Code:java
    1. ResultSet results = ps.executeQuery("SELECT USERNAME FROM PROFILE WHERE USERNAME = '" + args[0] + "';");


    is line 221 that is erroring out seen here
    Code:java
    1. [17:05:16 ERROR]: null
    2. org.bukkit.command.CommandException: Unhandled exception executing command 'profiles' in plugin Profile v0.2
    3. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-1433]
    4. at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:180) ~[spigot.jar:git-Spigot-1433]
    5. at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServer.java:724) ~[spigot.jar:git-Spigot-1433]
    6. at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerConnection.java:985) [spigot.jar:git-Spigot-1433]
    7. at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:830) [spigot.jar:git-Spigot-1433]
    8. at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java:28) [spigot.jar:git-Spigot-1433]
    9. at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat.java:65) [spigot.jar:git-Spigot-1433]
    10. at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:167) [spigot.jar:git-Spigot-1433]
    11. at net.minecraft.server.v1_7_R3.ServerConnection.c(ServerConnection.java:77) [spigot.jar:git-Spigot-1433]
    12. at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:713) [spigot.jar:git-Spigot-1433]
    13. at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:283) [spigot.jar:git-Spigot-1433]
    14. at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:576) [spigot.jar:git-Spigot-1433]
    15. at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:482) [spigot.jar:git-Spigot-1433]
    16. at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1433]
    17. Caused by: java.lang.NullPointerException
    18. at com.hotmail.taintedones.profile.Profile.onCommand(Profile.java:221) ~[?:?]
    19. at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-1433]
    20. ... 13 more
    21.  



    I want it to grab the data stored in the USERNAME place by the uesrname typed in the command so for example if it was /profiles namehere then args[0] would be nameshere so thats what it looks for in the sql DB but it seems to be erroring out.
     
Thread Status:
Not open for further replies.

Share This Page