connection.getMetaData does not seem to return table info

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

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.
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

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 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.