0

This code works and it get data from databasae. Take a look at the output and you will see the issue? How can I format this data so cols are nicly display inline. I dont want to use complex table or dataset to output my result. I just want to put "\t" between each cols.

here is the output:

  ID    FIRSTNAME   LASTNAME    USERNAME    AGE 
  2     Dave    Bill    DaveB   30  
  3     Jane    Mark    JaneM   37   
  4     Voo Gorge   VooG    50  




public class ex02 {
public static void main(String[] args) {
    String url = "jdbc:ucanaccess:Database11.accdb";

    Connection con;
    Statement stmt;
    String query = "Select * from user";

    try {
        Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "", "");
        stmt = con.createStatement();
       ResultSet rs = stmt.executeQuery(query);

        ResultSetMetaData rsmd = rs.getMetaData();
        int columns = rsmd.getColumnCount();
        for (int i = 1; i <= columns; i++) {
            System.out.print(rsmd.getColumnLabel(i) + "\t");
        }
        System.out.println();

        // Display columns data from database
        while (rs.next()) {
            for (int i = 1; i <= columns; i++) {
                System.out.print(rs.getString(i) + "\t");
            }
            System.out.println();
        }

        stmt.close();
        con.close();
    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
    }
}

}

user1924249
  • 543
  • 2
  • 10
  • 30

2 Answers2

0

You can look into StringUtils.rightPad() from Apache Commons (or code something equivalent yourself). Using rightPad in combination with restricting the column value to a max length should give you a nicely formatted output.

ghdalum
  • 871
  • 5
  • 17
0

You can left or right justify your columns by a fixed length. Here's an example setting them all to the same justification.

public static void main(String[] args) throws Exception {
    List<String[]> dataTable = new ArrayList() {{
        add(new String[] {"ID", "FIRSTNAME", "LASTNAME", "USERNAME", "AGE"}); 
        add(new String[] {"2", "Dave", "Bill", "DaveB", "30"});  
        add(new String[] {"3", "Jane", "Mark", "JaneM", "37"});   
        add(new String[] {"4", "Voo", "Gorge", "VooG", "50"});  
    }};

    // Left justified 15 character width
    System.out.println("Left Justified");
    for (String[] row : dataTable) {
        for (String col : row) {
            System.out.printf("%-15s", col);
        }
        System.out.println();
    }
    System.out.println();

    // Right justified 15 character width
    System.out.println("Right Justified");
    for (String[] row : dataTable) {
        for (String col : row) {
            System.out.printf("%15s", col);
        }
        System.out.println();
    }
}

Results:

Left Justified
ID             FIRSTNAME      LASTNAME       USERNAME       AGE            
2              Dave           Bill           DaveB          30             
3              Jane           Mark           JaneM          37             
4              Voo            Gorge          VooG           50             

Right Justified
             ID      FIRSTNAME       LASTNAME       USERNAME            AGE
              2           Dave           Bill          DaveB             30
              3           Jane           Mark          JaneM             37
              4            Voo          Gorge           VooG             50
Shar1er80
  • 8,751
  • 2
  • 16
  • 25