0

I am using ArrayList to Store Time,Bid and, ASK value from the database. And I am unable to access the data from Arraylist in form of row wise like database. how can I do it.

Here is the code:-

List<String> graphData=new ArrayList<String>();
 stmt = c.createStatement();
                rs = stmt.executeQuery( "SELECT TIME,BID,ASK FROM '"+title+"' ;" );
                System.out.println("Opened database successfully 20");
                List<Double> buyMap=new ArrayList<Double>();
                double buy_avg=0.0, bid=0.0,   temp_profit, cum_prof=0;
                while(rs.next()){
                     i++;
                    graphData.add(rs.getString(1));
                    graphData.add(df.format(rs.getFloat(2)));
                    graphData.add(df.format(rs.getFloat(3)));
                }

Now how can I get the data from graphData. Because I have to put these value in graph.

5 Answers 5

2

Ah, good lord just create an Object.

Example

public class Buy {
    private String firstValue;
    private String secondValue;
    private String thirdValue;
}

Then create a new Buy object with each row from the database..

graphData.add(new Buy(rs.getString(1), df.format(rs.getFloat(2)), df.format(rs.getFloat(3)));

Now you can access it in a pretty way, rather than jumping through an ArrayList in increments of 3.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this:

for (int i=0; i<graphData.size(); i+=3){
   // get elem at i
   // get elem at i+1
   // get elem at i+2
}

But I would recommend you create some bean/POJO object and populate it with
the 3 values from each rs row. Then you add the bean objects to graphData.

Comments

1

How about using a list of lists? In this way, each list would represent a row in the database:

List<List<String>> graphData=new ArrayList<List<String>>();
 stmt = c.createStatement();
                rs = stmt.executeQuery( "SELECT TIME,BID,ASK FROM '"+title+"' ;" );
                System.out.println("Opened database successfully 20");
                List<Double> buyMap=new ArrayList<Double>();
                double buy_avg=0.0, bid=0.0,   temp_profit, cum_prof=0;
                while(rs.next()){
                     i++;
                    List<String> row = new ArrayList<String>();
                    row.add(rs.getString(1));
                    row.add(df.format(rs.getFloat(2)));
                    row.add(df.format(rs.getFloat(3)));
                    graphData.add(row)
                }

To access the data in a row like manner (akin to how they are stored in the database), you could do something like so:

for(List<String> db : graphData)
{
    System.out.println("TIME: " + db.get(0) + " BID " + db.get(1) + " ASK " + db.get(2));
}

Comments

0

Your code:

 List<String> graphData=new ArrayList<String>();
   stmt = c.createStatement();
            rs = stmt.executeQuery( "SELECT TIME,BID,ASK FROM '"+title+"' ;" );
            System.out.println("Opened database successfully 20");
            List<Double> buyMap=new ArrayList<Double>();
            double buy_avg=0.0, bid=0.0,   temp_profit, cum_prof=0;
            while(rs.next()){
                 i++;
                graphData.add(rs.getString(1));
                graphData.add(df.format(rs.getFloat(2)));
                graphData.add(df.format(rs.getFloat(3)));
            }

retrieve data from list:

  for (String string : graphData) {
   System.out.println(string); // prints the values of the list
  }

Comments

0

Why don't you create a class to hold your data?

public class MyData {
private String field1;
private String field2;
private String field3;
public MyData( String f1, String f2, String f3) {
field1=f1;
field2=f2;
field3=f3;
}

// Add getters }

Then read data this way:

List<MyData> graphData=new ArrayList<MyData>();
 stmt = c.createStatement();
                rs = stmt.executeQuery( "SELECT TIME,BID,ASK FROM '"+title+"' ;" );
                System.out.println("Opened database successfully 20");
                List<Double> buyMap=new ArrayList<Double>();
                double buy_avg=0.0, bid=0.0,   temp_profit, cum_prof=0;
                while(rs.next()){
                     i++;
                    graphData.add(new MyData( rs.getString(1), df.format(rs.getFloat(2)), df.format(rs.getFloat(3)));
                }

and read it this way:

for ( MyData data : graphData )
{
   // do something with data
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.