Josef Lundström
Josef Lundström

Reputation: 197

Handling variables from another class when saving from ArrayList to MySQL database

I am creating a Rpg game. Now, the game progress is saved to a txt file. I have created a database which will handle the save game instead. Currently I trying to connect the save game ArrayList to a MySQL table. All variables in the ArrayList are created in the Player class and inserted into the ArrayList. This is a snippet of the original code:

public void save(Player p) {
    clearData();

    ArrayList<Object> saveGame = new ArrayList<>();
    saveGame.add(p.getName());

Since I have rewritten the code to make it possible to store the data in the database, I am getting syntax errors (can't find symbol) when I am trying to insert "Player p" into the new code. I have try the insert it in the arguments, but I am getting syntax errors. How do I do this, now when the code look like this:

public void save(ArrayList<ArrayList<Object>> saveGame) {

    conn = Sqlprogress.ConnectDB();

    String query = " insert into GameProgress (Name, CurrLoc, Strength, Constitution, Dexterity, Hitpoints, Level, xP, xpToLvlUp, ArmorClass, setNrOfPotions, EqWeapon)"
            + " vaules (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement ps = conn.prepareStatement(insert);
    clearData();

    for (ArrayList<Object> ar1 : saveGame) {
        ps.setString(1, p.getName());                  //Spelarens namn
        ps.setString(2, p.getCurrLoc());               //Spelarens position
        ps.setString(3, p.getStrength());              //Styrka
        ps.setString(4, p.getConstitution());          //Uthållighet
        ps.setString(5, p.getDexterity());             //Smidighet
        ps.setString(6, p.getHitpoints());             //Liv 
        ps.setString(7, p.getLevel());                 //Level
        ps.setString(8, p.xp());                 //Level
        ps.setString(9, p.getxPToLvlUp());             //Erfarenhetsgränsen för att gå upp i level
        ps.setString(10, p.getArmorClass());            //Armorclass
        ps.setString(11, p.getNrOfPotions());           //Antal hälsodrycker
        ps.setString(12, p.getEqWeapon().getName());    //Utrustat vapen (namnet på vapnet sparas)

        //Lägger till allt i inventariet i listan, det är namnen på föremålen sparas
        //Lägger till allt i inventariet i listan, det är namnen på föremålen sparas
        for (Object element : p.getInventory()) {
            if (element instanceof Weapon) {
                saveGame.add(((Weapon) element).getName());
            } else if (element instanceof Item) {
                saveGame.add(((Item) element).getName());
            } else if (element instanceof Armor) {
                saveGame.add(((Armor) element).getName());
            }
        }
    }

Upvotes: 0

Views: 80

Answers (1)

Young Emil
Young Emil

Reputation: 2276

You seems to have corrected all the errors associated with the query and the PreparedStatement.

If you are saying symbols cannot be found then, I suggest you look carefully at your Player p, if p is really intialized properly before passing it to any method (For example the save() method). For proper initialization , mean something like Player p = new Player();.

Okay then if that is done, and error still prevails, then check if the Player Class has the following methods that you are calling on its instance p(as in p.getName()).

If Still prevails, check if Player Class is actually imported if it is from a different package or else where.

Generally, I suggest you try to find out some of the possible causes of problem(Can't find symbol) and the various ways to combat it. So Suggest to you this page and here too, to figure out what may possibly be causing the error and how to fix it.

Upvotes: 1

Related Questions