9 Replies Latest reply on Oct 8, 2009 7:03 AM by 843807

    Jtable and vector of vector

    843807
      Hi,

      Im using a vector to get the values from jtable and assigning the values to another vector which now contains the entire list .

      Im facing problem while trying to insert it to the DB table. only the values of the first row is getting stored to the DB.

      anybody help please..
        • 1. Re: Jtable and vector of vector
          843807
          Post an SSCCE that shows how you are getting the values out of the table. Note that your SSCCE should have no database code.
          • 2. Re: Jtable and vector of vector
            walterln
            I think I read somewhere that line 42 contains the problem.

            Or for the long explanation: [How To Ask Questions The Smart Way|http://catb.org/~esr/faqs/smart-questions.html] .
            • 3. Re: Jtable and vector of vector
              843807
              Hi ,


              This is my code,..


              String [] row= {};
              String [] column = {};
              int tblrows = tbl_prd_list.getRowCount() ;
              System.out.println("No of tblrows" + tblrows);
              int tblcolumns = tbl_prd_list.getColumnCount() ;
              System.out.println("No of tblcolumns" + tblcolumns);

              Vector tbl = new Vector();

              for(int count = 0 ; count < tblrows ; count++)
              {
              Vector tblvalues = new Vector();
              tblvalues.add(tbl_prd_list.getValueAt(count, 0));
              tblvalues.add(tbl_prd_list.getValueAt(count, 1));
              tblvalues.add(tbl_prd_list.getValueAt(count, 2));
              tblvalues.add(tbl_prd_list.getValueAt(count, 3));
              tbl.add(tblvalues);

              }

              System.out.println(" values in tbl" + tbl);
              System.out.println(" size of tbl" + tbl.size());

              System.out.println(" b4 inserting values into DB");

              for(int i=0; i< tbl.size();i++){
              // Here im trying to fetch the values from tbl using tbl.get(0)....tbl.get(3) and i get array index out of range : 0 exception

              }

              Am I doing it right ? please let me know wat is the right procedure to insert into DB..

              Edited by: latchoo on Oct 7, 2009 4:52 AM
              • 4. Re: Jtable and vector of vector
                walterln
                for(int i=0; i< tbl.size();i++){
                  // Here im trying to fetch the values from tbl using tbl.get(0)....tbl.get(3)
                  // and i get array index out of range : 0 exception
                }
                This is impossible: the for loop will not even run once if tbl.size() == 0, so you cannot get an exception that 0 is out of bounds. Post the real code and exception.

                Side node, leaving out the a and e of table doesn't make your code more clear in my opinion. Compare your code to (using the for each loop):
                Vector<Vector<Object> tableRows = ...; // instead of tbl
                for(Vector<Object> row : tableRows) {
                  // clear without comments we're inserting
                  // each row of the table into the database
                  insertIntoDatabase(row);
                }
                Of course better would be to have an row object class (for example a Person class if that's what you're showing instead of Vector<Object>).
                • 5. Re: Jtable and vector of vector
                  843807
                  thanks for the response,.. could u please be more clear.. is that wat im doing is wrong ?

                  the entire code goes here:

                  int tblrows = tbl_prd_list.getRowCount() ; // getting the no of rows in jtable
                  System.out.println("No of tblrows" + tblrows);
                  int tblcolumns = tbl_prd_list.getColumnCount() ; // getting no of columns in jtable
                  System.out.println("No of tblcolumns" + tblcolumns);

                  Vector tbl = new Vector(); // main vector that is used to hold all the values from jtable finally

                  for(int count = 0 ; count < tblrows ; count++)
                  {
                  Vector tblvalues = new Vector(); // temporary vector that fetches vales from each of the jtable row
                  tblvalues.add(tbl_prd_list.getValueAt(count, 0));
                  tblvalues.add(tbl_prd_list.getValueAt(count, 1));
                  tblvalues.add(tbl_prd_list.getValueAt(count, 2));
                  tblvalues.add(tbl_prd_list.getValueAt(count, 3));
                  tbl.add(tblvalues); // assigning the values from temporary vector to main vector.

                  }
                  System.out.println(" values in tbl" + tbl);
                  System.out.println(" size of tbl" + tbl.size());

                  System.out.println(" b4 inserting values into DB");

                  for(int i=0; i< tbl.size();i++){

                  int insertdb= st.executeUpdate(RMTApplicationConstants.bill_entry_tb_bill_details + "('"+bilno+"','"+tbl.get(0)+"','"+tbl.get(1)+"','"+tbl.get(2)+"','"+tbl.get(3)+"')");

                  }
                  }


                  The exception i get is:

                  No of tblrows1
                  No of tblcolumns4
                  java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 1
                  tblvalues prdwheat
                  at java.util.Vector.get(Vector.java:694)
                  at rmt.CreateBill.insertValues(CreateBill.java:694)
                  tblvalues qty1
                  tblvalues unitskg
                  at rmt.CreateBill.btn_payActionPerformed(CreateBill.java:523)
                  tblvalues price10.0
                  at rmt.CreateBill.access$200(CreateBill.java:25)
                  at rmt.CreateBill$3.actionPerformed(CreateBill.java:192)
                  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
                  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
                  at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
                  at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
                  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
                  at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:272)
                  at java.awt.Component.processMouseEvent(Component.java:6041)
                  at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
                  at java.awt.Component.processEvent(Component.java:5806)
                  values in tbl[[wheat, 1, kg, 10.0]]
                  at java.awt.Container.processEvent(Container.java:2058)
                  size of tbl1
                  at java.awt.Component.dispatchEventImpl(Component.java:4413)
                  b4 inserting values into DB
                  at java.awt.Container.dispatchEventImpl(Container.java:2116)
                  at java.awt.Component.dispatchEvent(Component.java:4243)
                  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
                  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
                  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
                  at java.awt.Container.dispatchEventImpl(Container.java:2102)
                  at java.awt.Window.dispatchEventImpl(Window.java:2440)
                  at java.awt.Component.dispatchEvent(Component.java:4243)
                  at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
                  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
                  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
                  Caught Exception Array index out of range: 1
                  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
                  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
                  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
                  at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
                  BUILD SUCCESSFUL (total time: 2 minutes 39 seconds)

                  Edited by: latchoo on Oct 7, 2009 6:35 AM
                  • 6. Re: Jtable and vector of vector
                    walterln
                    Please use code tags when posting code.
                    for(int i=0; i< tbl.size();i++) {
                      // is this CreateBill.java: line 694?
                      int insertdb= st.executeUpdate(RMTApplicationConstants.bill_entry_tb_bill_details "('"+bilno"','"tbl.get(0)"','"tbl.get(1)"','"tbl.get(2)"','"tbl.get(3)"')");
                    
                    }
                    }
                    If yes, tbl contains the table rows. Do you really want to insert multiple row this way?.
                    • 7. Re: Jtable and vector of vector
                      843807
                      sorry,.. Im new to forums..

                      yes it is CreateBill.java :line 694

                      Im a beginner in java, Im not aware whether is that the right way to do or not.
                      The task is to insert the values listed in the Jtable to DB and the list contains products, quantity,units and price. all should be saved to a table with Billno as reference key. Kindly let me know if there is a better way to perform it.
                      • 8. Re: Jtable and vector of vector
                        843807
                        Change the second for loop as below:
                        for(int i=0; i< tbl.size();i++)
                        {
                            Vector tblvalues = tbl.get(i); // temporary vector that fetches vales from each of the jtable row
                        
                            int insertdb= st.executeUpdate(RMTApplicationConstants.bill_entry_tb_bill_details    "('"+bilno"','"tblvalues.get(0)"','"tblvalues.get(1)"','"tblvalues.get(2)"','"tblvalues.get(3)"')");
                        }
                        Edited by: hari454 on Oct 7, 2009 12:25 PM
                        • 9. Re: Jtable and vector of vector
                          843807
                          Hi, thanks a lot ,it now works fine after the modification of the for loop.. :)