Couldn't show sql data in jtable, please help

Dear All,
I'm trying to display the data from database in a jtable by following the jtable demo example in Java Tutorials Sample Code; but, I couldn't output the data to the jtable. The following code is what I've done so far. Can anybody please point out a direction for showing the data in jtable? Thanks a lot in advance.
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class Patient {
static String szJdbcURL = "jdbc:oracle:thin:@129.78.110.188:1521:MyDB";                
static String szUser = "...";                
static String szPasswd = "...";                
static String szModelName = "...";
static Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
public void findMedication() throws SQLException{
  Statement stmt1 = null;
  try {
         stmt1=oracle.getConnection().createStatement();             
          String res1=null;
          String res2=null;                
     String sqlQuery="Select * From PatientTable ";
        ResultSet rs= stmt1.executeQuery(sqlQuery);
   if (!rs.isBeforeFirst())
         {System.out.println("OOPS! data not found.");
          System.out.println(); }
   else {
    while (rs.next()) {
   ArrayList<Medication> medList = new ArrayList<Medication>();
   res1=rs.getString("patientName");
   res2=rs.getString("medication");
   Medication med=new Medication (res1, res2);
   medList.add(med);
   System.out.println();
   finally {stmt1.close();}
import java.util.ArrayList;
public class Medication {
    String ptName;
    String ptMedication;
public Medication(String ptName, String ptMedication){
  this.ptName=ptName;
  this.ptMedication=ptMedication;
public String getPtName() {
  return ptName;
public void setPtName(String ptName) {
  this.ptName = ptName;
public String getPtMedication() {
  return ptMedication;
public void setPtMedication(String ptMedication) {
  this.ptMedication = ptMedication;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
public class TableDemo extends JPanel {
  private boolean DEBUG = false;
public TableDemo () {
  super(new GridLayout(1,0));
  ArrayList<Medication> medList = new ArrayList<Medication>();
  //how to add the data to the ArrayList here?
        JTable table = new JTable(new MyTableModel(medList));
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        //Add the scroll pane to this panel.
        add(scrollPane);
  class MyTableModel extends AbstractTableModel {
     private String[] columnNames = new String []{"Patient Name", "Medication"};
     ArrayList<Medication> medList = null;
    public MyTableModel (ArrayList<Medication> medList){
     this.medList=medList;
       public int getColumnCount() {  
     return columnNames.length;
   public int getRowCount() {
    return medList.size();
   public String getColumnName(int columnIndex) {
        return columnNames[columnIndex];
public Object getValueAt(int rowIndex, int columnIndex) {
  Medication object = medList.get(rowIndex);
        switch (columnIndex) {
        case 0:
             return object.getPtName();
        case 1:
             return object.getPtMedication();   
        default:
             return "unknown";
@SuppressWarnings("unchecked")
public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
     private static void createAndShowGUI() {
         JFrame frame = new JFrame("TableDemo");
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         //Create and set up the content pane.
         TableDemo newContentPane = new TableDemo();
         newContentPane.setOpaque(true); //content panes must be opaque
         frame.setContentPane(newContentPane);
         //Display the window.
         frame.pack();
         frame.setVisible(true);
     public static void main(String[] args) {
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                 createAndShowGUI();

This line will create an ArrayList which will be used to store the output data of the sql query with the type is Medication class
NO! Read what TPD ask again carefully -
Please explain (as explicit as you can) what these two lines do.
Did you notice that TWO in there?
Did you notice that your reply said 'This line' when it should have said 'These TWO lines .'?
You are creating a new ArrayList EVERY TIME thru the loop; you are NOT creating ONE ArrayList and adding things to it.
The problem is how to show the ArrayList in the jtable defined in another class.
And the answer is: use one of the THOUSANDS of examples on the internet.
See the trail 'How to Use Tables' in The Java Tutorials
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
Read the trail - try the example. You learn by doing and using code that already works.

Similar Messages

  • My iPhone 5 will not sync with itunes. Icon does no appear in left hand column. Both iTunes and the iPhone are running the most up to date updates. Please help.

    My iPhone 5 will not sync with itunes. Icon does no appear in left hand column. Both iTunes and the iPhone are running the most up to date updates. Please help.

    Is your iPhone connected to your computer using the lightning cable or are you doing WiFi sync?
    Sometimes the iPhone doesn't show up in the side bar if you're using WiFi syncing, if its not working, then be connected to your WiFi on your phone and on your computer (Same network) let me know how it's going, if it doesn't work still. Turn your phone off and on again and put in your pass code then try to sync it (keep your phone unlocked and not in sleep mode) tel me if it appears in the side bar.

  • Errors when applying sql expression on filter, please help

    Hello guys
    I was trying to put a filter on the date column and filter between timestampadd(sql_tsi_day,-31,current_date) and current_date
    I was successfully able to define both field on sql expression of the filter and I selected "is between"
    However when I run reports it returns the following errors:
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 22024] A comparison is being carried out between non-compatible types. (HY000)
    SQL Issued: SELECT "Year"."Transaction Date" saw_0 FROM tranSale_cube WHERE "Year"."Transaction Date" BETWEEN timestampadd(sql_tsi_day,-31,current_date) AND current_date ORDER BY saw_0
    When I using the same timestamp expression on the column formula and run report without filters, the report will return the correct date however though
    I am using essbase as the data source.
    Please help
    Thanks

    Thanks
    However, the error still exist the same.. This error code returns when I run report based on this one single column
    when I put this date column with measures and other columns then run report, I get a different error:
    State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 42043] An external aggregate is found in an outer query block. (HY000)
    SQL Issued: SELECT Product."Brand Name" saw_0, "Up Level"."Trial - level 12" saw_1, "Year"."Transaction Year" saw_2, "Year"."Transactoin Month" saw_3, "Year"."Transaction Date" saw_4, tranSale.count_of_transactions saw_5, Avg(tranSale.count_of_transactions by "Up Level"."Trial - level 12", "Year"."Transactoin Month", Product."Brand Name") saw_6 FROM tranSale_cube WHERE ("Up Level"."Trial - level 12" IN ('0', 'Product Upsell')) AND ("Year"."Transactoin Month" >= '2009 Feb') AND ("Year"."Transaction Date" BETWEEN timestampadd(sql_tsi_day,-31,current_date) AND current_date) ORDER BY saw_0, saw_1, saw_2, saw_3, saw_4
    I believe it must be something with the essbase that is not serving OBIEE well. Because I tried the same filter on a different environment not having essbase, it return me the right results..
    Could anyone help?
    Thanks

  • Ipod will not show up, in iTunes library please help.

    Ipod will not show up, in iTunes library please help.
         I just updated to the newest version of ITunes well, not just but a few days ago. Anyway I have windows 8, and in ITunes my Ipod 5th gen will not show up. I have tried somethings such as unpluging repluging, going to services and restarting, basiclly everything on yoyutube, none still work. It's ***** to cause I just wanted to put music on my Ipod, if anyone has an idea of what to do please give some suggestions.
    Thank you, for trying to help.

    Hello Stephen19wfwake,
    Thanks for using Apple Support Communities.
    For more information on this, take a look at:
    iPod: Appears in Windows but not in iTunes
    http://support.apple.com/kb/TS1363
    1. Try the iPod troubleshooting assistant:
    If you haven't already done so, try the steps in the iPod Troubleshooting Assistant (choose your iPod model from the list).
    If the issue remains after following your iPod's troubleshooting assistant, follow the steps below.
    2. Restart the iPod Service
    3. Restart the Apple Mobile Device Service
    4. Empty your Temp directory and restart
    5. Verify that the Apple Mobile Device USB Driver is installed
    6. Change your iPod's drive letter
    7. Remove and reinstall iTunes
    8. Disable conflicting System Services and Startup Items
    9. Update, Reconfigure, Disable, or Remove Security Software
    10. Deleting damaged or incorrect registry keys
    Best of luck,
    Mario

  • I have iPad 4th Generation with iOS 6.1.3  Since my iBooks was updated to version 3.1.1 I cannot open pdf file attachment in my email.  When I tap of the attachment, the iBooks icon no longer show itself.  Can someone please help?

    I have iPad 4th Generation with iOS 6.1.3  Since my iBooks was updated to version 3.1.1 I cannot open pdf file attachment in my email.  When I tap of the attachment, the iBooks icon no longer show itself.  Can someone please help?

    Thanks for all (Ocean20 & Courcoul) who replied. 
    Reset the device did not help me but it does triggers me to attempt to resend the PDF file attachment on email--and it works the 2nd time.  My problem is now solved.
    The Adobe Reader is very helpful--it allows me to scroll through the document easily, unlike iBooks, where I cannot scroll straight up or down.

  • How to read a file with value of RAW data type? Please help

    Hi Experts,
       I  have a file with RAW data like DE864E48833BFFF1B805001CC4EF4BFA
       I am using GUI_UPLOAD.
       But this FM is throwing error.
       I tried by giving FILETYPE as ASC. The output internal table for this FM contains a field of type c size 32.
       Now it is able to read the file but I want to assign this value to a RAW data type variable.
       This it is unable to do. How to convert the char value to RAW data type?
    Please help!
    Thanks
    Gopal

    Hi,
    The documentation for the function module contains an example for RAW upload.
                begin of itab,
                      raw(255) type x,
                end of itab occurs 0.
               CALL FUNCTION 'GUI_UPLOAD'
               exporting
                  filetype =  'BIN'
                  filename = 'C:\DOWNLOAD.BIN'
               tables
                 data_tab = itab.

  • HT4211 I'm not able to download apps from App Store. It never downloads and it shows waiting status.can somebody please help me resolve this issue

    I'm not able to download apps from App Store. It never downloads and it shows waiting status.can somebody please help me resolve this issue

    I really do not think it is the iPad or your network connection. Other users have reported this very same issue. Try this and see if it works for you - it may not work - but it's easy to try - harmless - and it has worked in the past for others.
    Try signing out of your account and restart your iPad.
    Go to Settings>Store>Apple ID and tap the ID and sign out. Restart the iPad by holding down on the sleep button until the red slider appears and then slide to shut off. To power up hold the sleep button until the Apple logo appears and let go of the button. Go back to Settings>Store> Apple ID and sign in again.

  • I lost my iphone4 and replaced it with a 4s. It's been a year since my last sync. Do I set up my phone as "new" or "restore from backup?" Im hoping game center will automatically have a current record of game data...please help or advise.thx

    I lost my iphone4 and replaced it with a 4s. It's been a year since my last sync. Do I set up my phone as "new" or "restore from backup?" Im hoping game center will automatically have a current record of game data...please help or advise.thx

    Restore from backup.

  • I have got an iPhone MC605J version 6.0 soft bank iphon 4 but unable to use it in India.  I am trying to use Airtel sim but the phone is showing invalid sim.  So please help me out to unlock it.

    I have got an iPhone MC605J version 6.0 soft bank iphon 4 but unable to use it in India.  I am trying to use Airtel sim but the phone is showing invalid sim.  So please help me out to unlock it.

    No one here can help you, & Apple can't/won't help you. Fact is, there is no way to get that iPhone officially unlocked. Only Softbank can authorize the official unlocking of your phone, but they won't. Softbank does not offer official unlocking for any iPhone.
    It is what it is.

  • Iphone4 frozen and none of the buttons are working and it isnt showing up in iTunes either please help!

    my iphone4 keeps freezing on me and now it will not unfreeze. None of the buttons are responding and my iphone will not show up in iTunes. Please help!

    See: If you can't update or restore your iOS device

  • I have an ipod touch 4g. I did not have wifi for three days and when I came home my friends said they texted me when i didnt have signal. The messages i received when i did not have signal are not showing up. Can someone please help me?

    I have an ipod touch 4g. I did not have wifi for three days and when I came home my friends said they texted me when i didnt have signal. The messages i received when i did not have signal are not showing up. Can someone please help me?

    You said "The messages i received when i did not have signal are not showing up."
    How do you know received them?
    If the sender got a message that the Messagers were not delivered than they were never delivered and the only way for you to get them is for the sender to resent them

  • HT204382 hi , i have this extension .f4v  ,and i couldn't match it with any think please help me , thank u

    hi , i have this extension .f4v  ,and i couldn't match it with any think please help me , thank u

    http://en.wikipedia.org/wiki/Flash_Video

  • An app counting minutes and data use. Please help!!!

    An app counting minutes and data use. Please help!!!

    Here's one, there are others in the App store:
    http://itunes.apple.com/us/app/data-usage/id386950560?mt=8
    However, since your carrier is the one that bills you, the only stats that matter are your carrier's. Some carrier's offer apps that provide this info, see if yours does.

  • Whenever I am sending a MSG from my iPhone 4s to friends who don't have I phone or I device my smiley(emoji) seen to be square to them and when they send MSG to me with a smiley my iPhone shows :-) ;-p like symbols. Please help I want to receive smiley

    Whenever I am sending a MSG from my iPhone 4s to friends who don't have I phone or I device my smiley(emoji) seen to be square to them and when they send MSG to me with a smiley my iPhone shows :-) ;-p like symbols. Please help I want to receive smiley from my friend

    I think you answered your own question. As your friends phones are not iPhones, but maybe Nokia or Android the smiley will appear as a different character or box. Thew only way to fix this would be to send a basic smile using the basic keyboard so that your friends will receive it as a smile and you will when they reply. Or ask your friends to check if the phones they are using have the emoji type keyboard available and ask them to update their phones. This will show the smiley when they reply to you.
    Hope this helps.

  • I just got the iphone 4s and my text messages are showing as urgent to others,please help me fix this

    i just got the iphone 4s and my text messages are showing as urgent to others,please help me fix this

    As ckuan said, there's no way to mark messages as urgent from the phone. So, either your carrier is doing something and you'll need to contact them or it't something on the other end. Is it just one person who's seeing your texts as urgent? Or everyone?

Maybe you are looking for

  • Where can i find ram for 13" macbook pro mid 2009?

    I upgraded from 2G to 4G 2-3 years ago. Now I'd like to go to 8G, but Apple doesn't appear to be selling it anymore. Any suggestions for a reliable 3rd party solution? Here's a few specs of my Macbook Pro: MacBook Pro 13-inch, Mid 2009 Processor  2.2

  • Photo App not opening (iPhone iOS 5)

    So, i updated my iPhone 4 to iOS 5, absouletely everything works. I then updated my mother's iPhone 4 to iOS 5, there the exact same phone (I just dont sync photos on the computer with my phone, with hers i do) However on her phone, when u click the

  • Sync problems with iPhoto & iPhone through iTunes.

    I can not sync my iPhoto with my iPhone through iTunes. Error (-50) ? on my new laptop, I upgraded iPhoto to 9.5.1 and opened a library that I had backed-up from my previous laptop. iTunes refuses to sync photos with my iphone. It gts stuck on the "i

  • [svn:osmf:] 13851: OSMFPlayer: adding a nicer sample image.

    Revision: 13851 Revision: 13851 Author:   [email protected] Date:     2010-01-28 04:18:13 -0800 (Thu, 28 Jan 2010) Log Message: OSMFPlayer: adding a nicer sample image. Modified Paths:     osmf/trunk/apps/samples/framework/OSMFPlayer/html-template/in

  • How can install osx10.8, how can install osx10.8

    I need help install this software in my computer