Results 1 to 6 of 6

Thread: ArrayList overwriting error

  1. #1
    Join Date
    Mar 2010
    Posts
    200

    ArrayList overwriting error

    Hello,
    I work with JSF and I in one of my Java beans this code:
    Code:
    Public class MasterBean {
     
    	private List <ItemBean> coll, rwlst;
    
    	Public MasterBean() throws SQLException {
            
    	    ResultSet rslt =null;
    	    try{
    	    
    	    String u ="jdbc: oracle: thin: @ 192.168.100.35:1521: ORCL";
    DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    Connection conn = DriverManager.getConnection(u,"user","not");
    
    
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    rslt = stmt.executeQuery("SELECT FROM sym PVMCA WHERE id_cc = 11042920002");
    	    
    	    }
    	    catch (...)}     
    	    
    	    rwlst = new ArrayList <ItemBean>();
    	    while (rslt.next())
    	    {
    	      coll = new ArrayList <ItemBean>();
    String tmpSym = rslt.getString("sym");
    
    coll.add(new ItemBean(tmpSym));
    	    }
    	}
    	
    	Public List getList <ItemBean>() {
    		return coll;
    	}
     
    	Public void setlist(List <ItemBean> coll) {
    		this.coll = coll;
    	}
    	
    }
    When I compile the code I get an error, I think that this is a Arraylist overwriting problem. Please check my code and if I am wrong somewhere then please guide me with the correct.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: ArrayList overwriting error

    Hello,
    Yes you will get the following error because in your while loop each time you created a new ArrayList
    Code:
    while (rs.next())
    	    {
    	      coll = new ArrayList <ItemBean>();
    String tmp = rs.getString("sym");
    
    coll.add(new ItemBean(tmp));
    	    }
    Please correct your mistake then check if your code compiles successfully.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: ArrayList overwriting error

    Hello,
    Yes, I agree with the above post and you can modify your code something like this
    Here is the modified code
    Code:
     coll = new ArrayList <ItemBean>();
    while (rs.next())
    	    {
    	     
    	      String tmp = rs.getString("sym");
    
    coll.add(new ItemBean(tmp));
    	    }
    Another thing, your bean that accesses your Database is concerning me, I guess you have designed the beans correctly.

  4. #4
    Join Date
    Nov 2009
    Posts
    347

    Re: overwriting in arraylist

    Hello,
    I think it's normal. You create the "coll" it ever turn of your loop. We must remove this line in the while loop.
    Here is the part of code for it
    Code:
    Coll = new ArrayList <ItemBean>();
    This will give you a more effective result. If you need any more help regarding this topic then you can post here and we will try to solve your queries.

  5. #5
    Join Date
    Mar 2010
    Posts
    200

    Re: ArrayList overwriting error

    Hello,
    Thank you for your answers, but I have one more question for your guys, why access to the database is not good? I have not put the good version of the database, is this causing me a problem? I use a stored procedure to access my database, but the connection terminates, prepare it there another way? Any help regarding this will be appreciated. Thanks in advance.

  6. #6
    Join Date
    Nov 2009
    Posts
    446

    Re: java arraylist overwriting problem

    Hello,
    The access to your database given is correct but the level of conceptual separate, differentiates the layers, that is view, trends and data. This is not the case if you have your bean that accesses to the database. I think you should do some study on the design patterns, since you're working on the JSF MVC 2. I recommend you to read some articles or tutorials on the design pattern that will help you.
    Check out if the Introspection in Java Beans help you
    Why we use Introspection in Java Beans?

Similar Threads

  1. Replies: 3
    Last Post: 11-01-2014, 09:56 AM
  2. Using an ArrayList
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 12:56 PM
  3. Write to an XML file without overwriting
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 18-01-2010, 11:11 AM
  4. Page displayed overwriting
    By Shashee in forum Technology & Internet
    Replies: 4
    Last Post: 28-03-2009, 03:02 PM
  5. Replies: 2
    Last Post: 28-03-2008, 08:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,188,954.83344 seconds with 16 queries