mercredi 21 octobre 2015

Storing Columns names and their values in java from SQLITE

I'm trying to save the columns names and their values in an array list of strings, but I have not succeeded yet. Here is the Select function I'm trying to create.

public ArrayList<String[]> sqliteSelect(String sql)
{
    Statement stmt = null;
    ArrayList <String[]> result = new ArrayList<String[]>();

    try {
        stmt = c.createStatement();
        ResultSet rs = stmt.executeQuery(sql);

        int colCount = rs.getMetaData().getColumnCount();

        while(rs.next())
        {
            String[] row = new String[colCount];
            for(int i=0; i<colCount;i++)
            {
                row[i] = rs.getString(i+1);
            }
            result.add(row);
        }
        stmt.close();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return result;
}

Everything looks fine but when I reach the point to add the results I have into the array list, it doesn't push the correct values. How can I optimize this so I can get the columns names with their respected values: e.g of the desired list: {<userid,01>,<value,20>}

Aucun commentaire:

Enregistrer un commentaire