arraylist of objects or vector of objects, which is best?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: arraylist of objects or vector of objects, which is best?

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    arraylist of objects or vector of objects, which is best?

    Hello, i am working on a javabean that is connecting to a database and executes an sql string. The javabean returns the records retrieved in a tabular format.
    Currently i can achieve the above requireement, but only through using a simple table-formatted string, this is not dynamic and a 2d array approach would not be dynamic either.

    I have read a little about using a dynamic approach, possiblya vector of objects, or array list of objects to solve the problem, but i don't really know how to start, could anybody help me??

    The code i have so far is:

    package webtech;

    import java.sql.*;

    public class questiona {
    /* Step 1) Initialize Variables*/
    String result = "";
    String query = "select id, name, url, thumb_url, keywords, category, author from mytable;";

    String url = "";
    String driver = "";

    String uname = "";
    String dpass = "";


    /*Step 2) Make a database connection*/
    private Connection dbconn = null;
    public questiona()
    {
    try
    {
    Class.forName(driver);
    dbconn = DriverManager.getConnection(url, uname, dpass);
    /*Create an SQL statement*/
    Statement statement = dbconn.createStatement();

    if (statement.execute(query))
    {/*Step 3) If we have a result lets loop through*/
    ResultSet results = statement.getResultSet();
    ResultSetMetaData metadata = results.getMetaData();
    /*Validate result. Note switch to while loop if we plan on multiple results from query*/
    if(results != null)
    {
    /*Use results setmetadata object to determine the columns*/
    int li_columns = metadata.getColumnCount();




    result += " <tr>\n\r";
    result += " <td>" + metadata.getColumnLabel(1) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(2) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(3) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(4) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(5) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(6) + "</td>\n\r";
    result += " <td>" + metadata.getColumnLabel(7) + "</td>\n\r";
    result += " </tr>\n\r";

    /*loop throught the columns and append data to our table*/


    while(results.next())
    {
    result += " <tr>\n\r";
    result += " <td>" + results.getObject(1).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(2).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(3).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(4).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(5).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(6).toString() +"</td>\n\r";

    result += " <td>" + results.getObject(7).toString() +"</td>\n\r";




    result += " </tr>\n\r";
    }
    }
    }
    }
    catch (ClassNotFoundException e)
    { result = "<tr><td> Error in database";
    result += " <br/>" + e.toString() + "</td></tr>";
    }

    catch (SQLException e)
    { result = "<tr><td> Error in SQL";
    result += " <br/>" + e.toString() + "</td></tr>";
    }
    finally
    {
    try {
    if (dbconn !=null)
    { dbconn.close();}
    }

    catch (SQLException e)
    { result = " <tr><td> Error in closing connection.";
    result += " <br/>" + e.toString() + "</td></tr>";
    }
    }
    }
    public String getResults() {
    return result;
    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    I would go for ArrayList as it is supposedly thread safe....
    add()ing objects to the list is something you should look up in the sun javadoc.
    The same goes for get()ing objects from the list
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center