Solved

How can I show records on a JOption Window instead of a println?

Posted on 2008-10-20
3
448 Views
Last Modified: 2012-05-05
I have the input from the user as JOption but how can I show the results also on a JOption Window inested of the println code?

            while ( rs.next() ) {
                String itemidvar = rs.getString("itemid");
                System.out.println(itemidvar);
                String itemnamevar = rs.getString("itemname");
                System.out.println(itemnamevar);
            }


import java.sql.*; 
import javax.swing.*;
public class NewClass {
    
 
  
    public static void main (String[] args) { 
        String itemnamestr;
        
        itemnamestr = JOptionPane.showInputDialog(null, "Enter item name.");
 
        try { 
            String url = "jdbc:mysql://localhost:3306/test1"; 
            Connection conn = DriverManager.getConnection(url,"root","dragon77"); 
            Statement stmt = conn.createStatement(); 
            ResultSet rs; 
  
            rs = stmt.executeQuery("SELECT itemid, itemname FROM inventorytable WHERE itemname = '"+itemnamestr +"'"); 
            
            while ( rs.next() ) { 
                String itemidvar = rs.getString("itemid"); 
                System.out.println(itemidvar); 
                String itemnamevar = rs.getString("itemname"); 
                System.out.println(itemnamevar); 
            } 
            conn.close(); 
        } catch (Exception e) { 
            System.err.println("Got an exception! "); 
            System.err.println(e.getMessage()); 
        } 
    } 
}

Open in new window

0
Comment
Question by:glynco
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 500 total points
ID: 22764205
write a one custom object like Item class.i dont know swing but generic logic is like this...

public class Item
{
private String itemId;
private Sring itemName;

write setter and getter methods;


}
initilize arraylist here;
ArrayList al = new ArrayList();
while ( rs.next() ) { 
                String itemidvar = rs.getString("itemid"); 
                String itemnamevar = rs.getString("itemname"); 
      Item it = new Item();
it.setItemId(itemidvar);
it.setItemNamevar(itemnamevar);
al.add(it);
 
            } 
 
for loop this arraylist in ur JOption Window 
 
like (int i=0;i<al.size();i++)
{
Item it= (Item)al.get(i):
print these values
it.getItemId();
it.getItemName();
}

Open in new window

0
 

Author Comment

by:glynco
ID: 22764236
I need the correct syntax because I am still a beginner learning. I can also do it on Netbeans IDE 6.1
0
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22764260
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
factorial example 4 49
HashTable highest marks enumeration alternative 9 43
Configure a Bean in an XML file 4 42
null output 3 35
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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

828 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