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

  • Home
  • SEARCH
  • 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 7028741
In Process

The Archive Base Latest Questions

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

I am using a simple app to aid learning database with Apache.Derby and working

  • 0

I am using a simple app to aid learning database with Apache.Derby and working in Eclipse.
The following code runs ok but conn.getMetaData() does not return anything meaningful regarding the table – colnameslist.size for example is 0. However I added meta.getDatabaseProductName() to see what happened and that returns ‘Apache.Derby’ so I guess there is some sort of connection.

The connection url is "jdbc:derby:C:/Users/RonLaptop/MyDB".
The string passed into getTableContents() is "MYENERGYAPP.ENERGYTABLE7".

As it does not error I am at a bit of a loss.

package com.energy;

import javax.swing.*; 
import javax.swing.table.*; 
import java.sql.*; 
import java.util.*;
/** an immutable table model built from getting 
    metadata about a table in a jdbc database 
*/ 
public class JDBCTableModel extends AbstractTableModel {
    Object[][] contents;
    String[] columnNames;
    Class[] columnClasses;

    public JDBCTableModel (Connection conn,
               String string)
        throws SQLException {
        super();
        getTableContents (conn, string);

    }
    protected void getTableContents (Connection conn,
                 String string)
        throws SQLException {

    // get metadata: what columns exist and what
    // types (classes) are they?
    DatabaseMetaData meta = conn.getMetaData();
    String productName = meta.getDatabaseProductName();
    String[] types = null;

    System.out.println ("got meta = " + meta);
    ResultSet results =
        meta.getColumns (null, null, string, null) ;
    System.out.println ("got column results");
    ArrayList colNamesList = new ArrayList();
    ArrayList colClassesList = new ArrayList();
    while (results.next()) {
        colNamesList.add (results.getString ("COLUMN_NAME")); 
        System.out.println ("name: " + 
            results.getString ("COLUMN_NAME"));
        int dbType = results.getInt ("DATA_TYPE");
        switch (dbType) {
        case Types.INTEGER:
    colClassesList.add (Integer.class); break; 
        case Types.FLOAT:
    colClassesList.add (Float.class); break; 
        case Types.DOUBLE: 
        case Types.REAL:
    colClassesList.add (Double.class); break; 
        case Types.DATE: 
        case Types.TIME: 
        case Types.TIMESTAMP:
    colClassesList.add (java.sql.Date.class); break; 
        default:
    colClassesList.add (String.class); break; 
        }; 
        System.out.println ("type: " +
            results.getInt ("DATA_TYPE"));
        }
        columnNames = new String [colNamesList.size()];
        colNamesList.toArray (columnNames);
        columnClasses = new Class [colClassesList.size()];
        colClassesList.toArray (columnClasses);

        // get all data from table and put into
        // contents array

        Statement statement =
    conn.createStatement ();
        results = statement.executeQuery ("SELECT * FROM " +
                      string);

        ArrayList rowList = new ArrayList();
        while (results.next()) {
    ArrayList cellList = new ArrayList(); 
    for (int i = 0; i<columnClasses.length; i++) { 
        Object cellValue = null;


        if (columnClasses[i] == String.class) 
    cellValue = results.getString (columnNames[i]); 
        else if (columnClasses[i] == Integer.class) 
    cellValue = new Integer ( 
            results.getInt (columnNames[i])); 
        else if (columnClasses[i] == Float.class) 
    cellValue = new Float ( 
            results.getInt (columnNames[i])); 
        else if (columnClasses[i] == Double.class) 
    cellValue = new Double ( 
            results.getDouble (columnNames[i]));
        else if (columnClasses[i] == java.sql.Date.class) 
    cellValue = results.getDate (columnNames[i]); 
        else 
    System.out.println ("Can't assign " + 
            columnNames[i]);
        cellList.add (cellValue);
    }// for
    Object[] cells = cellList.toArray();
    rowList.add (cells);

} // while
// finally create contents two-dim array
contents = new Object[rowList.size()] [];
for (int i=0; i<contents.length; i++)

    contents[i] = (Object []) rowList.get (i);
System.out.println ("Created model with " +
           contents.length + " rows");

// close stuff
results.close();
statement.close();

}
// AbstractTableModel methods
public int getRowCount() {
    return contents.length;
}

public int getColumnCount() {
    if (contents.length == 0)
        return 0;
    else
        return contents[0].length;
    }

    public Object getValueAt (int row, int column) {
        return contents [row][column];
    }

    // overrides methods for which AbstractTableModel
    // has trivial implementations

    public Class getColumnClass (int col) {
        return columnClasses [col];
    }

    public String getColumnName (int col) { 
        return columnNames [col]; 
    } 
}
  • 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-28T00:26:54+00:00Added an answer on May 28, 2026 at 12:26 am

    Try using just the name of the table; when I do this, I don’t need to put the name of the ‘app’ level in front of the table name (i.e. just EVERYGYTABLE7). And make sure the table name has that capitalization; it is possible to create a table with a mixed-case name, and I think Derby is strict about that.

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

Sidebar

Related Questions

I am working on a simple budget app using Sinatra and DataMapper in Ruby.
I am running some import code asynchronously from a simple WinForms app using a
I'm learning to develop apps using Qt Creator. I have built a simple app
This is the code I am using to hopefully create a very simple app
I am using Delphi to code a simple app that needs to accept dropped
I have a really simple app I've built using RoR but I'm stuck modifying
I am developing a simple app using GWT, Hibernate, RPC in eclipse. I am
I am using simple date format in my app in following way in the
I want to code simple app, with index and about page, without using db,
I try to create a very simple app using windows API. I've done some

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.