1

I'm printing my output from MySQL queries in my console, and into the text area in my gui application. Right now, the columns get messed up in the text area any time a name is longer than the width of its column. How can I fix the segment of code below to make the output to the text area look uniform?

else if (e.getSource() == executeButton) {
    //Create a statement
    try{
        String commandString = enterSQLText.getText();
        Statement statement = conn.createStatement();
        //Execute a statement
        ResultSet resultSet = statement.executeQuery(commandString);
        //Process query results
        ResultSetMetaData metaData = resultSet.getMetaData();
        int numberOfColumns = metaData.getColumnCount();

        for(int i = 1; i<= numberOfColumns; i++){
            executionResultText.append(metaData.getColumnName(i) + "\t");
            System.out.printf("%20s\t", metaData.getColumnName(i));
        }
        executionResultText.append("\n");
        System.out.println();

        while (resultSet.next()){
            for (int i = 1; i <= numberOfColumns; i++){
                executionResultText.append(resultSet.getObject(i).toString() + "\t");
                System.out.printf("%20s\t", resultSet.getObject(i));
            }
            executionResultText.append("\n");
            System.out.println();
        }
        System.out.println("Execute Button Works!");
    }
    catch (Exception h){
        h.printStackTrace();
    }
}

I appreciate the help.

1
  • You could use a table instead of a text area. JTables are good at showing data that has come from database tables. Commented Oct 25, 2012 at 21:24

1 Answer 1

1

It's best to use one of the Loggers to do the formatting for you. It is very hard to control the tab spacing and vertical column alignment in a console without using some kind of text formatting library. A Logger can optionally store your output in a text file which you can use to view. It may also do a pretty good job of formatting the output.

Here is the most popular logger:

http://www.slf4j.org/

If you must output to console yourself, you need to use a Console Output Library. Take a look at this, looks promising:

http://www.codeproject.com/Articles/24896/JLib-A-Windows-Console-Library

Sign up to request clarification or add additional context in comments.

3 Comments

Those are some pretty complex solutions. I'm surprised there isn't a simple fix to this :-/
it doesnt need to be extravagant, it just needs to display the output in columns without them overlapping, thanks for the suggestions though.
Unfortunately, the console interface is neglected by the standards community. The only std I can think of is VT200/220 and MS-DOS does not care for that, so effectively, we have no console drivers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.