Search code examples
javasqlrowsderby

Get all rows from table in Java derby


I am trying to get all rows in a table that is already created and filled. This is the function I am currently attempting to do, obviously it doesn't work. Puts out nothing.

    public static void getTable(String[] table) throws ClassNotFoundException, SQLException{
    DatabaseMetaData metadata = null;
    Class.forName(driver);
    conn = DriverManager.getConnection(connectionURL);  

    metadata = conn.getMetaData();
    ResultSet res = metadata.getTables(null, null, null, table);

    while(res.next()){
        System.out.println(res.toString());
    }
    res.close();
    conn.close();
}

Any ideas what I am doing wrong?


Whew, After a few hours of playing around with combinations. I have SOMETHING. It will give me them all in irritation, but for those who might have this KIND of issue, here is what I came up with that will get the data from the entire table.

EDIT: Now returns a HashMap with the row number and values separated by ','.

    /**
 * Gets all rows of a table. Returns a HashMap.
 * 
 * Returns a HashMap with each table row value separated by ','. 
 * Each row in a new count in the hashmap. Example:
 * 
 * Returns:
 * 
 * <1, "FirstName,LastName,Age">
 * 
 * 
 * @param table
 * @return HashMap<Int, String>
 * @throws ClassNotFoundException
 * @throws SQLException
 */
@SuppressWarnings("null")
public static HashMap<Integer, String> getTable(String table) throws ClassNotFoundException, SQLException{
    DatabaseMetaData metadata = null;
    Class.forName(driver);
    conn = DriverManager.getConnection(connectionURL);  

    String sql = "select * from " + table;  // use actual name of the table
    PreparedStatement statement = conn.prepareStatement(sql);
    ResultSet res = statement.executeQuery();
    ResultSetMetaData md = res.getMetaData();
    HashMap<Integer,String> hash = new HashMap();
    int count = md.getColumnCount();
    String done;
    int y = 1;
    while(res.next()){
        for(int x = 1; x < md.getColumnCount(); x = x+md.getColumnCount()){
            done = res.getObject(x).toString() + "," + res.getObject(x+1).toString();
        }
        hash.put(y, done);
        y++;
    }
    res.close();
    conn.close();
    if(!hash.isEmpty())
        return hash;
    else
        return null;
}

Solution

  • You can use a sql statement and execute it, something like:

    String sql = "select * from <nameOfTable>";  // use actual name of the table
    PreparedStatement statement = connection.prepareStatement(sql);
    ResultSet rs = statement.execute();
    

    and this does what you are asking about.

    Now, it is not clear that you know what you will do next. Since you say you are trying to avoid knowing the names of the columns, then you will in fact have to query the metadata to get those names. You can get data from the resultset by column position, but you cannot know what the data types of the columns are (string, int, blob).

    You can iterate through the columns obtained from the metadata to find out their names, and (perhaps more importantly) their data types, so that you can call getInt or getString on the result set for each column you want to obtain.

    So though this may be the answer to the actual question, doing anything useful with it is going to require a lot more logic. We can help you with that, but right now I'm only guessing that you want to do that...