#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Ghana
    Posts
    193
    Rep Power
    14

    Putting database record into an object


    Hi guys I need an insight on how to achieve this. I have an object called product with various methods. Now after initializing the object I pass it as
    a parameter to a method which put the object into a vector. Now when the record is viewed in JTable it shows duplicates entries and more rows than is in database and the current added entry is some where in the middle of the table view. These a codes to explain what I mean

    product
    Code:
    public class Product 
    {
        String productName = "";
        String ProductNumber = "";
        Long qty = "";
    
        public Product(){
        }
    
        public void setProductName( String productName ){
            this.productName = productName;
        }
    
        public void setProductNumber( String productNumber ) {
            this.productNumber = productNumber;
        }
    
        public void setQty( Long qty ) {
            this.qty = qty;
        }
    
        public String getProductName(){
            return productName;
        }
    
        public String getProductNumber(){
            return productNumber;
        }
    
        public Long getQty(){
            return qty;
        }
    
    
    }
    This class is custom table model
    Code:
    public class ProductTableModel extends AbstractTableModel
    {
        public static Vector<Product> list = new Vector<Product>();
    
        public ProductTableModel(){
            
        }
        
        public void getRecords(){
           try {
               /** this <code>viewProductStock</code>
                * method returns ResultSet object
                * and it contains the sql statement 
                * executes and returns the ResultSet
                */
               resultSet = jmysqlConnect.viewProductStock();
            
               while( resultSet.next() ) {
                
                  product.setProductName( resultSet.getString("item_name") );
                  product.setProductNumber( 
                    resultSet.getString( "item_number" ) );
                  product.setQty( resultSet.getString("qty" ) ) ;
                  addRow( product );//if I call this method here,
                  //I get duplicate entries showing with the current entry
                  //somewhere in the middle of the duplicate entries
               }
           }catch( SQLException e ) {
              System.out.println( e.getMessage() );
           }
            //addRow( product ); if I call this method here I get 
            //only the current entry entered into the databases
        
        }// This method is called after a button click
    
        /**
         * Add a row ( a <code>Product</code> ) to this model
         *
         * @param product the product to add
         */
        public void addRow( Product product ) {
    	list.add( product );
    	fireTableRowsInserted( 0, 100 );
    	
        }
    Screen shoot below
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Hatfield
    Posts
    280
    Rep Power
    89
    This may not be the standard way but i do the following when using a class oject the way you are using it:

    Code:
     
    
    public void getDataMethod(){
    
    myMethod.AddRow(new Product(value1, value2,etc));
    
    
    }
    
    //Get product out, iterate through list.
    public Object getDataOutMethod()
    {
    for (int i = 0; i < arrayList.size(); i++)
    {
    Product p = (Product) arrayList.get(i);
    
    p.getMethod1();
    
    p.getMethod2();
    
    
    }
    }
    But thats just me, it just automates the code a little and allows alot better access, without creating multinamed objects or reusing the same object where a potential error could occur.
  4. #3
  5. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    It seems you are using the same product object over and over again. This will indeed result in an ArrayList, where each element contains the same object. You should construct a new Product instance each time you read values from the database. I also suggest you write a constructor that sets the three values (Name, Number and Qty).

    Comments on this post

    • tfecw agrees
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Ghana
    Posts
    193
    Rep Power
    14
    thanks for the response, I'm trying figure out your code but seems not
    to get it. Where do I use the "getDataOutMethod". Please could you let it relate to my code. thanks
  8. #5
  9. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    Originally Posted by eyedol
    thanks for the response, I'm trying figure out your code but seems not
    to get it. Where do I use the "getDataOutMethod". Please could you let it relate to my code. thanks
    Dagma's code just gets the data out of the ArrayList (not the database) and performs some random methods. IMHO a dead-end for your particular problem as you seem to be able to get the data out (screenshot).
    I am almost sure that the problem is in the reusing of the product variable as I mentioned in my previous post.

    Comments on this post

    • Yawmark agrees
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2005
    Location
    Ghana
    Posts
    193
    Rep Power
    14
    I managed to fix it based on the code and explanation given. I moved the creation of object of the product class to the while loop and it solved it

IMN logo majestic logo threadwatch logo seochat tools logo