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

Posted on 2008-10-20
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 ( ) {
                String itemidvar = rs.getString("itemid");
                String itemnamevar = rs.getString("itemname");

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 ( ) { 

                String itemidvar = rs.getString("itemid"); 


                String itemnamevar = rs.getString("itemname"); 




        } catch (Exception e) { 

            System.err.println("Got an exception! "); 





Open in new window

Question by:glynco
  • 2
LVL 20

Accepted Solution

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 ( ) { 

                String itemidvar = rs.getString("itemid"); 

                String itemnamevar = rs.getString("itemname"); 

      Item it = new Item();





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




Open in new window


Author Comment

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

Expert Comment

by:chaitu chaitu
ID: 22764260

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

895 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

16 Experts available now in Live!

Get 1:1 Help Now