htorun/dbtableprinter

Can you help me trying to hide the third value of a column from the table please?

Closed this issue · 6 comments

I have this:
+--------+------------+---------------------------------------+-----------------+------------------+\n
| iduser | username | password | date_of_birth | address |\n
+--------+-----------+----------------------------------------+------------------+-----------------+\n
| 1 | Admin | e3afed047b08059d0fada10f40c1e5 | null | Remote Access |\n
+--------+-----------+-----------------------------------------+-----------------+-----------------+\n
| 2 | dsadsad | 2dc114fc296ad82d4cd637bd2311cf7 | dd/mm/yyyy | Nº Street |\n
+--------+-----------+-----------------------------------------+-----------------+-----------------+\n

and I need this:

+--------+------------+----------+-----------------+-----------------+\n
| iduser | username | password | date_of_birth | address |\n
+--------+------------+----------+-----------------+-----------------+\n
| 1 | Admin | ****** | null | Remote Access |\n
+--------+------------+----------+-----------------+----------------+\n
| 2 | dsadsad | ****** | dd/mm/yyyy | Nº Street |\n
+--------+------------+----------+-----------------+----------------+\n

, OR I need to remove the 3rd column from that table to not show it...
I tried to treat that as a string and converted to char array and checked the indexes where i had to remove and add to a new array, but it seems like it is not working properly and is still showing me everything like before...

Here's the code, have to change to .java because it was not letting me submit it

hideP.txt

Also tried this on your code, but it's hard to understand it
if(rsmd.getColumnLabel(i).equals("password")) continue;

Hi Jessica,
Well, if you want to exclude the "password" column, you can do it with SQL:
Instead of

SELECT * FROM ...

use something like

SELECT iduser, username, date_of_birth, address FROM ...

If you want the column but hide the password, you need to replace the column values with "*****" in the code where you iterate over the result set:

// Go through each row, get values of each column and adjust
// column widths.
int rowCount = 0;
while (rs.next()) {

    // NOTE: columnIndex for rs.getXXX methods STARTS AT 1 NOT 0
    for (int i = 0; i < columnCount; i++) {
        Column c = columns.get(i);
        String value;
        int category = c.getTypeCategory();

        if (category == CATEGORY_OTHER) {

            // Use generic SQL type name instead of the actual value
            // for column types BLOB, BINARY etc.
            value = "(" + c.getTypeName() + ")";

        } else {
            // Change the following ...
            //value = rs.getString(i+1) == null ? "NULL" : rs.getString(i+1);

            // ... to something like
            value = rs.getString(i+1);
            if (value == null) {
                value = "NULL";
            } else if (c.getLabel() == "password") {
                value = "********";
            }
        }

Hope this helps.

No problem at all. I was happy to help.

Just comparing strings, must use equals and not compare the pointers with ==

  if (value == null) {
                        value = "NULL";
                    } else if (c.getLabel().equals("password")) {
                        value = "********";
                    }

The rest is perfect, thanks!