Solved

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

Posted on 2008-10-25
6
310 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 

Author Comment

by:glynco
ID: 22805811
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
ID: 22805821
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 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22805884
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:glynco
ID: 22805900
You mean I will concatenate all the strings?
0
 

Author Comment

by:glynco
ID: 22805926
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 60

Expert Comment

by:Kevin Cross
ID: 22805947
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to execute a Python program and gather return output in Java 2 60
Setup GlassFish 4 55
Java class and jar 3 58
ArrayList Adding/settign data 3 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

739 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