SQL Server into Java array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    SQL Server into Java array

    Hi guys, CK here from SQL Server forum.

    Is there a function that will automatically populate a two-dimensional array coming from a SQL Server table? Or should I do it the old fashion "WHILE NOT EOF" way? Am not much of a java programmer but I need this one.

    I can connect to my sql server and query the table. I just need to know how to place it to an array.

    Can anyone give me a sample code or some links?

    Thanks

    -- CK
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    If you are coding this just using JDBC you do the following:

    1. Define a specific class to represent one row of data. In my example, this is BusinessObject.
    2. Store the data in a list rather than an array, because you don't know ahead of time how many rows are in your result.

    Example:

    [CODE=Java]
    ResultSet rs = ...
    try {
    List<BusinessOb ject> result = new ArrayList<Busin essObject>();
    while (rs.next()) {
    BusinessObject row = new BusinessObject( );
    //populate row with data from rs
    result.add(row) ;
    }
    return result;
    } finally {
    yourCloseMethod (rs);
    }
    [/CODE]

    Comment

    Working...