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 7621657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:14:59+00:00 2026-05-31T04:14:59+00:00

I have a database function that retrieves data and then populates a jtable. I

  • 0

I have a database function that retrieves data and then populates a jtable. I need to change this function so it RETURNS the data from the database within an Object[][]. How can this be done? (I’m unsure on how to store the data on each row iteration – the while loop part in the code below).

public void data() {
    // clear table then load information 
    DefaultTableModel model=(DefaultTableModel)table.getModel();  
         model.getDataVector().removeAllElements();
  table.repaint(); 
  ResultSet rs=null;
  Statement st=null;

    try { 
        Class.forName("java.sql.Driver"); 
        _con = DriverManager.getConnection(_url,_user,_pwd);
        st = _con.createStatement(); 
        String query = "SELECT * FROM table";

        rs = st.executeQuery(query); 

        while (rs.next()) { 
            String d1 = rs.getString("record1"); 
            String d2 = rs.getString("record2");
            model.addRow(new Object[]{d1,d2}); 
        }        
    } catch (Exception e) { 
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (_con != null) {
                _con.close();
            }
        } catch (SQLException ex) {
        }
    }
}
  • 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-31T04:15:01+00:00Added an answer on May 31, 2026 at 4:15 am

    I don’t think this method is a good idea for several reasons:

    1. A method should do one thing well. You’ve got UI, connection acquisition, and querying all mingled in this one method. I’d start breaking them up a bit more. The persistence layer should not know or care that you’re using Swing.
    2. Close those resources in individual try/catch blocks in the finally block. You still want the others to close if one throws an exception.
    3. An empty catch block is a terrible idea. Print or log the stack trace.
    4. Create the Connection outside this method and pass it in.
    5. ResultSet and Statement should not be member variables of the class. Make them local to the method.

    Just change the method to return what you want instead of void. I wouldn’t use an Object [][]; I’d prefer a List of Maps or some other type that encapsulated a row. Use the column names as the keys in the Map.

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

Sidebar

Related Questions

I have a function that retrieves some data from the database, formats it as
I have the following function that is pulling data from a database. The ajax
I have a function that pulls rows from a database, the content->id and content->type
I have this function on my controller (Im using CodeIgniter) that reads the database,
i have a form that submits data to a database, i have a function
I have a function which calculates distance from database records. This function is called
I have create a simple grid that retrieves data from a server through php.
i have DataBase function that calculate distance by coordinates CREATE OR REPLACE FUNCTION distance(lat1
I have written a function to print database table to an array like this
I have the following function in ruby for retrieving certain information from a 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.