Solved YamlConfiguration#loadFromString issue

Discussion in 'Spigot Plugin Development' started by FyrenGaming, Mar 15, 2020.

  1. So, I am trying to store an item in a YamlConfiguration, and then store that configuration in a SQLite .db file. I have that working just fine, but when I try to retrieve the configuration from the database, and for some reason it is giving me the error below.


    Code (Java):

    public ArrayList<ItemStack> getItems(Integer invID){

        final String SQL = "SELECT Item FROM Items WHERE(invID = '" + invID + "');";
        ArrayList<String> configs = new ArrayList<String>();
        ArrayList<ItemStack> items = new ArrayList<ItemStack>();
        YamlConfiguration conf = new YamlConfiguration();

        Connection conn = getSQLConnection();
        try{
               PreparedStatement ps = conn.prepareStatement(SQL);
               ResultSet rs = ps.executeQuery();
               while(rs.next()){
               configs.add(rs.getString("Item"));
        }

        }catch(SQLException e){
            e.printStackTrace();
        }
        if(configs.size() > 0){
        for(String strings : configs){
            try{
                conf = conf.loadFromString(strings);
            }
            catch(InvalidConfigurationException e){
                e.printStackTrace();

            }
                ItemStack item = conf.getItemStack("i");
                items.add(item);
            }
        }
            return items;

        }
     
    Code (Java):

    Type mismatch: cannot convert from void to YamlConfiguration
     
     
  2. SteelPhoenix

    Moderator

    conf = conf.loadFromString(strings);
    #loadFromString() returns nothing (void).

    Also, when working with SQL use PreparedStatements, and why are you storing a yaml dump in a sql database?
     
    #2 SteelPhoenix, Mar 15, 2020
    Last edited: Mar 15, 2020
  3. I am using a prepared statement.
    Code (Java):

    PreparedStatement ps = conn.prepareStatement(SQL);
     
    What do you mean by a yaml dump?

    ***UPDATE***
    For those in the future that might run into the same issue I was having, Thank you to SteelPheonix, I did not realize what you meant by your post until now.

    Instead of
    Code (Java):

    conf = conf.loadFromString(strings)
     
    it should be:
    Code (Java):

    conf.loadFromString(strings);
     
     
    #3 FyrenGaming, Mar 15, 2020
    Last edited: Mar 15, 2020
  4. This is not using PreparedStatements properly
     
  5. I do not see how it is any different than what the oracle tutorial on prepared statements uses:

    Code (Java):
    Creating a PreparedStatement Object

    The following creates a PreparedStatement object that takes two input parameters:

    String updateString =
        "update " + dbName + ".COFFEES " +
        "set SALES = ? where COF_NAME = ?";
    updateSales = con.prepareStatement(updateString);