1
ArrayList<String> list_apps = new ArrayList<String>();
    String html = null;

    try
    {
        String username = null;
        String appname= null;
        String ip = null;
        String token = null;

        //connessione
        sqlite.setDbPath(dbPath);
        con = sqlite.connect();
        String query="SELECT Username, AppName, Ip, Token FROM Apps";
        ResultSet rs = con.createStatement().executeQuery(query);
        //

        while(rs.next())
        {
            username = rs.getString("Username");
            appname = rs.getString("AppName");
            ip = rs.getString("Ip");
            token = rs.getString("Token");
            list_apps.add(username);
            list_apps.add(appname);
            list_apps.add(ip);
            list_apps.add(token);
            System.out.println(username + " "+ appname + " " + ip+ " " + token);
        }


        html="<html>" + "<body>" + "<table border ='1'>" +
                "<tr>" + 
                "<td>Username</td>" +
                "<td>App Name</td>" +
                "<td>Ip</td>" +
                "<td>Token</td>" +
                "</tr>";

        for(int i = 0; i<list_apps.size(); i++)
        {
            html+="<td>"+list_apps.get(0).toString() +"</td>";//username
            html+="<td>"+list_apps.get(1).toString() +"</td>";//appname
            html+="<td>"+list_apps.get(2).toString() +"</td>";//ip
            html+="<td>"+list_apps.get(3).toString() +"</td>";//token
            html+="<tr>";
        }
        html+="</table>"+"</body>"+"</html>";

        //System.out.println(html);
        //System.out.println(list_apps.get(0).toString());

    }

I've a problem with the output of an html table with Java code. In my database I've only two element but the output of html is a table of 10 rows of first row of my database.

I don't know if is correct the utility of arraylist, I've to print username,appname,ip,token from SQLite database embedded:

    Username    App Name    Ip  Token
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44
lucapelle98 dsdasda 123.54.65.78    [B@2bcce44

Output is only first row of table about database repeated

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You're always fetching the first four elements of the list with `get(0)`, `get(1)`, `get(2)` and `get(3)`. – marstran Jan 07 '19 at 16:41
  • And, unrelated: please read about java naming conventions. You only use the "_" char for SOME_CONSTANT, it does *not* go into any other name. – GhostCat Jan 07 '19 at 16:52
  • 1
    @GhostCat ok, thanks!! –  Jan 07 '19 at 16:57
  • Rehi ;-) ... I didn't mean to delete the whole question. Instead: delete only the answer. Or simply: edit the answer to reflect the solution you found! – GhostCat Jan 07 '19 at 17:32

3 Answers3

2

First, you should create a class called App which contains the data for a single App. Doing this will make the task much easier for you.

class App {
    private final String username;
    private final String appname;
    private final String ip;
    private final String token;

    // Add constructor, getters, toString, equals, hashCode and so on.
}

Now, when you read data from the database, construct instances of this class and put them into the list.

List<App> list_apps = new ArrayList<>();

while(rs.next()) {
    App app = new App(
        rs.getString("Username"),
        rs.getString("AppName"),
        rs.getString("Ip"),
        rs.getString("Token")
    );

    list_apps.add(app);
    System.out.println(app);
}

Now, when you generate your html, you can do this:

for(App app : list_apps) {
    html += "<td>" + app.getUsername() + "</td>";
    html += "<td>" + app.getAppName() + "</td>";
    html += "<td>" + app.getIp() + "</td>";
    html += "<td>" + app.getToken() + "</td>";
    html += "<tr>";
}
marstran
  • 26,413
  • 5
  • 61
  • 67
1

Here:

for(int i = 0; i<list_apps.size(); i++)
    {
        html+="<td>"+list_apps.get(0).toString() +"</td>";//username
        html+="<td>"+list_apps.get(1).toString() +"</td>";//appname
        html+="<td>"+list_apps.get(2).toString() +"</td>";//ip
        html+="<td>"+list_apps.get(3).toString() +"</td>";//token
        html+="<tr>";
    }

What you actually did: appending your "rows" with 4 columns to your list. So, what you want is to have your for loop work in "chunks" of 4 elements, like:

for(int i = 0; i<list_apps.size(); i+=4)
    {
        html+="<td>"+list_apps.get(i).toString() +"</td>";//username
        html+="<td>"+list_apps.get(i+1).toString() +"</td>";//appname
        html+="<td>"+list_apps.get(i+2).toString() +"</td>";//ip
        html+="<td>"+list_apps.get(i+3).toString() +"</td>";//token
        html+="<tr>";
    }

Beyond that, the "real" answer would be: create a class that represents that data, like having member fields username, appname, ip, token. Then create one object for each row, and append that single object to your list. Then iterate that list, and simply print one object at a time!

Your approach is basically "unrolling" all your data into a flat list, and that is in general not a good idea.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    Thanks.. it works well.. I understood the mistake, thnaks you so much! –  Jan 07 '19 at 16:52
  • I've the same problem.. with another arraylist.. `while(rs1.next()) { name = rs1.getString("Name"); desc = rs1.getString("Descrizione"); listserv.add(name); listserv.add(desc); } ` –  Jan 16 '19 at 17:16
  • for(int j = 0; j < listserv.size(); j += 2) { html += ""; html += "" + listserv.get(j).toString() + "";//nome servizio html += "" + listserv.get(j+1).toString() + "";//descrizione html += String.format("", "check", j); html += ""; } –  Jan 16 '19 at 17:16
  • exception ` errore: java.lang.NullPointerException ` –  Jan 16 '19 at 17:17
  • help meeee please –  Jan 16 '19 at 17:27
  • Never put such information into comments. Always update your question. But beware of asking something completely new like that. If you have a new problem, consider writing a new question. And understand that the people you are asking for help are all volunteers with their own priorities. So: never go pleeeease like that. That is rather annoying and childish. – GhostCat Jan 16 '19 at 18:10
  • @lucapellegrini For the NPE, first of all: study this here: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it+ ... in great detail. If you worked through all of that, and you still cant fix it, then put up a new question with a real [mcve], and feel free to ping me then to look into that. But please understand that this is not a tutor forum where people sit down with you to work with you through all the problems of your homework. An essential part of learning to program is to spend the hours required to solve such issues yourself. – GhostCat Jan 16 '19 at 19:13
0

Take a quick look at your code once more. In your for loop:

 for(int i = 0; i<list_apps.size(); i++)

You are referencing the list_apps where you previously did the following: list_apps.add(username); list_apps.add(appname); list_apps.add(ip); list_apps.add(token);

You are outputting a list the size of the variables you had put in. You need to utilize a different counting system.

p.s. the table you are outputting is 8 lines long (your 4 variables added x 2 entries)