Solved

How can I change this code to display to JOptionPane instead of println?

Posted on 2008-10-25
6
298 Views
Last Modified: 2012-05-05
I need to display this code in JOptionPane.
con = DriverManager.getConnection(url,

"myLogin", "myPassword");

stmt = con.createStatement();

ResultSet rs = stmt.executeQuery(query);

ResultSetMetaData rsmd = rs.getMetaData();

int numberOfColumns = rsmd.getColumnCount();

int rowCount = 1;

while (rs.next()) {

System.out.println("Row " + rowCount + ": ");

for (int i = 1; i <= numberOfColumns; i++) {

System.out.print(" Column " + i + ": ");

System.out.println(rs.getString(i));

}

System.out.println("");

rowCount++;

}

Open in new window

0
Comment
Question by:glynco
  • 4
  • 2
6 Comments
 

Author Comment

by:glynco
Comment Utility
Here is with JOptionPane but how can I display all at once?

int rowCount = 1;

String columncontent;

while (uprsInventory.next()) {

System.out.println("Row " + rowCount + ": ");

for (int i = 1; i <= InventorynumberOfColumns; i++) {

    

                JOptionPane.showMessageDialog(null,

                "Column " +i);

                

                columncontent = uprsInventory.getString(i);

                

                JOptionPane.showMessageDialog(null,

                "column content " + columncontent );

                

                }

rowCount++;
 

}

Open in new window

0
 

Author Comment

by:glynco
Comment Utility
Something like this. But I got errors.
Illegal start of expression.
int rowCount = 1;

String columncontent;

while (uprsInventory.next()) {

System.out.println("Row " + rowCount + ": ");

                

                JOptionPane.showMessageDialog(null,

                        for (int i = 1; i <= InventorynumberOfColumns; i++) {

                        columncontent = uprsInventory.getString(i);

                        "i " + i + "\n "

                        +"column content " + columncontent);                      

                        }

rowCount++;
 

}

Open in new window

0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
You can't look within the message dialog like that.

You can show the output in a different component like a JTable or JComboBox, but if you want to use showMessageDialog you must use the for loop to build a string -- you can use the \n (or \n\r) to make multiple lines like you are trying to do above.

After you have your string variable, you can call:

JOptionPane.showMessageDialog(null, strData);
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:glynco
Comment Utility
You mean I will concatenate all the strings?
0
 

Author Comment

by:glynco
Comment Utility
It works.
But the page generated so big it won't fit the screen.



    int InventorynumberOfColumns = uprsInventoryMetaData.getColumnCount();
 

    String newstring1, newstring2;

newstring2 = "";

while (uprsInventory.next ()) {

for (int i=1; i<=InventorynumberOfColumns; i++) {

String columnName = uprsInventoryMetaData.getColumnLabel(i);

//System.out.print(columnName + ": ");

String columnValue = uprsInventory.getString(i);

//System.out.println(columnValue);

newstring1 = columnName + " " + columnValue;

newstring2 = newstring2 + newstring1 + "\n";

//System.out.println("String1:" + newstring1);
 

}

JOptionPane.showMessageDialog(null, newstring2);

//System.out.println("String2:" + newstring2);

//System.out.println("");

//System.out.println("");

}
 
 

stmtInventory.close();

conn.close();

}

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
I more of a business objects/server-side programmer; therefore, there may be better GUI solutions than my suggestion, but you can try a JTable instead.

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JTable.html

Hope that helps.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now