Results 1 to 3 of 3

Thread: Is it possible to change this to a vector or array of objects??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Question Is it possible to change this to a vector or array of objects??

    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:

    Code:
    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;
        }
    }
    Last edited by NoteMe; Mar 9th, 2005 at 01:00 PM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Is it possible to change this to a vector or array of objects??

    I would use ArrayList, I think vectors are kind of becoming older and obsolete.

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Re: Is it possible to change this to a vector or array of objects??

    At one time Vectors were synchronized. ArrayLists were not. You could make them snychronized by using some methods from the Collections class. synchronizedList(List<T> list).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width