Solved

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

Posted on 2008-10-20
3
450 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
[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
  • 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

Industry Leaders: 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 determine if a string is a valid SHA value 7 57
ejb entity bean example issue 2 43
hashmap order 17 63
IBM TS2900 (3572) Tape Autoloader Java? 12 109
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 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