Pushing contents of HashMap to Database :)

Discussion in 'Plugin Development' started by iPhysX, Dec 13, 2011.

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

    iPhysX

    Ok, so I would like to push the contents of my hashmap..
    Code:java
    1. public static HashMap<Player, String> toPush = new HashMap<Player, String>();


    to my database, using this query.

    Code:java
    1. public static void push(Player p, String a) {
    2. String query = "INSERT into ATTRIB_PLAYERS (player, attribute) values = '"+p.getName()+"', '"+a+"';";
    3. try {
    4. Attributes.manageMySQL.insertQuery(query);
    5. } catch (MalformedURLException e) {
    6. e.printStackTrace();
    7. } catch (InstantiationException e) {
    8. e.printStackTrace();
    9. } catch (IllegalAccessException e) {
    10. e.printStackTrace();
    11. }
    12. }


    All i need to know really, is how to extract the key and value from the hashmap to use in this method, i'm no used to using hashmaps :)

    Thanks!
     
  2. So, I don't know mysql, but i'm guessing the table will look like this:
    --------------------------
    Tips48|ValueHere
    OtherName|ValueHere

    So would you just go through each player and get the value?
     
  3. Offline

    iPhysX

    Yeah, i just need to get the player and value from the hashmap. That is what i am unsure about. :)
     
  4. Go through each (row) and the first value is the key and the second is the value, yes?
     
  5. Offline

    iPhysX

    Erm, yes

    Code:java
    1. public static void push(Player p, String a) {
    2. for (int i = 0; i < attributes.size(); i++) {
    3. String query = "INSERT into ATTRIB_PLAYERS (player, attribute) values = ('"+attributes.get(i)+"', '"+???????+"');";
    4. try {
    5. Attributes.manageMySQL.insertQuery(query);
    6. } catch (MalformedURLException e) {
    7. e.printStackTrace();
    8. } catch (InstantiationException e) {
    9. e.printStackTrace();
    10. } catch (IllegalAccessException e) {
    11. e.printStackTrace();
    12. }
    13. }
    14. }


    I'm just confused as how to get the value of the key?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 22, 2016
  6. Ok :)

    Code:java
    1.  
    2. for(Map.Entry<Player, String> entry : toPush.entrySet()){
    3. Player p = entry.getKey();
    4. String s = entry.getValue();
    5. //Your query etc
    6. }
    7.  
     
    iPhysX likes this.
Thread Status:
Not open for further replies.

Share This Page