This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Displaying data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Displaying data " Watch "Displaying data " New topic

Displaying data

Ashwin Tadepalli

Joined: Feb 01, 2001
Posts: 27
I am having a question regarding the display of data reteived from a database through a servlet.I am having all the rows of the database table displayed on the command prompt but with the spacing between columns in a row ripped apart because JDBC does that.Now I want to know if there is any method to keep the same spacing as it was before in the database table for all of the columns or use any other better spacing method so that now the display is better organised.To give you an idea of my table, it has got 5 columns with lengths 30,30,integer,20,500.Can anyone please give me an idea on how to solve this problem.
Thanking You,
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

I am not clear on what you want to acheive. If its as simple
as I think, its just display of data in tables. So your servlet
should send it to a html table.
I am not clear.......
- satya

Take a Minute, Donate an Hour, Change a Life
Ashwin Tadepalli

Joined: Feb 01, 2001
Posts: 27
Hi Satya,
Yes, we can definitely use a html table for that but i do not want to use that.I have already used that(html table) for displaying the result on the browser.Now I want another method of displaying the fetched data from the database in a command window(MS-DOS) after running that the java client program.So could you provide me of any alternate solution to this problem.
Thanking you,
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Try XML (extensible markup language).

"JavaRanch, where the deer and the Certified play" - David O'Meara
Ashwin Tadepalli

Joined: Feb 01, 2001
Posts: 27
Hi Cindy,
I do not want to use XML and complicate things.Is there anyway I can do it using only java.Thanking You.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I'm guessing what you want is a method to return a Sting with a minimum width, taking an input String and padding with spaces as necessary. That's pretty simple to write:
public class Test {

public static String padWithSpaces(String input, int size) {
StringBuffer sb = new StringBuffer(input);
while (sb.length() < size) {
sb.append(" ");
return sb.toString();

public static void main(String s[]) {
String[] a = {"red", "blue", "green", "yellow", "orange" };
String[] b = {"alpha", "beta", "gamma", "delta", "epsilon" };
String[] c = {"dog", "cat", "ferret", "parakeet", "lizard" };
for (int i = 0; i < 5; i++) {
System.out.print(padWithSpaces(a[i], 10));
System.out.print(padWithSpaces(b[i], 10));
System.out.print(padWithSpaces(c[i], 10));
And the output lines up nicely:
red alpha dog
blue beta cat
green gamma ferret
yellow delta parakeet
orange epsilon lizard
Of course, this in only useful if you have a fixed-width font. Like, say, at a DOS prompt. And of course you have to decide what to do if the input string is larger than the desired column width. This method simply returns the full input string in that case. If you want you can truncate the string instead; it's up to you.

"I'm not back." - Bill Harding, Twister
Ashwin Tadepalli

Joined: Feb 01, 2001
Posts: 27
Hi Jim,
Thank you for reponding to my question and giving me your thoughtful opinion on it.What you meant was very close to what I have intended to do.I had to make modifications and make use of the padWithSpacesmethod.Anyway again thank you very,very much for helping me out of this problem.Bye.
I agree. Here's the link:
subject: Displaying data
Similar Threads
pictures in a jsp
Interchanging Database columns as rows while displaying in a JTable
Is there a way for updating JTree and JTable with a same model?
Is it possible to give row names and column names for a JTable
Resultset from Action class to JSP ?