Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7036531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:26:07+00:00 2026-05-28T01:26:07+00:00

I have defined a variable with Object[] data; How can I fill it up

  • 0

I have defined a variable with

Object[] data;

How can I fill it up with data in the next step?

I want to do something like this:

    public Object[] select() {
   Object[] data; // Here I definded it
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("SELECT * FROM CUSTOMERS");
        while (rs.next()) {

/* data = {(rs.getString("fname"), (rs.getString("lname")); */
// I know it's wrong, but how can I fill it with data from a database?

        }
        rs.close();
        stmt.close();
    } catch (SQLException ex) {
        System.out.println("error while selecting");
        System.err.println(ex);
    }
    return data;
}


// -----
// somewhere else
model.addRow(DB.INSTANCE.select());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:26:08+00:00Added an answer on May 28, 2026 at 1:26 am

    It’s

     data[i++] = new Object[] {rs.getString("fname"), rs.getString("lname")}; 
    

    But frankly I’d look at the option of creating a new class. I even have a fancy name for it: Customer.

    So that your main cycle would look like that:

     while (rs.next()) {
         data[i++] = new Customer(rs.getString("fname"), s.getString("lname"));
     }
    

    Now you may ask what’s i and how do we create the data in the first place? All good questions. You don’t know beforehand how long the result set is going to be, so arrays are not a good idea. Try using List instead:

    public List<Customer> select() {
        List<Customer> data = new ArrayList<Customer>(); // Here you define it
        // some code
            while (rs.next()) {
                data.add(new Customer(rs.getString("fname"), (s.getString("lname")));
            }
        // etc.
        return data;
    }
    

    Note that you can almost read it aloud: data, add a new Customer, please.

    Finally, I highly recommend you to take a look at the finally keyword: it will save you time otherwise spent on strange and intermittent bugs.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have defined a variable like this (C++): static const char str[] =
I want to have a macro dbtest that can be used like this: (dbtest
I have a simple object that holds some [public] data. I want to keep
Can we cast an object with user-defined types, like we do for normal data
I have to extract defined variable and function names from a js code passed
I need to mock a GrailsControllerClass interface. Instance should have a static variable defined.
I have a function, where if a variable is not defined, exit; is called,
I would like to have a variable (or #define ) in C++ source that
I have defined a personalizable property on a web part and I would like
This is what I have: All objects that can be persisted on the database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.